ButlerBlog

chad butler's weblog

  • About
  • Blog
  • WordPress Plugins
  • Contact
Home / Web / WordPress / WP-Members / Blocking content in a custom template

Blocking content in a custom template

By Chad Butler 2 Comments

Now that you have the key concepts, we can put together what we need to do in the template.

First, we will set up a test to see if the user is logged in, and then display things differently based on the user’s login state.

<?php if( is_user_logged_in() ) {

	// the user is logged in
	// display appropriate content

} else {

	// the user is not logged in
	// display the user login and registration

} ?>

If the user is not logged in, we want to display the WP-Members login form and registration form and handle the results accordingly.  Some things to note about this: First, it is a good idea to test for the existence of the wpmem_securify function to see if the plugin is turned on.  If it’s not, and you don’t want the page to break, you need a condition for that.  Second, since we are out of the Loop, you will need to display certain things that are created by the Loop, but are not put here normally.  In this example, I am coding in a display of the page title.  In a real world situation, you may find that you need to call more than this.  The possibilities are outside the scope of this example, so I would suggest learning more about WordPress functions.

<?php get_header(); ?>

<?php if( is_user_logged_in() ) { ?>

	Replace this with your wp_query
	or whatever it is you are displaying
	as content. It will only display to
	users that are logged in.

<?php } else {

	// the user is not logged in
	// display the user login and registration
	if (function_exists('wpmem_securify'))
    {
        // display loging prompt as on other pages
        echo '<h1 class="entry-title">' . get_the_title() . "</h1>";
        wpmem_securify();
        the_content();
    }

} ?>

So now, if the user is logged in, and WP-Members is activate, the title will be output, wpmem_securify will load the forms into the $content variable, and the WP function the_content() will echo the contents of $content.  (In plainspeak – this will output the same forms that are on other protected pages.)

To tie this all together, a simple template might lookin something like this:

<?php get_header(); ?>

<?php if( is_user_logged_in() ) { ?>

	Replace this with your wp_query
	or whatever it is you are displaying
	as content. It will only display to
	users that are logged in.

<?php } else {

	// the user is not logged in
	// display the user login and registration
	if (function_exists('wpmem_securify'))
    {
        // display loging prompt as on other pages
        echo '<h1 class="entry-title">' . get_the_title() . "</h1>";
        wpmem_securify();
        the_content();
    }

} ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

This is a simplified example at this point.  Depending on your theme, you may need to add to this for completely integrated display.  Review the existing page templates in your theme accordingly.

This is a simplified example at this point. Depending on your theme and template structure, you

Enjoyed this article?

Don't miss a single post. Subscribe to our RSS feed!

  • Facebook
  • Twitter
  • Email
  • Print
  • More
  • LinkedIn
  • Reddit
  • Tumblr
  • Pocket
  • Pinterest
Pages: 1 2

Filed Under: WP-Members Tagged With: development, plugins, tips, WP-Members

About Chad Butler

Chad Butler is a freelance writer and web developer. He has developed several popular WordPress plugins and has written for forbes.com, sfomag.com, and investopedia.com. He also runs a small organic farm in east Georgia.

Join Us!

I will never share your information. No spam. No junk. No kidding. Unsubscribe anytime.

Recent Posts:

  • YouTube Success: Key Tips for Enhancing Video Optimization and Visibility
  • Mobile App vs. Mobile Website Ideal Choice for your Business
  • Top Strategies to Boost Your Brand’s Visibility and Impact
  • Advanced Blogging Strategies: Using Analytics, A/B Testing, and Conversion Optimization Techniques to Grow Your Audience
  • Unlock Real-Time Process Insights to Save Time and Money
  • How Writers Can Attract More Audience Attention
  • Dress for Success – Even at Home
  • Mastering the Art of Crafting SMART Marketing Goals
  • Rediscover Your Brand Story: 7 Tips for Refreshing Your Company Identity
  • Creating Engaging Content: Tips for a Successful Content Marketing Strategy

Archives

  • About
  • Blog
  • Archive
  • Contact

Site powered by WordPress, running on the Genesis Framework from StudioPress.

Unless otherwise noted, content on this site is © 2006-2025 ButlerBlog and may not be reproduced without express written permission from the author.

Some content may include affiliate links for which this site receives a small commission.