activate_plugin [ WordPress Function ]
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
Attempts activation of plugin in a "sandbox" and redirects on success.
A plugin that is already activated will not attempt to be activated again.
The way it works is by setting the redirection to the error before trying to include the plugin file. If the plugin fails, then the redirection will not be overwritten with the success message. Also, the options will not be updated and the activation hook will not be called on plugin 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 work. {@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.
Source
<?php
function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
$plugin = plugin_basename( trim( $plugin ) );
if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) {
$network_wide = true;
$current = get_site_option( 'active_sitewide_plugins', array() );
} else {
$current = get_option( 'active_plugins', array() );
}
$valid = validate_plugin($plugin);
if ( is_wp_error($valid) )
return $valid;
if ( !in_array($plugin, $current) ) {
if ( !empty($redirect) )
wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
ob_start();
include_once(WP_PLUGIN_DIR . '/' . $plugin);
if ( ! $silent ) {
do_action( 'activate_plugin', $plugin, $network_wide );
do_action( 'activate_' . $plugin, $network_wide );
}
if ( $network_wide ) {
$current[$plugin] = time();
update_site_option( 'active_sitewide_plugins', $current );
} else {
$current[] = $plugin;
sort($current);
update_option('active_plugins', $current);
}
if ( ! $silent ) {
do_action( 'activated_plugin', $plugin, $network_wide );
}
if ( ob_get_length() > 0 ) {
$output = ob_get_clean();
return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
}
ob_end_clean();
}
return null;
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- how to activate wordpress plugins internally? - Stack Overflow
[i've tried different combinations for sending arguments, echoing arguments in plugin.php etc. but activate_plugin() does receive correct ...
stackoverflow.com - activate_plugin | A HitchHackers guide through WordPress
Feb 11, 2011 ... function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) { $plugin = plugin_basename( trim( $plugin ) ); if ...
hitchhackerguide.com - PHPXRef 0.7 : WordPress : Function Reference: activate_plugin()
Function and Method Cross Reference. activate_plugin(). Defined at: /wp-admin/ includes/plugin.php -> line 491. Referenced 5 times: ...
phpxref.ftwr.co.uk - activate_plugin (WordPress Function) - WPSeek.com
WordPress lookup for activate_plugin, a WordPress Function. wpseek.com is a WordPress-centric search tool for developers and theme authors.
wpseek.com