How to restricted a page in wordpress. For example : user [without login] can see 5 of game list. [example/game/] and after click 'view more', user must login/register and after that, user can access full/100 game list. [example/game/]
Anyone know to make it without plugin? thank you
How to restricted a page in wordpress. For example : user [without login] can see 5 of game list. [example/game/] and after click 'view more', user must login/register and after that, user can access full/100 game list. [example/game/]
Anyone know to make it without plugin? thank you
Share Improve this question edited Oct 1, 2019 at 11:39 jsmod 5013 silver badges18 bronze badges asked Jul 8, 2012 at 17:01 Juan LieJuan Lie 6534 gold badges9 silver badges12 bronze badges 3- 1 -1 – That’s pure plugin territory. – fuxia ♦ Commented Jul 8, 2012 at 18:17
- 1 why you don't want to use plugin? – Sisir Commented Jul 8, 2012 at 18:20
- Found good resource to do that : scratchcode.io/… – Mayank Dudakiya Commented Dec 20, 2020 at 10:05
3 Answers
Reset to default 4You 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]
May be a custom shortcode is useful See this plugin http://wordpress/extend/plugins/restrictedarea It is obsolete but you should use the code for your pourpose
First you need to add a custom meta box that allows you to mark the post as hidden.
For detail please click on the original answer this source link
I modify this function to fulfill your needs
add_action( 'pre_get_posts', 'yourtextdomain_pre_get_posts_hidden', 9999 );
function yourtextdomain_pre_get_posts_hidden( $query )
{
// Check if on frontend and main query.
if( ! is_admin() && $query->is_main_query() )
{
if( ! is_user_logged_in() )
{
// For the posts we want to exclude.
$exclude = array();
// Locate our posts marked as hidden.
$hidden = get_posts(array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'meta-box-checkbox',
'value' => 'true',
'compare' => '==',
),
)
));
// Create an array of hidden posts.
foreach($hidden as $hide)
{
$exclude[] = $hide->ID;
}
// Exclude the hidden posts.
$query->set('post__not_in', $exclude);
}
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745115376a4612096.html
评论列表(0条)