I am creating a custom wordpress plugin.

Where for some pages like



i will append my template code from my plugin folder. Which mean i will include my custom template not even related to wordpress theme.

my code will look like this inside my wordpress plugin page.

if ( $wp->query_vars('pagename')=="clinic"){            
    include(MYPLUGINPATH . '/template/clinic.php');
}else if($wp->query_vars('pagename')=="pharmacy"){
    include(MYPLUGINPATH . '/template/clinic.php');

But $wp-query_vars('pagename') return null.is there a way to get the current loading wordpress pagename inside the plugin code.

  • Can you provide more information? For example, what is $wp? Are you using that code in some action hook? If so, which one?
    – cybmeta
    Feb 6, 2015 at 14:37

1 Answer 1


Use the $pagename global variable or pull it from the url

$slug = basename(get_permalink());

or grab the title before the loop starts:

$page_title = $wp_query->post->post_title;

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.