1
votes

I have created custom divi module and then convert it to the plugin. Now I want to active plugin only if Divi theme is activated. when i write code like this way it shows notice everytime.

function my_admin_notice(){
    echo '<div class="updated">
       <p>test Admin notice.</p>
    </div>';
}
add_action('admin_notices', 'my_admin_notice');

But when I write code with conditions based

    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>';
    }

I copied code for showing admin notice here https://wptheming.com/2011/08/admin-notices-in-wordpress/

Now add_action I am calling in function angelleye_setup_For_paypal_divi_install But in the function add_action is not working,.

2
I think that your add_action is working but there is a problem within that add_action function. Try echo Hello World; before implementing logic within the function. If you see hello world then the add_action is working but there is a problem with the logic you are implementing - Fahad Sohail
@FahadSohail Check my code now. - Tejas Mehta
@FahadSohail I only write echo and it is also not calling function :( - Tejas Mehta

2 Answers

3
votes

You are adding condition in wrong place. Please remove condition from here.

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

Add this code after activation function for display admin notice

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

The problem with your code is that you are missing two requirements of admin notices {class}and {message} as mentioned in https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices

try

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
}