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
1 Answer
Reset to default 1In 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
版权声明:本文标题:rewrite rules - WooCommerce - Conditional for page created by rewrite_rule 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650630a2617661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论