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!