0

Currently, to add a single page that should not be part of the CMS, I add a new Page entry in the WP admin panel and then markup the page in page-{page-slug}.php. This however causes the Pages list to contain pages that are not meant to be edited by the client/editor (title, content are irrelevant).

How should I create these custom PHP pages without using the Pages system?

  • The slug should still work and not be overwritable by new pages
  • A php file should be linked dynamically like page-{slug}.php or /inc/{slug}.php
1

2 Answers 2

0

Such an interesting question, I will be keen to see the other responses.

I am sure their is a more elegant solution but I would just hook into the wp admin css and hide those pages from the users view like this.

    // Hook into the admin head
    add_action('admin_head', 'hidepages_css');

    // Add our custom CSS to the admin pages
    function hidepages_css() { ?>

    <style>

/*Each page entry is within a table (table has a class of .pages) and has a unique ID, use your browsers inspect element to find the ID of the pages you want hidden from users view */

    .pages #post-5, .pages #post-9,  {
        display: none !important;
    }

    </style>

    <?php } ?>

You can also set it so that only you can see the pages as an admin but your users with lower roles cant.

// Check if the user is an admin
    if ( ! current_user_can( 'manage_options' ) ) {
        // Hook into the admin head
    add_action('admin_head', 'hidepages_css');

    // Add our custom CSS to the admin pages
    function hidepages_css() { ?>

    <style>

    .pages #post-5, .pages #post-9,  {
        display: none !important;
    }

    </style>

    <?php } ?>
    } 
2
  • Interesting suggestion, but a bit too hack-y and unmaintainable for my taste.
    – Berend
    May 20, 2014 at 7:02
  • Haha fair enough, its super hack-y I agree ;-)
    – Matt Royal
    May 20, 2014 at 8:03
0

Same pages as answer above "5 and 9" can also be hidden/removed from backend with PHP:

function hidepages_php( $query ) {
        if( !is_admin() )
                return $query;
        global $pagenow;
        if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) )
                $query->set( 'post__not_in', array(5,9) ); //ids of posts you dont want visible in backend 
        return $query;
}
add_action( 'pre_get_posts' ,'hidepages_php' );

OR even do the same thing for specific users only. Here is an example that hids from everyone except YOU specifically or a specific user:

function hidepages_php( $query ) {
        if( !is_admin() )
                return $query;
        global $current_user;
        get_currentuserinfo();
   if($current_user->user_login != 'yourusername') {
        global $pagenow;
        if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) )
                $query->set( 'post__not_in', array(5,9) ); //ids of posts you dont want visible in backend 
        return $query;
    }
}
add_action( 'pre_get_posts' ,'hidepages_php' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.