0

I am new to WordPress. I have created a Shortcode. Now I want to add description of Shortcode to Pages->Add New Page so the user can easily use the Shortcode which I have created.

I want that text to be added on plugin activation with register_activation_hook and will get removed on deactivation with register_deactivation_hook.

1 Answer 1

0

The hook to use here is admin_notices. However in register_activation_hook and register_deactivation_hook it shouldn't be used because these function run when plugin activates and deactivates instance.

A workaround it we use add_option when plugin activates and delete_option when plugin deactivates.

Here is the code. Hopefully you will understand it properly

    <?php 
    function my_admin_notice() {
        $screen = get_current_screen();
        if($screen->post_type != 'your-post-type-name')
            return;
        if($screen->base != 'post')
            return;
        ?>
        <div class="updated">
            <p><?php _e( 'Updated!', 'my-text-domain' ); ?></p>
        </div>
        <?php
    }

    function display_admin_notice(){
        $display_admin_msg = get_option('display_admin_msg');
        if($display_admin_msg == 1){
            add_action( 'admin_notices', 'my_admin_notice' );
        }
    }
    add_action('admin_init','display_admin_notice');

    function my_plugin_activate() {
         add_option('display_admin_msg',1);
    }
    register_activation_hook( __FILE__, 'my_plugin_activate' );


    function myplugin_deactivate(){
        delete_option('display_admin_msg');
    }
    register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
 ?>
6
  • Can you tell me where the notice will be displayed?
    – Abhee
    Commented Jun 30, 2015 at 6:53
  • Top of every page inside admin panel.
    – Karun
    Commented Jun 30, 2015 at 6:54
  • Actually I want it only on "Add New Page" and "Add New Post" of admin and it should be like a note for page placed below the form.
    – Abhee
    Commented Jun 30, 2015 at 6:59
  • I updated the above code. You can check.
    – Karun
    Commented Jun 30, 2015 at 7:04
  • Thanks @karun But it is showing like notice on top of the page I want it to be display below the form as label or page content.
    – Abhee
    Commented Jun 30, 2015 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.