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.

First, you need to put the code below to function.php file in your active theme. After that, to hide an element or part in the post, you have to put that part between [hide] and [/hide] tags.

For example: [hide]Hidden paragraph bla bla[/hide]

The Non-logged in visitor now can’t read the text between those two tags

Here’s the code for function.php :

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

Hope this tutorial helps.

Credits:
The code is taken from WP UCanHide plugin, http://wordpress.org/extend/plugins/wp-ucanhide/, with a lithe modification.

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

Add Comments (+)

  1. saqib says:

    Hi,

    I think we should use something like

    with wordpress shortcode like..

    function caption_shortcode( $atts, $content = null ) {
    return ” . $content . ”;
    }

    add_shortcode( ‘caption’, ‘caption_shortcode’ );

    [caption]My Caption[/caption]

  2. It’s very important to hide any post or page from non logged in users. If it is done none will be able to read the whole post without logging in. Thus we can increase user of our sites.

  3. Interesting post – thanks for sharing this information I can see us using it in the future.

Trackbacks/Pingbacks

  1. Hide Parts Of Wordpress Posts Or Pages From Non-Logged Users
  2. Hướng Dẫn Ẩn Nội Dung Bài Viết Trong Wordpress
  3. 40 Best Web Design And Development Tutorials From August 2011 | stylishwebdesigner

Leave a Reply

 

Amazingly Beautiful WordPress Themes