首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress插件admin_notices不显示

Wordpress插件admin_notices不显示
EN

Stack Overflow用户
提问于 2022-04-02 05:13:12
回答 3查看 123关注 0票数 0

我试图创建一个Wordpress插件,并且无法使用admin_notices功能显示错误消息。

代码语言:javascript
复制
function gbrew_notice_no_api_key() {
    $url = admin_url('admin.php') . '?page=api-key';
    ?>
    <div class="notice-info notice">
        <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');
EN

回答 3

Stack Overflow用户

发布于 2022-04-02 08:50:15

我认为在您的第一个函数中,您可以更好地返回HTML,这可能会解决您的问题。

代码语言:javascript
复制
function gbrew_notice_no_api_key() {
    $url = admin_url('admin.php') . '?page=api-key';
    return <<<HTML
    <div class="notice-info notice">
        <p>Please set up your client ID and client secret from the <a href='$url'>API Key</a> page first.</p>
    </div>
HTML;
}
票数 1
EN

Stack Overflow用户

发布于 2022-04-02 10:07:54

你的呼叫通知太晚了,它们会在admin_menu之前开火;试试这个

代码语言:javascript
复制
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 class="notice notice-success is-dismissible">
            <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');

如果您也在运行多站点,可能也值得添加:add_action('network_admin_notices', 'gbrew_notice_no_api_key');

票数 0
EN

Stack Overflow用户

发布于 2022-10-06 18:19:01

在我看来,问题就在gbrew_dashboard函数中。

代码语言:javascript
复制
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');
    }
}

else语句中,您将gbrew_notice_no_api_key添加到admin_notices中,但是的操作从未被称为。阅读这个答案关于do_actionadd_action如何串联工作的文章。总而言之,

do_action()函数执行与add_action()挂钩的任何代码。

我认为最简单的解决方案如下:

代码语言:javascript
复制
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 {
        do_action('admin_notices');
    }
}

add_action('admin_notices', 'gbrew_notice_no_api_key');

无论如何,将gbrew_notice_no_api_key添加到操作中,然后使用do_action调用它。

您可以使用自定义操作(可选)

但是,请记住,如果调用do_action('admin_notices'),则插件将调用与 _挂钩的每个函数,即使它们属于其他plugins**_。

如果你对此不满意,那么你可以用添加自定义操作代替。将gbrew作为唯一识别你自己的钩子的前缀,可以添加一个仅由插件调用的自定义操作。在这种情况下,代码如下所示:

代码语言:javascript
复制
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 {
        // calling your custom action here!!!
        do_action('gbrew_admin_notices');
    }
}

add_action('gbrew_admin_notices', 'gbrew_notice_no_api_key');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71715037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档