admin管理员组

文章数量:1389779

I have a page created by a plugin (Dokan) called Products: example/dashboard/products

The /dashboard/products page is created by a rewrite rule, it's not an actual page which can be seen in the admin panel. Therefore I can't use this conditional: is_page(page-id).

How else could I do a WordPress conditional to check the page is the /dashboard/products page?

This is the register_rule function code from the plugin:

$this->query_vars = apply_filters( 'dokan_query_var_filter', array(
    'products',
    'new-product',
    'orders',
    'withdraw',
    'settings',
    'edit-account'
) );

foreach ( $this->query_vars as $var ) {
    add_rewrite_endpoint( $var, EP_PAGES );
}

My end goal is to wrap these two lines in a conditional so they only run when on the /dashboard/products page:

add_action( 'dokan_dashboard_content_inside_before', 'custom_dashboard_menu', 10 );
add_action( 'dokan_dashboard_content_before', 'custom_dashboard_header', 10 );

I have a page created by a plugin (Dokan) called Products: example/dashboard/products

The /dashboard/products page is created by a rewrite rule, it's not an actual page which can be seen in the admin panel. Therefore I can't use this conditional: is_page(page-id).

How else could I do a WordPress conditional to check the page is the /dashboard/products page?

This is the register_rule function code from the plugin:

$this->query_vars = apply_filters( 'dokan_query_var_filter', array(
    'products',
    'new-product',
    'orders',
    'withdraw',
    'settings',
    'edit-account'
) );

foreach ( $this->query_vars as $var ) {
    add_rewrite_endpoint( $var, EP_PAGES );
}

My end goal is to wrap these two lines in a conditional so they only run when on the /dashboard/products page:

add_action( 'dokan_dashboard_content_inside_before', 'custom_dashboard_menu', 10 );
add_action( 'dokan_dashboard_content_before', 'custom_dashboard_header', 10 );
Share Improve this question asked Mar 21, 2020 at 16:53 wharfdalewharfdale 3484 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

In WordPress, the request path, e.g. path/to/something as in example/path/to/something?query=string&if=any, is saved in WP::$request which is accessible via the global $wp variable, so in your case, you can do so to check if the page is /dashboard/products:

global $wp;
// Note: No trailing slashes.
$is_my_page = ( 'dashboard/products' === $wp->request );

// Or without the "global $wp;"
$is_my_page = ( 'dashboard/products' === $GLOBALS['wp']->request );

// To check if you're on /dashboard/products/<anything>:
global $wp;
$is_my_page = preg_match( '#^dashboard/products/#', $wp->request );

And there might be a Dokan-specific way/API/function, but you will have to find that on your own.

UPDATE

If /dashboard/products is not actually a registered WordPress rewrite rule, or that you're checking prior to WordPress parses the request URL, path, etc., then you can do "the PHP way" like so:

if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
    $path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
    $is_my_page = ( '/dashboard/products/' === $path );
}

本文标签: rewrite rulesWooCommerceConditional for page created by rewriterule