WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to know how to make a page template in WordPress that can only be accessed by admins. How could I apply it to the following template?

<?php /*
Template Name: Agency Area
*/
?>
<?php get_header(); ?>

<div id="body">
    <div class="agency_area_menu">
        <?php wp_nav_menu(); ?>
    </div>
</div>

<?php get_footer(); ?>

I'd prefer to do it without plugins.

share|improve this question
up vote 3 down vote accepted
<?php
/*
Template Name: Agency Area
*/
get_header();

echo '<div id="body">';

    global $current_user;

    if( in_array( 'administrator', $current_user->roles ) ) {
        echo '<div class="agency_area_menu">';
            wp_nav_menu();
        echo '</div>';
    } else {
        // echo '<p>You do not have the rights required to view this page. Sorry.</p>';
        /* or with internationalization
           (uncomment either, adjust text domain if applicable) */
        echo '<p>' .
            __(
                'You do not have the rights required to view this page. Sorry.',
                'theme-text-domain'
            ) .
            '</p>';
    }

echo '</div>';

get_footer();
?>
share|improve this answer
    
thank you for your answer but i need a log in form to appear once he opens the page if he logged in successfully the page would appear if not 'else' and thanks again – Faisal Abu Jabal Nov 17 '12 at 21:11
    
Adjust the content of the conditional accordingly... – Johannes Pille Nov 17 '12 at 21:17
    
it worked i added wp_login_form after the else function – Faisal Abu Jabal Nov 17 '12 at 21:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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