最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - How to show notice alert only on product dashboard page?

programmeradmin4浏览0评论

I want to remind to my plugin users that the plugin is enabled but only on product page (dashboard), i've came across the $pagenow global variable:

global $pagenow;
$pagenow != 'post.php'; // This will be shown on "normal posts" too

How can i add a notice alert only on product page? This is what i have at the moment:

add_action('admin_notices', function () {
    if (!is_admin()) {
        return;
    }

    $title = __e('Your plugin is enabled');
    $div = '<div class="notice notice-info is-dismissible"><p>%s</p></div>';

    $pluginPath = 'path-to/my-plugin.php';
    if (is_plugin_active($pluginPath)) {
        echo sprintf($div, $title);
    }
});

I want to remind to my plugin users that the plugin is enabled but only on product page (dashboard), i've came across the $pagenow global variable:

global $pagenow;
$pagenow != 'post.php'; // This will be shown on "normal posts" too

How can i add a notice alert only on product page? This is what i have at the moment:

add_action('admin_notices', function () {
    if (!is_admin()) {
        return;
    }

    $title = __e('Your plugin is enabled');
    $div = '<div class="notice notice-info is-dismissible"><p>%s</p></div>';

    $pluginPath = 'path-to/my-plugin.php';
    if (is_plugin_active($pluginPath)) {
        echo sprintf($div, $title);
    }
});
Share Improve this question asked May 14, 2020 at 22:27 RFLRFL 731 silver badge8 bronze badges 3
  • I'd guess there'll be a post_type=product query string parameter that you can test for too? – Rup Commented May 14, 2020 at 22:33
  • (I'd also guess that is_admin will always be true in an admin_init handler, but there's no harm in checking.) – Rup Commented May 14, 2020 at 22:34
  • @Rup on product editing page there's no ?post_type=product query string – RFL Commented May 14, 2020 at 23:09
Add a comment  | 

1 Answer 1

Reset to default 1

If you're on wp-admin/post.php, you've got either

  • global $post_type
  • or possibly $_POST['post_type'], if we don't have a post ID to look up the type from

that you can test for 'product'. See the first 50 lines of wp-admin/post.php.

As an aside, I don't think you need to check for is_admin in a admin_init handler; nor do you need to check if your plugin is enabled if this code is in the same plugin, since it won't be run unless the plugin is enabled. Unless you're testing for a different plugin that is.

发布评论

评论列表(0)

  1. 暂无评论