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 things like user role or capability, and whether or not the user is logged in to your WordPress-powered site.
[usp_form]
that you want to display via shortcode must be published (not draft or pending).Display form based on user role or 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 or role that is required in order for the user to view the content. To specify multiple roles/capabilities, separate each with a comma (see example below). Check out WordPress.org for a complete guide to user roles. Here are some more examples to give you a better idea:
// display content only to users with Contributor role
[usp_access cap="contributor"]Put content here[/usp_access]
// display content only to users with Contributor or Administrator role
[usp_access cap="contributor, administrator"]Put content here[/usp_access]
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. Make sure to use single quotes inside of the curly brackets. Otherwise the shortcode will break.
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.
Examples
Here are some real-world examples showing some different form configurations that are possible using Access Control Shortcodes.
Example 1: Submit post and maybe register user
Say we want to display a form for users to submit posts and register with the site. So if the user is logged in, only the post-submit fields are displayed. If the user is not logged in, then a few additional fields are added to the post-submit form, to enable the user to register with the site while submitting a post. This configuration can be achieved using Access Control shortcodes within any USP Form. Here is a basic example showing the required fields:
[usp_name]
[usp_title]
[usp_content]
<input name="usp-is-post-submit" value="1" type="hidden">
[usp_visitor]
[usp_email]
[usp_files multiple="true" required="true"]
<input name="usp-is-register" value="1" type="hidden">
[/usp_visitor]
That can be used for any USP Form, added to the content area just like you see in all the demo forms. The first part of the code displays the fields required to display a post-submit form. Then the second part of the code displays the conditional registration fields. If the user is a visitor and not logged in, the three fields inside of the [usp_visitor]
shortcode are displayed. Basically the above code is a conditional variation of a post-submit/register-user combo form.
Example 2: Submit post OR register user if not logged in
Say we want to display a submit-post form only to logged-in users. And for users who are not logged in, display a registration form. There are numerous ways to do this, but the easiest is to:
- Create a new WP Page where you want to display the form
- Make a post-submit form or use the demo form
- Make a user-registration form or use the demo form
- Add the following code to the page you made in Step 1:
[usp_member]
<h3>Submit a post!</h3>
[usp_form id="submit"] // replace with your form shortcode
[/usp_member]
[usp_visitor]
<h3>Register with the site!</h3>
[usp_form id="register"] // replace with your form shortcode
[/usp_visitor]
The first part of the code displays the post-submit form to logged-in users, and the second part displays the registration form to non-logged-in users. Important: for this technique to work, both of the forms must exist and have published status, and the id
attribute in each of the [usp_form]
shortcodes must be correct (i.e., use the correct shortcodes to display the forms that you want to display).
The above examples are very basic, but can be used to make a USP Form that will get you started. Try a few test posts while logged in and out, to see how it works. Then you can customize as needed by adding other fields, using shortcode attributes to change the field labels and placeholders, and so forth.