• I am testing the admin_notices hook in a plugin with the following code.

    
    function sample_admin_notice__success() {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php _e( 'Done!', 'textdomain' ); ?></p>
        </div>
        <?php
    }
    add_action( 'admin_notices', 'sample_admin_notice__success' );
    

    For some reason the notices are displayed above the title and then move below it. I tried searching Google and the WordPress forums for a solution but didn’t find any. Is there a way to fix this problem? I am testing WordPress on my localhost and this is the only plugin activated.

    Thank

    Marcus

    • This topic was modified 7 years, 10 months ago by Jose Castaneda.
    • This topic was modified 7 years, 10 months ago by Jose Castaneda. Reason: added backticks to code
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Are you able to share a screenshot to show what you mean?

    Or a gif, or movie clip?

    Thread Starter mlc1959

    (@mlc1959)

    Jose here are two screenshots.

    Notices 1

    Notice 2

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Is that really the only thing that you are doing?

    If so, I’ll test on my local install to see if I get the same result.

    Thread Starter mlc1959

    (@mlc1959)

    No! I am using this code to debug the admin_notices hook.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Gotcha.

    Had to take a look at the code and part of that is because of the way the DOM is loaded. The hook is fired here: https://core.trac.ww.wp.xz.cn/browser/tags/4.9.7/src/wp-admin/admin-header.php#L255

    Which if you take a close look at the markup happens just before the most of the <h1>Heading Title for the page</h1>

    It is then moved via JavaScript here: https://core.trac.ww.wp.xz.cn/browser/tags/4.9.7/src/wp-admin/js/common.js#L514

    Hope this helps shed some light. I know it did for me. 🙂

    Thread Starter mlc1959

    (@mlc1959)

    Is there a way to fix this without writing a lot of code?

    Just adding my two bits on this. Been trying to get to display some info messages for during save_post (after validating some custom meta fields) for a woocommerce admin side order.

    After searching through length and breadth of various sites & forums, I could only get a custom message displayed with this

    https://onextrapixel.com/10-tips-for-a-deeply-customised-wordpress-admin-area/

    function frl_on_save_post($post_id, $post) {/* add warning filter when saving post */
     
        if($post->post_type == 'post') //test for something real here       
            add_filter('redirect_post_location', 'frl_custom_warning_filter');
     
    }
    add_action('save_post', 'frl_on_save_post', 2, 2);
     
    function frl_custom_warning_filter($location) { /* filter redirect location to add warning parameter*/
     
        $location = add_query_arg(array('warning'=>'my_warning'), $location);
        return $location;
    }
     
    function frl_warning_in_notice() { /* print warning message */
             
        if(!isset($_REQUEST['warning']) || empty($_REQUEST['warning']))
            return;
             
        $warnum = trim($_REQUEST['warning']);
     
        /* possible warnings codes and messages */                 
        $warnings = array(
            'my_warning' => __('This is my truly custom warning!', 'frl')
        );
             
        if(!isset($warnings[$warnum]))
            return; 
         
        echo '<div class="error message"><p><strong>';
        echo $warnings[$warnum];
        echo '</strong></p></div>';
    }       
    add_action('admin_notices', 'frl_warning_in_notice');
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘admin_notices hook doesn’t work correctly.’ is closed to new replies.