Home / functions.php / Require login to view posts in wordpress
  • How to Require login to view posts in wordpress

    no thumbnail sorry: Require login to view posts in wordpress

    Adding this PHP code to the functions.php of your wordpress theme will require users to login before viewing specific posts defined by and array of post IDs. Once the user is logged in they will be redirected to the original post.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    // add to functions.php
    function you_must_login() {
    	global $post;
    	if ( !is_single() ) // Use: !is_single() for members only posts and, Use:  !is_page() for members only pages.
    	return;
    	$post_ids = array( 188, 185, 171 ); // array of post IDs that force users to login before reading
    	if ( in_array( (int) $post->ID, $post_ids ) && !is_user_logged_in() ) {
    		auth_redirect();
    	}
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    is_single, is_page, auth_redirect, is_user_logged_in,
  • Yakup Hoca

    What about google or other search bots?