Quick Tip #3 – Writing your own ‘is_page’ function similar to the one in WordPress

Posted on

After building several WordPress sites lately, I've been getting used to writing it's common, but useful functions to do various different things. I've now come to write new sites that don't use WordPress and I miss those functions! So I've created my own version of the is_page function among others. View the is_page function in the WordPress Codex.

The is_page function is great for non WordPress sites that use lots of includes, such as menus. Say if you have two different includes that are included into different types of pages, you can put them in one include and use this conditional function to detect the page.

Defining the page

First I set the page name in a value called $page and echo it into the title of the document. It's not necessary to echo it but it makes sense to.


Writing the 'is_page' function

Now in my index.php file I'll include a functions.php file at the top of the page before the doctype.


Next place the is_page function into inc/functions.php. First we define the function name with a value called $current_page that we can set when writing the function. Then set the $page value to global so that it can be read from outside the function. All that's left is a simple if statement that checks whether our page name entered into the function's brackets matches our title we set at the top of our index.php page in $title. If $page and $current_page match, it will return true if not, it will return false.


Calling the 'is_page' function on our page

Back on the index.php add call the function and enter the matching page name.

Now a live demo wouldn't be much help unless you could see the un-generated code, so download the files and give it a blast. Let me know you thoughts/improvements or if you use it, below.


  • Liam Goodacre wrote on

    Cool, nice tip :)
    used something similar to this awhile back, where I used a ‘select’ to determine which page content to include, in relation to what the current page title was.

    –Liam Goodacre

  • Pingback: WordPress Tips From Twitter, Digest #1 | Weblog Tools Collection

  • JD Hartley wrote on

    Looks great!

    You could even simplify the title tag

    To this:

    Thanks for the code, Benjamin.


  • Xs wrote on

    function is_page($current_page, $page, $title){

    if($current_page == $page)
    echo $title;


    This function could save you a bit more of time ;)

  • Marc wrote on

    Hey Benjamin, thanks for this helpful little post. It’s exactly what I was looking for. I do have a problem though and I was wandering if you’d help me solve it?

    I pull my header[dot]php with the PHP function requier into all my pages. Now all of my pages are having the same name.

    I’m not good enough to find a work around myself. Could you help me with a quick fix? Many thanks Marc

    • Nouveller wrote on

      Instead of defining $page in the page title just add it above you’re header.php include, that should sort it out. If you’re still having problems, send me a message using my contact form.

  • Marc wrote on

    Thanks Benjamin, I just moved the entire title tag including you PHP snippet from above onto my page befor my require (include) header. It works perfect!

    • Nouveller wrote on

      Great! Glad you got it sorted.

  • Pingback: baby möbel shop

  • HostGatorForFree - Coupon for HostGator wrote on

    It’s actually a great and useful piece of information. I’m satisfied that you shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

  • build your own software wrote on

    Great blog here! Also your website so much up very fast! What web host are you using? Can I get your associate link in your host? I want my website loaded up as quickly as yours lol

  • sameed wrote on

    I need to have one css file for home page and another one for all other pages. It is simply PHP files not using any CMS.

    Can you help me please

  • sameed wrote on

    thanks a lot for your very useful code for the php novice like me…

    It works as follow, if any one needed.

    I called the following in my header.php

  • sameed wrote on

  • Pingback: Find Tablet Computer Discounts

  • game zuma wrote on

    It’s actually a great and useful piece of information. I’m satisfied that you shared this useful info with us. Please keep us up to date like this. Thank you for sharing.

  • a wrote on

    Eat a balanced and healthy meal. viagra http://www.totalrock.hu

  • Louie wrote on

    Very useful thank you.

    To save some lines, you could also replace the “if ($page == $current)” statement with a nice ternary operator:

    return ($page == $current) ? true : false;

Add your comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>