0

I have a page created by a plugin (Dokan) called Products: example.com/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 );

1 Answer 1

1

In WordPress, the request path, e.g. path/to/something as in example.com/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 );
}
3
  • Thanks Sally, great point and informative for future too. Unfortunately I am getting nothing when on /dashboard/products, I did a var_dump of the global $wp: public 'request' => null when on the products page.
    – wharfdale
    Commented Mar 22, 2020 at 8:47
  • I found a solution (probably not ideal but works): This global has /dashboard/products/ - $GLOBALS['_ENV']['REQUEST_URI'] I am now checking against that array item.
    – wharfdale
    Commented Mar 22, 2020 at 8:57
  • Actually, did you use the wrong hook? Because at least, you should use the parse_request hook to check the $wp->request - and if you use that hook, there's no need to global $wp; since the first parameter is indeed $wp.
    – Sally CJ
    Commented Mar 22, 2020 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.