How to Hide a Part of WordPress Post or Page from Non-logged in Visitor

WordPress provide private feature to make your post or page only visible to other editors or site admin. Once a post is set to private, regular visitor won’t be able to read the post. Now, what a about if We want to hide only a part of the post? This code snippet will show You how to do it. Read More: http://www.dynamicwp.net/articles-and-tutorials/hide-a-part-of-wordpress-post-or-page/

PHP

put the code to function.php file of active theme

and to hide a part of the post, put it between [hide] and [/hide]

function Wp_UCanHide($text) {

	global $user_ID;

        if ($user_ID == '') {

	$posdebut = strpos($text, '[hide]');
	$posfin = strpos($text, '[/hide]');
	$posfin = $posfin + 5;

        $texttohide = substr($text,$posdebut,$posfin);
        $text = str_replace($texttohide, "", $text);        

        return $text;

        }else{

        $text = str_replace('[hide]', "", $text);
        $text = str_replace('[/hide]', "", $text);

        return $text;

        }
}

// Apply the filters, to get things going
add_filter('the_content', 'Wp_UCanHide');

Share the love

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!

Advertisement