Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to restricted a page in wordpress. For example : user [without login] can see 5 of game list. [example.com/game/] and after click 'view more', user must login/register and after that, user can access full/100 game list. [example.com/game/]

Anyone know to make it without plugin? thank you

share|improve this question
1  
-1 – That’s pure plugin territory. –  toscho Jul 8 '12 at 18:17
1  
why you don't want to use plugin? –  Sisir Jul 8 '12 at 18:20

2 Answers 2

You can do this pretty easily with a shortcode. Hook into init and add the shortcode in your hooked function.

<?php
add_action('init', 'wpse57819_add_shortcode');
/**
 * Adds the shortcode
 *
 * @uses add_shortcode
 * @return null
 */
function wpse57819_add_shortcode()
{
    add_shortcode('restricted', 'wpse57819_shortcode_cb');
}

Then in your callback function, you can check to see if the user is logged in. If they are, show them the content. If not, show them a login message. You can do literally whatever you want here: check for user capabilities to show them the content (different "membership levels"), show them an entire login form. A simple example:

<?php
/**
 * Callback function for the shortcode.  Checks if a user is logged in.  If they
 * are, display the content.  If not, show them a link to the login form.
 *
 * @return string
 */
function wpse57819_shortcode_cb($args, $content=null)
{
    // if the user is logged in just show them the content.  You could check
    // rolls and capabilities here if you wanted as well
    if(is_user_logged_in())
        return $content;

    // If we're here, they aren't logged in, show them a message
    $defaults = array(
        // message show to non-logged in users
        'msg'    => __('You must login to see this content.', 'wpse57819'),
        // Login page link
        'link'   => site_url('wp-login.php'),
        // login link anchor text
        'anchor' => __('Login.', 'wpse57819')
    );
    $args = wp_parse_args($args, $defaults);

    $msg = sprintf(
        '<aside class="login-warning">%s <a href="%s">%s</a></aside>',
        esc_html($args['msg']),
        esc_url($args['link']),
        esc_html($args['anchor'])
    );

    return $msg;
}

As a plugin.

Usage

Somewhere in your pages/posts:

[restricted]
Content for members only goes here
[/restricted]
share|improve this answer
    
You can drop the code above in your functions.php file (minus the opening <?php tags). Example usage added to the answer. –  chrisguitarguy Jul 9 '12 at 12:43
    
It's not in php code, the [restricted] is a shortcode: codex.wordpress.org/Shortcode_API –  chrisguitarguy Jul 9 '12 at 13:55
    
@Davis: Sorry, but not working in my syntax. [restricted] <a href="sample.com">Sample</a> [/restricted] Whats wrong? thx –  Juan Lie Jul 9 '12 at 14:10
    
You're logged in. That's probably what's wrong. –  chrisguitarguy Jul 9 '12 at 15:17
    
@Davis: Sorry, but coud this code put in any php file not in post. But in my case it didn't <? php code [restricted] php code [/restricted] php code ?> Thank you –  Juan Lie Jul 10 '12 at 13:41

May be a custom shortcode is useful See this plugin http://wordpress.org/extend/plugins/restrictedarea It is obsolete but you should use the code for your pourpose

share|improve this answer

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.