active_plugin(string $plugin, string $redirect =”, bool $network_wide =false, bool $silent= flase ): null|WP_Error
از این هوک برای فعال کردن پلاگین به صورت مطمئن و بدون خطا و ریدایرکت در صورت موفقیت آمیز بودن استفاده میشود.
توضیحات active_plugin:
افزونهای که قبلاً فعال شده است، دوباره فعال نمیشود.
روش کار این است که قبل از تلاش برای گنجاندن فایل افزونه، تغییر مسیر را روی خطا تنظیم کنید. اگر پلاگین خراب شود، تغییر مسیر با پیام موفقیت بازنویسی نخواهد شد. همچنین گزینه ها به روز نمی شوند و قلاب فعال سازی بر روی خطای افزونه فراخوانی نمی شود.
پارامترها:
plugin$: مسیر فایل پلاگین نسبت به پوشه پلاگینها. این پارامتر ضروری و از نوع string میباشد.
redirect$: مسیری که بعد از فعال سازی میخواهید ریدایرکت شود. این پارامتر اختیاری است و مقدار پیش فرض آن ” میباشد.
network_wide$: در صورتی که از وردپرس شبکه استفاده میکنید، با این پارامتر میتوانید مشخص کنید برای کل شبکه فعال شود یا شبکه انتخابی شما.این پارامتر انتخابی و مقدار پیش فرض آن false میباشد.
silent$:
برای جلوگیری از فراخوانی هوک فعال سازی. این پارامتر انتخابی و پیش فرض آن مقدار false میباشد.
مقدار بازگشتی:
معمولا یک پلاگین به دلیل بروز خطا در هدر، مشکل Cache، یا خطای مجوزها فعال نمیشود و با خطا مواجه میشود.
The plugin does not have a valid header.
مشکلات مربوط به کش افزونه، زمانی ایجاد می شود که فایل های افزونه اضافه یا اصلاح می شوند، پس از اینکه همه افزونه ها مقداردهی اولیه شدند. این مشکل را می توان با رفرش کردن صفحه و ارسال activate_plugin() به عنوان یک درخواست جداگانه AJAX یا در صورت لزوم با به روز رسانی دستی کش حل کرد. مثال زیر:
$cache_plugins = wp_cache_get( 'plugins', 'plugins' );
if ( !empty( $cache_plugins ) ) {
$new_plugin = array(
'Name' => $plugin_name,
'PluginURI' => $plugin_uri,
'Version' => $plugin_version,
'Description' => $plugin_description,
'Author' => $author_name,
'AuthorURI' => $author_uri,
'TextDomain' => '',
'DomainPath' => '',
'Network' => '',
'Title' => $plugin_name,
'AuthorName' => $author_name,
);
$cache_plugins[''][$plugin_path] = $new_plugin;
wp_cache_set( 'plugins', $cache_plugins, 'plugins' );
}
منبع:
wp-admin/includes/plugin.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() );
$_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
} else {
$current = get_option( 'active_plugins', array() );
}
$valid = validate_plugin( $plugin );
if ( is_wp_error( $valid ) ) {
return $valid;
}
$requirements = validate_plugin_requirements( $plugin );
if ( is_wp_error( $requirements ) ) {
return $requirements;
}
if ( $network_wide && ! isset( $current[ $plugin ] )
|| ! $network_wide && ! in_array( $plugin, $current, true )
) {
if ( ! empty( $redirect ) ) {
// We'll override this later if the plugin can be included without fatal error.
wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) );
}
ob_start();
// Load the plugin to test whether it throws any errors.
plugin_sandbox_scrape( $plugin );
if ( ! $silent ) {
/**
* Fires before a plugin is activated.
*
* If a plugin is silently activated (such as during an update),
* this hook does not fire.
*
* @since 2.9.0
*
* @param string $plugin Path to the plugin file relative to the plugins directory.
* @param bool $network_wide Whether to enable the plugin for all sites in the network
* or just the current site. Multisite only. Default false.
*/
do_action( 'activate_plugin', $plugin, $network_wide );
/**
* Fires as a specific plugin is being activated.
*
* This hook is the "activation" hook used internally by register_activation_hook().
* The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
*
* If a plugin is silently activated (such as during an update), this hook does not fire.
*
* @since 2.0.0
*
* @param bool $network_wide Whether to enable the plugin for all sites in the network
* or just the current site. Multisite only. Default false.
*/
do_action( "activate_{$plugin}", $network_wide );
}
if ( $network_wide ) {
$current = get_site_option( 'active_sitewide_plugins', array() );
$current[ $plugin ] = time();
update_site_option( 'active_sitewide_plugins', $current );
} else {
$current = get_option( 'active_plugins', array() );
$current[] = $plugin;
sort( $current );
update_option( 'active_plugins', $current );
}
if ( ! $silent ) {
/**
* Fires after a plugin has been activated.
*
* If a plugin is silently activated (such as during an update),
* this hook does not fire.
*
* @since 2.9.0
*
* @param string $plugin Path to the plugin file relative to the plugins directory.
* @param bool $network_wide Whether to enable the plugin for all sites in the network
* or just the current site. Multisite only. Default false.
*/
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;
}
هوکهای قابل اتصال:
do_action( 'activated_plugin', string $plugin, bool $network_wide )
بعد از اکتیو شدن یک افزونه فایر میشود.
do_action( "activate_{$plugin}", bool $network_wide )
بعد از فعال شدن یک افزونه مشخص این اکشن فایر میشود.
چند مثال:
یک مثال ساده:
کد زیر سعی میکند افزونه را فعال کند و در صورت فعال نشدن یک خطا از WP_ERROR برمیگرداند.
$result = activate_plugin( 'plugin-dir/plugin-file.php' );
if ( is_wp_error( $result ) ) {
// Process Error
}
نکته مهم:
add_action( 'activated_plugin', 'wpdocs_my_redirection' );
function wpdocs_my_redirection( $plugin ) {
$table = new WP_Plugins_List_Table;
if ( plugin_basename( __FILE__ ) === $plugin && 'activated-selected' !== $table->current_action() ) {
wp_redirect( ... );
exit();
}
}