Added Plugin to WordPress Directory

May 29th 2009

is-page-or-ancestor
I recently added an updated version of the is_page_or_sub() function now called is_page_or_ancestor() to the plugins directory over at WordPress.org. The new version of the function is a recursive test to see if the page ID or name you pass into the function is actually the current page or one of its ancestors. Basically you can test to see if you are viewing an ancestor of the About page and if so execute some code (i.e. change a pages class or id).

I use this function on my site and pretty much on every other site I’ve ever built. However, the only different is this new code is leaner and will test many levels deep. A very handy tool for the custom type themes that seem to be popping up in the wake of what I am calling a WordPress feeding frenzy. The popularity of this platform has been growing exponentially and who can blame it, I absolutely love working with WordPress.

Anyhow, this is the first plugin I have submitted to be hosted in the Directory, mostly I just wanted to test out committing a plugin to the directory and this seemed like a logical place to start. If you’re interested in what was changed here is the code. Also, you can update the old functions code with this much better code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * The parameter can contain the page ID, or page name
 *
 * @since 1.0
 * @uses $post
 * @uses $wpdb
 *
 * @param mixed $page either int or string
 * @return bool
 */
if (!function_exists('is_page_or_ancestor')) {
  function is_page_or_ancestor($page = '') 
  { 
    global $post, $wpdb;
 
    // If is not numeric get page ID
    if (!is_numeric($page)) {
      $page = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page."' AND post_type = 'page'");
    }
 
    // Recursive search through page hierarchy
    if (is_page($page) || in_array($page, $post->ancestors)) {
     return true;
    }
    return false;
  }
}

Sample Usage

1
2
3
4
5
if (is_page_or_ancestor('about')) { 
    DISPLAY SOMETHING
} else {
    DISPLAY SOMETHING DIFFERENT
}

Download File

Donations

  • Make a donation and help keep the awesomeness flowing.

Disclaimer

Valen Designs grants you a nonexclusive copyright license to use all programming code examples from which you can generate similar function tailored to your own specific needs.

Valen Designs, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.


Comments

  1. Matt Hill says:

    Cool, this looks very useful indeed, thank you for making it into a plugin :)

    Are the PHP warnings at the top of this post supposed to be there? Looks like you’re executing PHP directly in your post and it’s failing on preg_match().

    • Derek Herman says:

      I just upgraded the new syntax highlighter and now it’s doing this. Weird! It was fine when I left an hour ago. I’ll look into it.

    • Derek Herman says:

      I had to revert back to an older version 0.9.4 of the plugin wp-syntax cause the latest 0.9.6 just tried to make me look like an idiot. What a let down. thanks for the heads up. I was out getting new tires and got the email. It was a bit frustrating not being able to fix it from my iPhone LOL, but thanks.

  2. Hey Derek… So here’s my situation. I absolutely love the way that the Valen Designs Vesper theme handles the Page/Sub-page sidebar listing. However, for various reasons, I’m not able to use the Vesper theme in a project that I’m currently working on. So my question is this: is this plugin that you’ve created supposed to provide non Vesper themes with the kind of Page/Sub-page sidebar functionality included in Vesper? If so, does the plugin just automatically set things up? Or do I need to do some tweaking in order to get the functionality working?

    I’m kind of new to WP, so I’m not really sure if I have all of the WP terminology correct. Anyway, in my best *Wordpress lingo*, the gist of what I want to learn how to do do is the following.

    How can I set things up so that when I’m on any *sub-page* of *page* X, there is a list of all other *sub-pages* of *page* X listed in the side-bar of the *sub-page* that I’m on. Did that make sense? I hope so.

    For instance, here’s a link to a testsite that I’m building.

    http://www.testsite.tennberg.com/iep/

    Right now, I have two *pages* (called A and B) and one subpage for each of the two pages (Augustine as a subpage of A, and Big Bang Theory as a subpage of B). And here’s what I want to do. When someone is viewing the Augstine subpage of A (or any other subpage of A), I would like all and only subpages of A to be listed in the sidebar. I’d also need this to work for all other page / subpage relations (e.g. when I create a B page and various subpages of B, the same sidebar sorting takes place, except this time for the B subpages).

    As mentioned above, this sort of sorting functionality seems to be automatic in Vesper, but sadly I can’t use that theme for this project. Anyway, I could really use some help with this.

    Thanks…

    • Derek Herman says:

      If you go into the sidebar.php in vesper there is some code used to do that that you could copy and paste and it should work fine, you don’t really need this plugin to do that.

      There are a lot of ways to do what you’re asking and I would do it differently than the vesper theme does because I can write more complex WP code than what I wrote a year ago, but for your purposes that snippet should work. If not let me know and I’ll try and help you further.

  3. Craig says:

    I am using this in my side bar and have also enabled the search widget. When I search for something, the search page comes up with this error:

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/user/domain.com/wp-content/themes/newkubrick/functions.php on line 43

    Any idea why that might be? It occurs only in the search page.

Leave a Reply