Support » Plugins » Hacks » Showing private pages in default page-menu

  • Resolved frha

    (@frha)


    I was struggeling to be able to show private pages in the default page-menu when user was logged in.

    Finally i was able to do this in a child-theme, but it should have been a standard way to do this regardless of the theme chosen…

    I would suggest that the core-developer just adds a few lines in includes\post.php like this :

    // Make sure we have a valid post status.
    	if ( ! is_array( $post_status ) ) {
    		$post_status = explode( ',', $post_status );
    	}
      //Added to show private pages if user can view them
      if(current_user_can('read_private_pages')) {
        $post_status[] = 'private';
      }

    But since it’s a bad idea to change those in my own installation, I can add this through the filter-mechanism like this (in my child-theme’s functions.php) :

    function my_page_menu_args( $args ) {
    
      if(current_user_can('read_private_pages')) {
        $args['post_status'] = 'publish,private';
      }
    	return $args;
    }
    add_filter( 'wp_page_menu_args', 'my_page_menu_args' );
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes! That is exactly why filters are provided.

    The core team has a tough job. They have one package that needs to meet the needs of the widest audience possible. Many things come down to a choice of doing it one way or another. Offering both is impractical. Sure a choice can be offered in the settings, giving more flexibility. But there are a LOT of users where a plethora of options is confusing and intimidating. The core team is actually leaning towards removing options to the extent possible. In their place are plenty of filters for those with advanced skills to be able to change things as they wish.

    I agree that filter could be the way to go for more of the options, but then there should be a way to apply filters without having to create child-themes. I don’t want to have to redo this work for every theme I want to make available in my network.

    In this explicit case, I also had to inlcude the default-value for the method, since the filter is applied before the default-value is applied…

    I haven’t tried to create a plugin yet – could this filter have been applied just as easy by installing it in a plugin ?

    Moderator bcworkz

    (@bcworkz)

    That’s right. If your code has no need for theme related things like custom templates (there’s actually a plugin workaround for custom templates anyway), then plugins are the way to go specifically because they are not tied to a theme. Creating a basic plugin is just as easy as creating a child theme.

    I always encourage all WP hackers to have a custom site specific plugin to contain all their little tweaks they inevitably come up with.

    Thx.
    I found that creating a basic plugin was actually easier than creating a child-theme 🙂
    I only had to create this one php-file, and put it in my plugins-directory. Then i was able to enable it for my network.

    So for now, since I believe this is the appropiate way to ‘hide/show’ private pages, I’ll leave it enabled for all sites in the network.

    I’ll have to dig deeper in order to find the right method for enabling private pages to be selectable when creating custom menues 🙂

    save this as ‘view_private.php’ in the plugin-directory…

    <?php
    /**
     * @package View_Private
     * @version 1.0
     */
    /*
    Plugin Name: View Private
    Description: This plugin allows for viewing of private pages in the default page-menu
    Version: 1.0
    */
    
    function my_page_menu_args( $args ) {
    
      if(current_user_can('read_private_pages')) {
        $args['post_status'] = 'publish,private';
      }
    	return $args;
    }
    
    add_filter( 'wp_page_menu_args', 'my_page_menu_args' );
    
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Showing private pages in default page-menu’ is closed to new replies.