Home > Blockchain >  Wordpress plugin admin_notices not displaying
Wordpress plugin admin_notices not displaying

Time:04-02

I'm trying to create a Wordpress plugin and I'm unable to display error messages using the admin_notices feature.

function gbrew_notice_no_api_key() {
    $url = admin_url('admin.php') . '?page=api-key';
    ?>
    <div >
        <p><?php _e("Please set up your client ID and client secret from the <a href='{$url}'>API Key</a> page first."); ?></p>
    </div>
    <?php
}

function gbrew_setup_menu() {
    # Add the main menu option
    add_menu_page('Spruce Beer Dashboard', 'Spruce Beer', 'manage_options', 'spruce-beer', 'gbrew_dashboard');

    # Add the sub menu item to manage API keys
    add_submenu_page('spruce-beer', 'API Key', 'API Key', 'manage_options', 'api-key', 'gbrew_manage_api_key');
}
 
function gbrew_dashboard() {
    $client_id = get_option('untappd_client_id');
    $client_secret = get_option('untappd_client_secret');

    echo "<h1>Spruce Beer</h1>";

    if(!empty($client_id) && !empty($client_secret)) {
        var_dump($client_id);
        var_dump($client_secret);
    } else {
        add_action('admin_notices', 'gbrew_notice_no_api_key');
    }
}

# Add the plugin to the sidebar menu
add_action('admin_menu', 'gbrew_setup_menu');

CodePudding user response:

already I think in your first function you can do better to return the HTML and maybe that will fix your problem

function gbrew_notice_no_api_key() {
    $url = admin_url('admin.php') . '?page=api-key';
    return <<<HTML
    <div >
        <p>Please set up your client ID and client secret from the <a href='$url'>API Key</a> page first.</p>
    </div>
HTML;
}

CodePudding user response:

Your calling notices too late, they fire before admin_menu; try this

function gbrew_notice_no_api_key()
{
    $client_id = get_option('untappd_client_id');
    $client_secret = get_option('untappd_client_secret');
    if (empty($client_id) && empty($client_secret)) {
        $url = admin_url('admin.php') . '?page=api-key';
        ?>
        <div >
            <p><?php
                _e("Please set up your client ID and client secret from the <a href='{$url}'>API Key</a> page first."); ?></p>
        </div>
        <?php
    }
}

function gbrew_setup_menu()
{
    # Add the main menu option
    add_menu_page('Spruce Beer Dashboard', 'Spruce Beer', 'manage_options', 'spruce-beer', 'gbrew_dashboard');

    # Add the sub menu item to manage API keys
    add_submenu_page('spruce-beer', 'API Key', 'API Key', 'manage_options', 'api-key', 'gbrew_manage_api_key');
}

function gbrew_dashboard()
{
    $client_id = get_option('untappd_client_id');
    $client_secret = get_option('untappd_client_secret');

    echo "<h1>Spruce Beer</h1>";

    if ( ! empty($client_id) && ! empty($client_secret)) {
        var_dump($client_id);
        var_dump($client_secret);
    } else {
        echo "<p>No Api Key Exists";
    }
}

# Add the plugin to the sidebar menu
add_action('admin_menu', 'gbrew_setup_menu');
add_action('admin_notices', 'gbrew_notice_no_api_key');

If your also running multisite, might be worth adding: add_action('network_admin_notices', 'gbrew_notice_no_api_key'); as well

  • Related