Search code examples
phpwordpresshookadmin

How can I replicate WordPress' update notification?


In WordPress I've learned how to use wp_insert_post() to insert a custom JSON link but I'm wanting a way I can conditionalize the status of the JSON pull and if I were to receive an error I can flag the custom post type with the count of errors for review. I've spent awhile looking for this in the codex and developer documentation with no success. I've tried browsing the site under WordPress admin but I've had no luck.

I'm using an older WordPress version to throw what I am talking about. In this screenshot you will see the circle with the number for updates:

enter image description here

here is the red circle to indicate a plugin needs to be updated:

enter image description here

What is this called and what is the hook I can use to tie into this so when I use wp_insert_post() I can throw the circle with the count on the errors the JSON returns so I can review what is going on?

When I navigate through the core's wp-admin folder I am able to find the code that controls this under update-core.php but I am unable to resolve how this is done. In menu.php I do see:

if ( ! is_multisite() ) {
    if ( current_user_can( 'update_core' ) )
        $cap = 'update_core';
    elseif ( current_user_can( 'update_plugins' ) )
        $cap = 'update_plugins';
    else
        $cap = 'update_themes';
    $submenu[ 'index.php' ][10] = array( sprintf( __('Updates %s'), "<span class='update-plugins count-{$update_data['counts']['total']}' title='{$update_data['title']}'><span class='update-count'>" . number_format_i18n($update_data['counts']['total']) . "</span></span>" ), $cap, 'update-core.php');
    unset( $cap );
}

so I tried researching anything on the variable $cap to see if I can pull something up in the codex but still nothing.

To clarify: when I detect one of the responses in my JSON feed, for example the error status, im going to conditionalize it to create a post for error_cpt. Ive already done it and it works, however Id like to throw the circle with the count of new posts under the new post status I created for the error_cpt and when I review the post and change the post status to something else, like post status reviewed the circle with the count will go away. I need the hook that throws the circle with count so if I have 6 error posts there would be a circle with 6 next to that custom post type error_cpt.


Solution

  • You call add_options_page(), not later. It's always better to do this with the supported API instead of playing with the internal structures:

    add_action( 'admin_menu', 'customnotification_created' );
    function customnotification_created()
    {
        $warnings = get_transient( 'custom_warnings' ); // You can place your json count
        $warning_count = count( $warnings );
        $warning_title = esc_attr( sprintf( '%d plugin warnings', $warning_count ) );
    
        $menu_label = sprintf( __( 'Plugin Checker %s' ), "<span class='update-plugins count-$warning_count' title='$warning_title'><span class='update-count'>" . number_format_i18n($warning_count) . "</span></span>" );
    
        add_options_page( 'Plugin Check', $menu_label, 'activate_plugins', 'sec_plugin_check', 'sec_checker' );
    }
    

    Please refer to Add update notification bubble to admin menu item?