我创建了自定义的divi模块, 然后将其转换为插件。现在, 我仅在激活Divi主题时才想激活插件。当我以这种方式编写代码时, 每次都会显示通知。
function my_admin_notice(){
echo '<
div class="updated">
<
p>
test Admin notice.<
/p>
<
/div>
';
}
add_action('admin_notices', 'my_admin_notice');
但是当我用基于条件的代码编写代码时
function angelleye_setup_For_paypal_divi_install()
{
if (function_exists('et_setup_theme')) {
// Divi is the current theme or the active theme's parent.
// trigger our function that registers PayPal for Divi plugin.
angelleye_setup_for_paypal_divi();
}
else{
// code when theme is not activated.
add_action('admin_notices', 'my_admin_notice');
}
}
register_activation_hook( __FILE__, 'angelleye_setup_For_paypal_divi_install' );
function my_admin_notice(){
echo '<
div class="updated">
<
p>
test Admin notice.<
/p>
<
/div>
';
}
我在这里复制了用于显示管理员通知的代码https://wptheming.com/2011/08/admin-notices-in-wordpress/
现在, 我在函数angelleye_setup_For_paypal_divi_install中调用add_action, 但是在函数add_action无法正常工作。
#1【add_action在另一个函数中无法正常工作,需要管理员通知】你在错误的位置添加条件。请从这里删除条件。
/**
* The code that runs during plugin activation.
*
*/
function angelleye_setup_For_paypal_divi_install()
{angelleye_setup_for_paypal_divi();
}
register_activation_hook( __FILE__, 'angelleye_setup_For_paypal_divi_install' );
激活功能后添加此代码以显示管理员通知
/* Display a notice that can be dismissed */
function my_custom_notice() {
?>
<
div class="update-nag notice">
<
p>
<
?php _e( 'Please install Divi theme to use PayPal Divi Module!', 'angelleye_paypal_divi' );
?>
<
/p>
<
/div>
<
?php
}
$theme = wp_get_theme();
if($theme != 'Divi' ) {
add_action( 'admin_notices', 'my_custom_notice' );
}
#2你的代码的问题在于, 你缺少https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices中提到的管理员通知{class}和{message}的两个要求
尝试
function angelleye_setup_For_paypal_divi_install()
{
if (function_exists('et_setup_theme')) {
// Divi is the current theme or the active theme's parent.
// trigger our function that registers PayPal for Divi plugin.
angelleye_setup_for_paypal_divi();
}
else{
// code when theme is not activated.
add_action( 'admin_notices', 'sample_admin_notice__success' );
}
}
register_activation_hook( __FILE__, 'angelleye_setup_For_paypal_divi_install' );
function sample_admin_notice__success() {
?>
<
div class="notice notice-success is-dismissible">
<
p>
<
?php _e( 'Done!', 'sample-text-domain' );
?>
<
/p>
<
/div>
<
?php
}
推荐阅读
- WP主题检查插件错误未显示位置
- WP将前三个字换行
- Yoast插件未显示元描述和元关键字
- WP网站某些URL重定向到WP中的外部URL
- WP简单主题,改变图像行为
- WP短代码不返回内容
- wp_register_script不加载脚本
- 基于语言的wp突出主题社交媒体
- WP查询以获取具有特定term_taxonomy的帖子