Jun
2007
15

Hiding Pages with Wordpress part 2 - Errors, Omissions, and Solutions

by Matt

In a previous post, how to hide pages in wordpress, I talked about hiding pages in wordpress. I gave three solutions, one was wrong Embarassed and the others were a little confusing. I could have sworn that the draft and private methods worked...maybe they were bugs in older versions? I want to thank a reader from blogsandbucks.com for pointing out that the draft method for hiding pages does not work.

if you make a page, and save it as a draft, you will be able to access it but only if you are logged in. I guess made a page and didn't logout to see how the write would look from.

adding exclude='' and include='' should still work, even for WP2.2...I'm looking into it.

exclude (string)
Define a comma-separated list of Page IDs to be excluded from the list (example: 'exclude=3,7,31'). There is no default value. See the Exclude Pages from List example below.
include (string)
Only include certain Pages in the list generated by wp_list_pages. Like exclude, this parameter takes a comma-separated list of Page IDs. There is no default value.
Source: wp_list_pages function at wordpress.org 

 I also want to thank, blogsandbucks for pointing me to the code section in the post-template.php file. Since a lot of people are having trouble with hiding pages from the menu and sidebars, I wrote a little script which adds checkboxes next to the pages in the admin section so you can easily hide pages.
hide pages from wordpress in the admin section

Warning, this is geeky...you will be editing 3 files...if you are scared, shoot me an email and I'll take care of you.

You can download the altered pages here: hide_pages.zip - for wp 2.2 only...but it might work with earlier versions.

first, we have to update the blog options to store an array of our hidden pages.

open wp-admin/admin-functions.php
You have to add the checkboxes to the table.

There is a function around line 830: page_rows( $parent = 0, $level = 0, $pages = 0, $hierarchy = true )
this function lists the pages in the admin section. You need to add a table row.

add this to the '<tr>' block...you should make it the last '<td>' section

CODE:
  1. <?php $hidden_pages = get_option('hidden_pages'); ?>
  2. <td align="center"><?php echo (in_array($id, (array)$hidden_pages)) ? "<input name='page_hide[]' type='checkbox' value=$id  checked='checked' />" : "<input name='page_hide[]' type='checkbox' value=$id  />" ; ?></td>

That code will add checkboxes to your admin pages section. you are done with this file. Save and upload to your website.

now open wp-admin/edit-pages.php
You have to wrap the table that lists the pages in a form. so you are going to add form tags around line 34.

CODE:
  1. <!--add these form tags-->
  2. <form action="" method="post">
  3. <table class="widefat">
  4.   <thead>
  5.   <tr>
  6.     <th scope="col" style="text-align: center"><?php _e('ID') ?></th>
  7.     <th scope="col"><?php _e('Title') ?></th>
  8.     <th scope="col"><?php _e('Owner') ?></th>
  9.     <th scope="col"><?php _e('Updated') ?></th>
  10.     <th scope="col" colspan="3" style="text-align: center"><?php _e('Action'); ?></th>
  11.     <th scope="col" style="text-align: center"><?php _e('Hidden') ?></th>
  12.   </tr>
  13.   </thead>
  14.   <tbody id="the-list">
  15. <?php
  16. page_rows(0, 0, $posts, $all);
  17. ?>
  18.   </tbody>
  19. </table>
  20. <!--add the closing form tags-->
  21. <p class="submit"><input name="update_pages" type="submit" value="Update Visiblity" /></p>
  22. </form>

this will send an array of hidden pages as post data to get inserted into the blog options.

Then right after "require_once('admin-header.php');" on line 6 of the same page add this code:

CODE:
  1. if(isset($_POST['update_pages']))
  2. {
  3.     $hidden = $_POST['page_hide'];
  4.     update_option('hidden_pages', $hidden);
  5. }

That will do the insert. you might need to serialize the $hidden variable before you update_option. I think on newer versions of WP, update_option will serialize arrays for you. 

you are done with edit-pages.php. Save and upload to website

Now open wp-includes/post-template.php
This is the page that controls the layout visible by the public. you will find a function called wp_list_pages($args = '') around line 277. a few lines after that you will see all the arguments you can send that function, one of which is exclude=''. right after this line: $r = array_merge($defaults, $r);
add this code:

CODE:
  1. if(get_option('hidden_pages')!='')
  2.     {
  3.             $r['exclude'] = implode(",", get_option('hidden_pages'));
  4.     }

that just reads your blog options and then makes a string of page id's to exclude. 

this is licensed under the WOMC (worked on my computer), so if you have any questions feel free to shoot an email.

Tags:

Popularity: 40% [?]

Other posts you might be interested in
  • How to hide Pages in wordpress  You might run across a time in your life when you need to hide a page in wordpress.You have 3
  • Better SEO for your Website using Plugins and Tags -Part 2  Ok, so we have our tags set up from part 1 for single post pages. Now we need to make
  • Blogsites Focusing on the Community with Diverse Solutions IDX MLS Search  We've been working on a few new community blogsites with lots of moving parts. Using Wordpress to build real
  • Answers to a few common questions About wordpress  The Wordpress questions are starting to roll in. The first couple questions come from Stacey Macioszek stateofmindrealestate.com Q.
  • How to Create Full PDF Web Pages that Require no Downloading  We've been asked recently about how to add property disclosure documents and floor plans to blogs and websites. There


  • 10 Responses to “Hiding Pages with Wordpress part 2 - Errors, Omissions, and Solutions”

    1. Jason Ganz (http://www.LandBrokr.com)

      Great post! I am gonna share it with my own blog readers at jason.landbrokr.com ! Thanks.


    2. Hiding Pages with Wordpress at Chris’ Notebook (http://notes.eaglefliers.com/2007/06/28/hiding-pages-with-wordpress/)

      […] Hiding Pages with Wordpress part 2 - Errors, Omissions, and Solutions ยป Realivent Real Estate Technology Blog Since a lot of people are having trouble with hiding pages from the menu and sidebars, I wrote a little script which adds checkboxes next to the pages in the admin section so you can easily hide pages. […]


    3. taltoolio » How to hide Pages in wordpress (http://www.taltoolio.com/?p=16)

      […] Link […]


    4. Chris Sherrod (http://www.whodefinesyou.com)

      Your very good directions on your website about manually editing files to add the hide checkboxes doesn’t work for WP 2.3. Do you know how to add this functionality back. Thanks.

      Regards,

      Chris


    5. Klemens Raab

      Yes…Any idea how to do this with WP 2.3.1? Doesn’t seem to follow the same file names. Thanks for all your work!
      Klemens


    6. Another Step In hiding or excluding WordPress Pages at Top 10 Tech Web Tips (http://www.top10tech.com/web/2008/01/29/blogging-tips/another-step-in-hiding-or-excluding-wordpress-pages/)

      […] Anyway the article I found actually offered up some script that essentially seems to install a plugin or something in WordPress so that you can hide pages at a global level.  That was definitely interesting, but over kill for what I was looking for. […]


    7. My First Complaint About Wordpress 2.5 | Intelligent Website Widget Deployment Strategies (http://realespace.com/2008/03/31/my-first-complaint-about-wordpress-25/)

      […] I wrote a post and added updated wordpress files to a .zip while I was with Realivent. The post was written around Wordpress 2.2, so if you want to try it with Wordpress 2.5, good luck. It will probably still work, but I’m not guaranteeing anything. When I get some time, I’ll try it myself. […]


    8. jason (http://www.lonestarfinancing.com)

      Hello, is there a way to just exclude a page(s) in the wp_list_pages function in the post-template.php file so that if you exclude a page, it is automatically excluded anywhere in the site, i.e. archives, blog page, side bar, menu, etc…?

      Here is the function below in WP2.52:

      function wp_list_pages($args = ‘’) {
      $defaults = array(
      ‘depth’ => 0, ’show_date’ => ‘’,
      ‘date_format’ => get_option(’date_format’),
      ‘child_of’ => 0, ‘exclude’ => ‘’,
      ‘title_li’ => __(’Pages’), ‘echo’ => 1,
      ‘authors’ => ‘’, ’sort_column’ => ‘menu_order, post_title’
      );

      Thanks,
      Jason


    9. Rick (http://www.rickety.us)

      I took a different approach and decided not to hide a page but to password protect it. I wanted somewhere out of the way to place a link so I used the footer:

      To Do (Private)

      Now simply create a private page that is password protected with comments disabled. When you click on your “private” link in the footer you have to be logged in or else you will get a 404 error.


    10. Rick (http://www.rickety.us)

      I took a different approach and decided not to hide a page but to password protect it. I wanted somewhere out of the way to place a link so I used the footer:

      To Do (Private)

      Now simply create a private page that is password protected with comments disabled. When you click on your “private” link in the footer you have to be logged in or else you will get a 404 error.


    Leave a Reply

    Close
    E-mail It
    Rodney's Adsense-Deluxe Add ons plugged in.