User Submitted Posts Shortcodes

Below, you’ll find a detailed guide on how to add the User Submitted Posts Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the User Submitted Posts Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the User Submitted Posts Plugin and the shortcodes it provides:

Plugin Icon
User Submitted Posts – Enable Users to Submit Posts from the Front End

"User Submitted Posts is a WordPress plugin enabling users to create posts directly from the frontend. This simplifies content submission, boosting user engagement and interactivity."

★★★★☆ (886) Active Installs: 20000+ Tested with: 6.3.2 PHP Version: 5.6.20
Included Shortcodes:
  • [usp_access]
  • [usp_visitor]
  • [usp_member]
  • [usp-login-form]
  • [usp-reset-button]
  • [usp_display_posts]
  • [usp_gallery]

User Submitted Posts [usp_access] Shortcode

The User Submitted Posts (USP) shortcode, ‘usp_access’, is a versatile tool. It checks the user’s capabilities, and if they match the specified ‘cap’, it returns the content. If the user doesn’t have the required capability, it returns the ‘deny’ message. This shortcode is a useful tool for controlling access to content based on user roles.

Shortcode: [usp_access]

Parameters

Here is a list of all possible usp_access shortcode parameters and attributes:

  • cap – Defines user capabilities, default is ‘read’
  • deny – Message displayed when access is denied

Examples and Usage

Basic example – The shortcode allows a user with the capability to read to access specific content. If the user does not have this capability, they will see a denied message.

[usp_access cap="read" deny="Access Denied."]Your content here[/usp_access]

Advanced examples

1. The shortcode is used to allow users with either read or edit_posts capability to access the content. If the user does not have these capabilities, they will see a custom denied message. The capabilities are separated by a comma.

[usp_access cap="read,edit_posts" deny="You do not have the right capabilities."]Your content here[/usp_access]

2. The shortcode is used to allow users with the capability to publish_posts to access the content. If the user does not have this capability, they will see a default denied message.

[usp_access cap="publish_posts"]Your content here[/usp_access]

3. The shortcode is used without any capabilities. This means that all users, regardless of their capabilities, can access the content. If for some reason the content is null or the shortcode is being used in a feed, the users will see the denied message.

[usp_access deny="Content not available."]Your content here[/usp_access]

PHP Function Code

In case you have difficulties debugging what causing issues with [usp_access] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp_access', 'usp_access');

Shortcode PHP function:

function usp_access($attr, $content = null) {
	extract(shortcode_atts(array(
		'cap'  => 'read',
		'deny' => '',
	), $attr));
	
	$deny = str_replace("{", "<", $deny);
	$deny = str_replace("}", ">", $deny);
	
	$deny    = htmlspecialchars($deny, ENT_QUOTES);
	$content = htmlspecialchars($content, ENT_QUOTES);
	
	$caps = array_map('trim', explode(',', $cap));
	
	foreach ($caps as $c) {
		if (current_user_can($c) && !is_null($content) && !is_feed()) return do_shortcode($content);
	}
	
	return $deny;
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-access.php

User Submitted Posts [usp_visitor] Shortcode

The User Submitted Posts (USP) shortcode ‘usp_visitor’ is designed to control content visibility. This shortcode checks if a user is logged in or not. If the user isn’t logged in or if the content is a feed, it returns the specified content. If not, it returns a ‘deny’ message. The ‘deny’ attribute allows you to customize the message displayed to denied users.

Shortcode: [usp_visitor]

Parameters

Here is a list of all possible usp_visitor shortcode parameters and attributes:

  • deny – A message displayed to users who are not logged in or viewing from a feed.
  • content – The content that is shown to logged-in users or those not viewing from a feed.

Examples and Usage

Basic example – The basic usage of the ‘usp_visitor’ shortcode without any parameters.

[usp_visitor /]

Advanced examples

Using the shortcode to display a specific message to non-logged in users or visitors. The shortcode will check if the user is logged in or not. If the user is not logged in, it will display the content within the shortcode. If the user is logged in, it will display the ‘deny’ message.

[usp_visitor deny="<p>You are logged in. This content is not available for you.</p>"]Content for non-logged in users goes here.[/usp_visitor]

Using the shortcode to display content in the RSS feed. The shortcode will check if the page is an RSS feed or not. If it is an RSS feed, it will display the content within the shortcode. If it is not an RSS feed, it will display the ‘deny’ message.

[usp_visitor deny="<p>This is not an RSS feed. Content is not available.</p>"]Content for RSS feed goes here.[/usp_visitor]

PHP Function Code

In case you have difficulties debugging what causing issues with [usp_visitor] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp_visitor', 'usp_visitor');

Shortcode PHP function:

function usp_visitor($attr, $content = null) {
	extract(shortcode_atts(array(
		'deny' => '',
	), $attr));
	
	$deny = str_replace("{", "<", $deny);
	$deny = str_replace("}", ">", $deny);
	
	$deny    = htmlspecialchars($deny, ENT_QUOTES);
	$content = htmlspecialchars($content, ENT_QUOTES);
	
	if ((!is_user_logged_in() && !is_null($content)) || is_feed()) return do_shortcode($content);
	
	return $deny;
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-access.php

User Submitted Posts [usp_member] Shortcode

The User Submitted Posts (USP) plugin shortcode ‘usp_member’ checks if a user is logged in. It returns the content if the user is logged in and not in a feed. If not, it returns a ‘deny’ message.

Shortcode: [usp_member]

Parameters

Here is a list of all possible usp_member shortcode parameters and attributes:

  • deny – the message displayed when access is denied
  • content – the content shown to logged-in users

Examples and Usage

Basic example – Displays the content if the user is logged in, otherwise, it shows the denied message.

[usp_member deny="Sorry, you must be logged in to view this content."]Your exclusive content here[/usp_member]

Advanced examples:

Here, we’re using the shortcode to display different content for logged in users and non-logged in users. If the user is not logged in, they will see a custom deny message.

[usp_member deny="Sorry, this content is for members only."]Welcome, member! Here is your exclusive content.[/usp_member]

In this advanced example, we’re using the shortcode to display a membership form for non-logged in users, and a welcome message for logged in users.

[usp_member deny="[contact-form-7 id='123' title='Membership Form']"]Welcome back, member![/usp_member]

Please note that you need to replace the ‘123’ in the ‘deny’ attribute with the ID of your contact form. The ‘title’ should also match the title of your contact form.

PHP Function Code

In case you have difficulties debugging what causing issues with [usp_member] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp_member', 'usp_member');

Shortcode PHP function:

function usp_member($attr, $content = null) {
	extract(shortcode_atts(array(
		'deny' => '',
	), $attr));
	
	$deny = str_replace("{", "<", $deny);
	$deny = str_replace("}", ">", $deny);
	
	$deny    = htmlspecialchars($deny, ENT_QUOTES);
	$content = htmlspecialchars($content, ENT_QUOTES);
	
	if (is_user_logged_in() && !is_null($content) && !is_feed()) return do_shortcode($content);
	
	return $deny;
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-access.php

User Submitted Posts [usp-login-form] Shortcode

The User Submitted Posts (USP) plugin shortcode is used to generate a login form. This shortcode calls the ‘usp_login_form’ function which captures the login form’s HTML output. It then returns this HTML, effectively outputting the login form wherever the shortcode is placed.

Shortcode: [usp-login-form]

Examples and Usage

Basic example – Displays the User Submitted Posts login form on your page or post.

[usp-login-form /]

PHP Function Code

In case you have difficulties debugging what causing issues with [usp-login-form] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp-login-form', 'usp_login_form_shortcode');

Shortcode PHP function:

function usp_login_form_shortcode($attr, $content = null) {
	ob_start();
	usp_login_form();
	$form = ob_get_contents();
	ob_end_clean();
	return $form;
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-login.php

User Submitted Posts [usp-reset-button] Shortcode

The User Submitted Posts plugin shortcode ‘usp-reset-button’ is designed to create a reset button for user forms. The shortcode extracts arguments like class, value, and URL, and checks if the site uses SSL. It then ascertains the server host and request URI. The plugin uses these details to create a URL, removing any unnecessary query arguments. The final output is a paragraph element with a reset link, the class and value of which can be customized.

Shortcode: [usp-reset-button]

Parameters

Here is a list of all possible usp-reset-button shortcode parameters and attributes:

  • class – Specifies the CSS class for the reset button
  • value – Defines the text displayed on the reset button
  • url – Sets the URL to which the reset button points

Examples and Usage

Basic example – The basic usage of the shortcode involves simply calling the shortcode with no additional parameters. This will create a reset button with default settings. The button will display the text ‘Reset form’ and will reset the form upon click.

[usp-reset-button /]

Advanced Examples

1. Customizing the button text – This example demonstrates how to change the default text displayed on the button. Instead of ‘Reset form’, the button will display ‘Clear All’.

[usp-reset-button value='Clear All' /]

2. Adding a CSS class – This example shows how to add a custom CSS class to the reset button. This can be used to apply custom styles to the button.

[usp-reset-button class='my-custom-class' /]

3. Changing the reset URL – In this example, we change the URL that the button redirects to upon reset. Instead of returning to the current page, the button will redirect to the specified URL after resetting the form.

[usp-reset-button url='https://www.example.com' /]

Please note that you can combine multiple parameters in a single shortcode. For instance, you can change the button text, add a CSS class, and change the reset URL all at once:

[usp-reset-button value='Clear All' class='my-custom-class' url='https://www.example.com' /]

PHP Function Code

In case you have difficulties debugging what causing issues with [usp-reset-button] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp-reset-button', 'usp_reset_button_shortcode');

Shortcode PHP function:

function usp_reset_button_shortcode($args) {
	
	extract(shortcode_atts(array(
		'class' => '',
		'value' => __('Reset form', 'usp'),
		'url'   => '#please-check-shortcode',
	), $args));
	
	$protocol = is_ssl() ? 'https://' : 'http://';
	
	$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
	
	$current = isset($_SERVER['REQUEST_URI']) ? $protocol . $host . $_SERVER['REQUEST_URI'] : '';
	
	$url = preg_replace('/%%current%%/', $current, $url);
	
	$url = remove_query_arg(array('usp_reset_form', 'post_id', 'success', 'usp-error'), $url);
	
	$href = get_option('permalink_structure') ? $url .'?usp_reset_form=true"' : $url .'&usp_reset_form=true';
	
	$class = empty($class) ? '' : ' class="'. esc_attr($class) .'"';
	
	$output = '<p'. $class .'><a href="'. esc_url($href) .'">'. esc_html($value) .'</a></p>';
	
	return $output;
	
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-misc.php

User Submitted Posts [usp_display_posts] Shortcode

The USP Display Posts shortcode is a powerful tool that allows you to fetch and display user-submitted posts. It accepts parameters like ‘userid’, ‘post_type’, and ‘numposts’ to customize the output. If ‘userid’ is specified, it fetches posts from that user. If ‘userid’ equals ‘all’, it fetches all user-submitted posts. If ‘userid’ equals ‘current’, it fetches posts from the current user. The ‘post_type’ parameter allows you to specify the type of posts to fetch, and ‘numposts’ controls the number of posts to display. The shortcode outputs a list of links to the fetched posts. Each link is wrapped in an ‘li’ HTML tag, and the entire list is wrapped in an ‘ul’ tag. The link’s title attribute is set to ‘View full post’ for accessibility.

Shortcode: [usp_display_posts]

Parameters

Here is a list of all possible usp_display_posts shortcode parameters and attributes:

  • userid – Defines the user ID whose posts we want to display.
  • post_type – Specifies the type of the posts to display.
  • numposts – Sets the number of posts to be displayed.

Examples and Usage

Basic example – Display all user-submitted posts regardless of the author.

[usp_display_posts userid="all" post_type="post" numposts="-1"]

Advanced examples

Display all posts submitted by a specific user, identified by their user ID. Here, we will display all posts submitted by the user with ID 5.

[usp_display_posts userid="5" post_type="post" numposts="-1"]

Display only the most recent post submitted by the currently logged-in user.

[usp_display_posts userid="current" post_type="post" numposts="1"]

Display all posts submitted by a specific user, identified by their username. In this case, we’ll display all posts submitted by the user with the username “JohnDoe”.

[usp_display_posts userid="JohnDoe" post_type="post" numposts="-1"]

Display a maximum of 10 posts of a custom post type, say “reviews”, submitted by any user.

[usp_display_posts userid="all" post_type="reviews" numposts="10"]

PHP Function Code

In case you have difficulties debugging what causing issues with [usp_display_posts] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp_display_posts', 'usp_display_posts');

Shortcode PHP function:

function usp_display_posts($attr = array(), $content = null) {
	
	global $post;
	
	extract(shortcode_atts(array(
		
		'userid'    => 'all',
		'post_type' => 'post',
		'numposts'  => -1,
		
	), $attr));
	
	if (ctype_digit($userid)) {
		
		$args = array(
			'author'         => $userid,
			'posts_per_page' => $numposts,
			'post_type'      => $post_type,
			'meta_key'       => 'is_submission',
			'meta_value'     => '1'
		);
		
	} elseif ($userid === 'all') {
		
		$args = array(
			'posts_per_page' => $numposts,
			'post_type'      => $post_type,
			'meta_key'       => 'is_submission',
			'meta_value'     => '1'
		);
		
	} elseif ($userid === 'current') {
		
		$args = array(
			'author'         => get_current_user_id(),
			'posts_per_page' => $numposts,
			'post_type'      => $post_type,
			'meta_key'       => 'is_submission',
			'meta_value'     => '1'
		);
		
	} else {
		
		$args = array(
			'posts_per_page' => $numposts,
			'post_type'      => $post_type,
			
			'meta_query' => array(
				
				'relation' => 'AND',
				
				array(
					'key'    => 'is_submission',
					'value'  => '1'
				),
				array(
					'key'    => 'user_submit_name',
					'value'  => $userid
				)
			)
		);
		
	}
	
	$args = apply_filters('usp_display_posts_args', $args);
	
	$submitted_posts = get_posts($args);
	
	$display_posts = '<ul>';
	
	foreach ($submitted_posts as $post) {
		
		setup_postdata($post);
		
		$display_posts .= '<li><a href="'. get_the_permalink() .'" title="'. esc_attr__('View full post', 'usp') .'">'. get_the_title() .'</a></li>';
		
	}
	
	$display_posts .= '</ul>';
	
	wp_reset_postdata();
	
	return $display_posts;
	
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-misc.php

User Submitted Posts [usp_gallery] Shortcode

The User Submitted Posts shortcode, ‘usp_gallery’, displays a gallery of user-submitted images. It allows customization such as image size, format, target, class, number, and ID. .

Shortcode: [usp_gallery]

Parameters

Here is a list of all possible usp_gallery shortcode parameters and attributes:

  • size – defines the size of the images in the gallery
  • format – determines the file format of the images
  • target – sets the target attribute for the image links
  • class – applies a CSS class to the gallery images
  • number – sets the maximum number of images in the gallery
  • id – unique identifier for the specific gallery

Examples and Usage

Basic example – Show user-submitted images in thumbnail size.

[usp_gallery size="thumbnail"]

Advanced examples

Display user-submitted images in a specific format, with a target of opening in a new tab and applying a specific CSS class.

[usp_gallery format="image" target="blank" class="my-custom-class"]

Limit the number of user-submitted images to 10 and get images from a specific post ID.

[usp_gallery number="10" id="123"]

Combine multiple parameters to customize the display of user-submitted images. This example shows images in full size, in image format, opens in the same tab, applies a custom class, limits to 20 images, and fetches images from a specific post ID.

[usp_gallery size="full" format="image" target="self" class="my-custom-class" number="20" id="123"]

PHP Function Code

In case you have difficulties debugging what causing issues with [usp_gallery] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('usp_gallery', 'usp_gallery');

Shortcode PHP function:

function usp_gallery($attr, $content = null) {
	
	extract(shortcode_atts(array(
		
		'size'    => 'thumbnail',
		'format'  => 'image',
		'target'  => 'blank',
		'class'   => '',
		'number'  => 100,
		'id'      => false,
		
	), $attr));
	
	$images = usp_get_images($size, $format, $target, $class, $number, $id);
	
	$gallery = '';
	
	foreach ($images as $image) $gallery .= $image;
	
	$gallery = $gallery ? '<div class="usp-image-gallery">'. $gallery .'</div>' : '';
	
	return $gallery;
	
}

Code file location:

user-submitted-posts/user-submitted-posts/library/shortcode-misc.php

Conclusion

Now that you’ve learned how to embed the User Submitted Posts Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *