I want to know what is the logic of the code that they use for this type of restriction and how can I do it on my WordPress website.
CodePudding user response:
Medium is not restricting viewers to view posts right, they only restrict people from clapping or commenting on a particular post.
Still, you can use is_user_logged_in condition to find if the user has logged in, if not you can ask them to either login or create an account.
CodePudding user response:
This is just a simple example of how you could do this.
Method A:
You need to update the excerpt of each article.
Edit single.php
:
while ( have_posts() ) {
the_post();
if ( is_user_logged_in() ) :
// Also you can include the template part here.
the_content();
else :
// Show just the article excerpt.
the_excerpt();
endif;
}
Method B
Here you need to edit articles and add Read More tag (Shift Alt T)
in the place where you want to restrict the article visibility.
Edit single.php
:
while ( have_posts() ) {
the_post();
// Show part of article for visitors.
if ( ! is_user_logged_in() ) :
global $more;
$more = 0;
endif;
the_content();
}
Edit functions.php
:
/**
* Change read more link.
*
* @return mixed Sign up page link.
*/
function modify_read_more_link() {
$url = wp_login_url(); // Your sign up page url goes here.
$title = __( 'Login or Sign up to view the rest of the article.', 'text-domain' );
return sprintf( '<a href="%s" title="%s">%s</a>',
esc_url( $url ),
esc_attr( $title ),
$title
);
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );