Plugin Planet

Premium WordPress Plugins

USP Pro includes a plethroa of shortcodes to use for displaying forms, submitted content, and more. In this post, we will look at some examples of using USP Pro “access-control” shortcodes to display forms conditionally, based on whether or not the user is logged in to your WordPress-powered site.

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

Display form based on user capability

Here we are using nested shortcodes to display the form only to users who have the ability to “read”, as specified by the cap attribute:

[usp_access cap="read" deny="You must be able to read to submit content!"]
[usp_form id="submit"]
[/usp_access]

Here, the deny attribute is the message that will be displayed to visitors who do not meet the requirements. The cap attribute specifies the capability that is required in order for the user to view the content. To specify multiple capabilities, separate each with a comma. Check out the WP Codex for a complete guide to user roles.

Display form only if user is logged in

Here we are displaying the form only if the current user is logged in:

[usp_member deny="You must be logged in to submit content!"]
[usp_form id="submit"]
[/usp_member]

As before, the deny attribute contains the message that will be displayed to users who do not meet the requirements.

Display form only if the user is not logged in

Lastly, here is a nested shortcode combo to display the form only for visitors (users who are not logged in to the site):

[usp_visitor deny="Please log out to view this content!"]
[usp_form id="submit"]
[/usp_visitor]

As before, the deny attribute contains the message that will be displayed to users who do not meet the requirements.

For more details about these and other USP Pro shortcodes, visit the USP Pro Shortcode Reference.

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.

Related