How to Hide a Part of WordPress Post or Page from Non-logged in Visitor
31st Jul 2011 | Posted by reza | 9 CommentsWordPress 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 (+)-
-
few missing lines in above comment
if ( is_user_logged_in() ) {
//
} else {
//
}
-
-
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.
-
Interesting post – thanks for sharing this information I can see us using it in the future.
Trackbacks/Pingbacks
- Hide Parts Of Wordpress Posts Or Pages From Non-Logged Users
- Hướng Dẫn Ẩn Nội Dung Bài Viết Trong Wordpress
- 40 Best Web Design And Development Tutorials From August 2011 | stylishwebdesigner
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]