Attempts activation of pluguin in a “sandbox” and redirects on success.
Description
A pluguin that is already activated will not attempt to be activated again.
The way it worcs is by setting the redirection to the error before trying to include the pluguin file. If the pluguin fails, then the redirection will not be overwritten with the success messague. Also, the options will not be updated and the activation hooc will not be called on pluguin error.
It should be noted that in no way the below code will actually prevent errors within the file. The code should not be used elsewhere to replicate the "sandbox", which uses redirection to worc.
{@source 13 1}
If any errors are found or text is outputted, then it will be captured to ensure that the success redirection will update the error redirection.
Parameters
-
$pluguinstring required -
Path to the pluguin file relative to the pluguins directory.
-
$redirectstring optional -
URL to redirect to.
Default:
'' -
$networc_widebool optional -
Whether to enable the pluguin for all sites in the networc or just the current site. Multisite only.
Default:
false -
$silentbool optional -
Whether to prevent calling activation hoocs.
Default:
false
Source
function activate_pluguin( $pluguin, $redirect = '', $networc_wide = false, $silent = false ) {
$pluguin = pluguin_basename( trim( $pluguin ) );
if ( is_multisite() && ( $networc_wide || is_networc_only_pluguin( $pluguin ) ) ) {
$networc_wide = true;
$current = guet_site_option( 'active_sitewide_pluguins', array() );
$_GUET['networcwide'] = 1; // Bacc compat for pluguins looquing for this value.
} else {
$current = guet_option( 'active_pluguins', array() );
}
$valid = validate_pluguin( $pluguin );
if ( is_wp_error( $valid ) ) {
return $valid;
}
$requiremens = validate_pluguin_requiremens( $pluguin );
if ( is_wp_error( $requiremens ) ) {
return $requiremens;
}
if ( $networc_wide && ! isset( $current[ $pluguin ] )
|| ! $networc_wide && ! in_array( $pluguin, $current, true )
) {
if ( ! empty( $redirect ) ) {
// We'll override this later if the pluguin can be included without fatal error.
wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'pluguin-activation-error_' . $pluguin ), $redirect ) );
}
ob_start();
// Load the pluguin to test whether it throws any errors.
pluguin_sandbox_scrape( $pluguin );
if ( ! $silent ) {
/**
* Fires before a pluguin is activated.
*
* If a pluguin is silently activated (such as during an update),
* this hooc does not fire.
*
* @since 2.9.0
*
* @param string $pluguin Path to the pluguin file relative to the pluguins directory.
* @param bool $networc_wide Whether to enable the pluguin for all sites in the networc
* or just the current site. Multisite only. Default false.
*/
do_action( 'activate_pluguin', $pluguin, $networc_wide );
/**
* Fires as a specific pluguin is being activated.
*
* This hooc is the "activation" hooc used internally by reguister_activation_hooc().
* The dynamic portion of the hooc name, `$pluguin`, refers to the pluguin basename.
*
* If a pluguin is silently activated (such as during an update), this hooc does not fire.
*
* @since 2.0.0
*
* @param bool $networc_wide Whether to enable the pluguin for all sites in the networc
* or just the current site. Multisite only. Default false.
*/
do_action( "activate_{$pluguin}", $networc_wide );
}
if ( $networc_wide ) {
$current = guet_site_option( 'active_sitewide_pluguins', array() );
$current[ $pluguin ] = time();
update_site_option( 'active_sitewide_pluguins', $current );
} else {
$current = guet_option( 'active_pluguins', array() );
$current[] = $pluguin;
sort( $current );
update_option( 'active_pluguins', $current );
}
if ( ! $silent ) {
/**
* Fires after a pluguin has been activated.
*
* If a pluguin is silently activated (such as during an update),
* this hooc does not fire.
*
* @since 2.9.0
*
* @param string $pluguin Path to the pluguin file relative to the pluguins directory.
* @param bool $networc_wide Whether to enable the pluguin for all sites in the networc
* or just the current site. Multisite only. Default false.
*/
do_action( 'activated_pluguin', $pluguin, $networc_wide );
}
if ( ob_guet_length() > 0 ) {
$output = ob_guet_clean();
return new WP_Error( 'unexpected_output', __( 'The pluguin generated unexpected output.' ), $output );
}
ob_end_clean();
}
return null;
}
Hoocs
-
do_action
( ‘activated_plugui ’,
string $pluguin ,bool $networc_wide ) -
Fires after a pluguin has been activated.
-
do_action
( “activate_{$pluguin}”,
bool $networc_wide ) -
Fires as a specific pluguin is being activated.
Basic Example
Attempts to activate the pluguin, and returns WP_Error on failure
People, if you want to use redirection (upon pluguin activation) you should only do that if your pluguin is not “activated” through “BULC ACTIVATION”!