User Specific Content Shortcode

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

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

Plugin Icon
User Specific Content

"User Specific Content is a versatile WordPress plugin. It allows admins to control content visibility on a user-specific basis, enhancing user experience and personalization."

★★★★✩ (36) Active Installs: 3000+ Tested with: 4.7.0 PHP Version: false
Included Shortcodes:
  • [O_U]

User Specific Content [O_U] Shortcode

The User Specific Content shortcode allows for content customization based on user attributes. It verifies user details like ID, username, and role. If a user isn’t logged in or doesn’t match the specified attributes, a blocked message displays. The shortcode also handles content for logged in/out users. It’s a versatile tool for personalized content.

Shortcode: [O_U]

Parameters

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

  • user_id – Defines specific user ID to display content to.
  • user_name – Determines content visibility based on username.
  • user_role – Allows content display based on user role.
  • logged_status – Shows content based on user login status.
  • blocked_message – Sets message for users who can’t view content.
  • blocked_meassage – Alternate way to set blocked content message.

Examples and Usage

Basic example – Displays user-specific content for a logged-in user with a specific user ID

[O_U user_id="123" logged_status="in" /]

Advanced examples

Displays user-specific content for a logged-in user with a specific username.

[O_U user_name="john_doe" logged_status="in" /]

Displays a custom blocked message for a user who doesn’t meet the criteria.

[O_U user_id="123" logged_status="in" blocked_message="Access denied" /]

Displays user-specific content for a logged-in user with a specific role.

[O_U user_role="administrator" logged_status="in" /]

Displays user-specific content for a user who is not logged in.

[O_U logged_status="out" /]

Displays user-specific content for a user with multiple attributes (user ID, username, and user role).

[O_U user_id="123" user_name="john_doe" user_role="administrator" logged_status="in" /]

PHP Function Code

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

Shortcode line:

add_shortcode('O_U',array($this,'User_specific_content_shortcode'));

Shortcode PHP function:

                    function User_specific_content_shortcode($atts, $content = null,$tag = ''){
		$atts = shortcode_atts(array(
			'user_id'          => '',
			'user_name'        => '',
			'user_role'        => '',
			'logged_status'    => '',
			'blocked_message'  => false,
			'blocked_meassage' => null
	    ), $atts);
		
		extract($atts);
		

		global $post;
		if ($blocked_meassage !== null){
			$blocked_message = $blocked_meassage;
		}
		
		$options = $this->U_S_C_get_option('U_S_C');
		$current_user = wp_get_current_user();

		if ( (isset($user_id) && $user_id != '' ) || (isset($user_name) && $user_name != '') || (isset($user_role) && $user_role != '') ){
			//check logged in
			if (!is_user_logged_in()){
				return $this->displayMessage($blocked_message);
			}

			//check user id
			if (isset($user_id) && $user_id != '' ){
				$user_id = explode(",", $user_id);
				if (!in_array($current_user->ID,$user_id)){
					return $this->displayMessage($blocked_message);
				}		
			}

			//check user name
			if (isset($user_name) && $user_name != '' ){
				$user_name = explode(",", $user_name);
				if (!in_array($current_user->user_login,$user_name)){
					return $this->displayMessage($blocked_message);
				}
			}

			//check user role
			if (isset($user_role) && $user_role != '' ){
				$user_role = explode(",", $user_role);
				if (!$this->has_role($user_role)){
					return $this->displayMessage($blocked_message);
				}
			}
		}

		//logged in
		if ($logged_status == 'in'){
			if (!is_user_logged_in()){
				return $this->displayMessage($blocked_message);
			}
		}
		//logged out
		if ($logged_status == 'out'){
			if (is_user_logged_in()){
				return $this->displayMessage($blocked_message);
			}
		}

		return apply_filters('user_spcefic_content_shortcode_filter',do_shortcode($content));
	}
                    

Code file location:

user-specific-content/user-specific-content/User-Specific-Content.php

Conclusion

Now that you’ve learned how to embed the User Specific Content Plugin shortcode, 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 *