Back WordPress

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

WRITTEN BY ON 31 Jul 2011
14,337 VIEWS • SHARES
1 comment

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');

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

1 comment
Danny 13 years ago
This works fine with the exception of using wptouch (mobile device plugin). Mobile devices display everything regardless.
Reply