WordPress.org

Support

Support » Plugins and Hacks » Function to tell if a page password protected

Function to tell if a page password protected

  • Is there any built in WP function that says like is_password_protected() on a page that will return TRUE if a page is password protected or something? I will like to use something like this to exclude password protected pages from the sidebar links.

Viewing 11 replies - 1 through 11 (of 11 total)
  • No built-in function I’m aware of, so you might use this:

    <?php if(!empty($post->post_password)) { ?>

    etc.

    oooh…that looks good! 🙂

    Thanks! I will go try it now!

    Ok, I tried that. Here’s how I used it:

    <?php if (!empty($post->post_password)) { ?>
    <?php wp_list_pages('title_li'); ?></ul></li>
    <?php } ?>

    It doesn’t list any of the pages.

    Um, it’s not going to work like that.

    The expression would need to operate on Page data individually, so you’d need to edit the source of wp_list_pages() and incorporate it at a point before each Page link is generated. Question is, do you want password-protected Pages to be linked, or not? The line as provided, if it had worked, would have returned true for password-protected Pages.

    Anyway, I’ll take a look to see where the change you want is required.

    Yeah, after I thought about for a while I realized that I would have to edit the wp_list_pages() function itself to get my desired results…I can’t seem to find which file that function is included in.

    Let me know if you find where it is.

    A simple change to the SQL query that calls the Pages from the posts table will do it. In template-functions-post.php (wp-includes/ directory), locate the get_pages() function (around line 274). Within it you’ll find the $pages variable assignment. It’ll look something like this:

    $pages = $wpdb->get_results("SELECT * " .
    "FROM $wpdb->posts " .
    "WHERE post_status = 'static' " .
    "$exclusions " .
    "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);

    To exclude Pages with passwords, change it to:

    $pages = $wpdb->get_results("SELECT * " .
    "FROM $wpdb->posts " .
    "WHERE post_status = 'static' " .
    "AND post_password = '' " .
    "$exclusions " .
    "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);

    To exclude Pages without passwords (!), alter the new AND line to:

    "AND post_password <> '' ".

    My regular code hacking footer: Back up source files before editing them, comment your changes for future reference, etc.

    I was actually hoping that I could copy the wp_list_pages() function and create a new one based on that so that I don’t have to make any permanent changes to the functions and processes that come with wordpress. Do you happen to know where this function is located?

    It’s all in template-functions-post.php . Problem is the functionality behind wp_list_pages() is broken into several different functions. However, if you copy wp_list_pages() and get_pages(), rename them, and have your new version of wp_list_pages() call your new version of get_pages() — look for this line:

    $pages = & get_pages($args);

    that should work for you.

    Sorry to be such a pain. But I’m going in circles here.

    Ok, I copied wp_list_pages() and named it wp_list_pages_nopassword(). Did the same with get_pages() calling it get_pages_nopassword().

    Now I have this:

    <?php if (!empty($post->post_password)) { ?>
    <?php } ?>

    But where do I need to put that if statement?

    Here’s what I did. I have very limited MYSQL knowledge so please let me know if this might break anything.

    In my new get_pages_nopassword(), where it says:

    "FROM $wpdb->posts " .
    "WHERE post_status = 'static' " .
    "$exclusions " .
    "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);

    And changed it to:

    "FROM $wpdb->posts " .
    "WHERE post_status = 'static' " .
    "AND post_password = '' " .
    "$exclusions " .
    "ORDER BY " . $r['sort_column'] . " " . $r['sort_order']);

    It seems to work, I just want to make sure it won’t break anything.

    Looks good to me melbell. As you figured out, no need of the if statement to remove any Pages with passwords, as the additional SQL bit already does it for you.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Function to tell if a page password protected’ is closed to new replies.
Skip to toolbar