WordPress.org

Ready to get started?Download WordPress

Forums

Function to tell if a page password protected (12 posts)

  1. melbell
    Member
    Posted 8 years ago #

    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.

  2. Kafkaesqui

    Posted 8 years ago #

    No built-in function I'm aware of, so you might use this:

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

    etc.

  3. melbell
    Member
    Posted 8 years ago #

    oooh...that looks good! :)

    Thanks! I will go try it now!

  4. melbell
    Member
    Posted 8 years ago #

    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.

  5. Kafkaesqui

    Posted 8 years ago #

    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.

  6. melbell
    Member
    Posted 8 years ago #

    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.

  7. Kafkaesqui

    Posted 8 years ago #

    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.

  8. melbell
    Member
    Posted 8 years ago #

    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?

  9. Kafkaesqui

    Posted 8 years ago #

    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.

  10. melbell
    Member
    Posted 8 years ago #

    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?

  11. melbell
    Member
    Posted 8 years ago #

    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.

  12. Kafkaesqui

    Posted 8 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic