Plugin Planet

Premium WordPress Plugins

USP Pro enables you to display forms anywhere in your site. You can use shortcodes to display forms in Posts and Pages, or use template tags to display forms anywhere in your theme template. You can also display USP Forms in any widgetized areas using the USP Form Widget. These techniques provide quite a bit of flexibility as to where your forms are displayed, and you can get even more granular control using WordPress conditional tags and USP’s access-control shortcodes.

There are two ways to display content conditionally: shortcodes and template tags. This tutorial explains how to do both. First, we’ll cover shortcodes, and then look at template tags.

Conditional Content via Shortcodes

To make it easy to display conditional content, USP Pro provides a set of access-control shortcodes. These shortcodes enable you to display forms and other content conditionally, based on whether or not the user is logged in to your site. Here are the access shortcodes that can be used to display conditional content:

  • [usp_access cap="read"][/usp_access] – Display content based on user role
  • [usp_visitor][/usp_visitor] – Display content only to visitors (not logged in)
  • [usp_member][/usp_member] – Display content only to logged in users

To see how these shortcodes work, let’s look at an example that shows how to display any USP Pro form to logged-in/registered users, and then display alternate content to visitors (users who are not logged in).

Pro Tip: any [usp_form] that you want to display via shortcode must be published (not draft or pending).

Step 1

Once you have a form built and published, you can display it to logged-in users by including the following shortcodes on any Post or Page:

[usp_member]
[usp_form id="submit-article"]
[/usp_member]

Change the form id, submit-article to match that of your form.

Step 2

Then to add content that only visitors (not logged in) will see, include the following shortcodes on the same Post or Page:

[usp_visitor]
[usp_login_form]
[/usp_visitor]

Here we are using the [usp_login_form] shortcode to display a login/register form to visitors. Of course, this is just an example — you can display anything that is required to logged and/or not-logged guests, including markup (see Notes below for more info).

Notes

Each of the access-control shortcodes accepts a deny attribute, for example:

[usp_access cap="read" deny="You do not have access!"][/usp_access]

So you can display a custom message to any user that does meet the criteria. And you can include markup in the deny message, by using curly quotes, for example:

[usp_access cap="read" deny="{h1}You do not have access!{/h1}"][/usp_access]

Such that {tag} will display as <tag> in the deny message. Also note that you can include basic markup in between the opening and closing access shortcodes, for example:

[usp_visitor]
<h1>Hello there!</h1>
<p>Please log in to post content.</p>
[usp_login_form]
[/usp_visitor]

So you can structure your conditional content however is required.

Conditional Content via Template Tags

Conditional template tags include tags such as is_single() and is_home(). There are many conditional tags listed in the Codex. For example, you can use is_home() to display the form (or any content) only when the visitor is viewing the home page:

<?php if (is_home()) {
	
	display_usp_form(123, false);
	
} ?>

This will check to see if the current page is the home page, and if so use display_usp_form() to display the specified USP Form (“123” in this example). Now let’s use conditional tags to display the form only if the current visitor is logged in to WordPress.

<?php if (is_user_logged_in()) {
	
	display_usp_form(123, false);
	
} else {
	
	// not logged in
	
} ?>

For more USP template tags, check out the Template Tag Reference.

Related