add_action不适用于管理员通知的另一个功能

时间:2017-02-02 06:31:18

标签: php wordpress wordpress-theming

我创建了 custom divi module ,然后将其转换为插件。 现在我只想激活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/

现在add_action我正在调用函数 angelleye_setup_For_paypal_divi_install 但是在函数中add_action不起作用,。

2 个答案:

答案 0 :(得分:2)

你在错误的地方添加条件。请从这里删除条件。

/**
 * 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' );
}

答案 1 :(得分:0)

您的代码存在的问题是,您错过了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
}