Below, you’ll find a detailed guide on how to add the Content Control 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 Content Control Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Content Control Plugin and the shortcodes it provides:
Content Control is the ultimate plugin for WordPress, allowing you to restrict content, create conditional blocks, and more. Customize your content visibility with ease using this powerful tool.
- [content_control]
Content Control [content_control] Shortcode
The Content Control shortcode is a powerful tool for managing user access in WordPress. It allows fine-tuning of user roles and status. This shortcode lets you define whether a user is ‘logged_in’ or ‘logged_out’, specify ‘allowed_roles’ and ‘excluded_roles’, and customize the denial message. The shortcode also handles deprecated attributes, providing backward compatibility. It can normalize attributes and convert classes to an array for easier processing. In essence, it checks if a user meets specific requirements, then displays content accordingly, making it a valuable tool for content control.
Shortcode: [content_control]
Parameters
Here is a list of all possible content_control shortcode parameters and attributes:
status
– defines if the user is ‘logged_in’ or ‘logged_out’allowed_roles
– sets the user roles that can view the contentexcluded_roles
– sets the user roles that cannot view the contentclass
– adds custom classes to the containermessage
– displays a message if the user doesn’t meet the criteria
Examples and Usage
Basic example – A shortcode that uses the content control function to display content only to logged-in users.
[content_control status="logged_in"]Your exclusive content here[/content_control]
Advanced examples
Using the shortcode to display content only to users with specific roles. In this case, the content is only visible to users with the role of ‘editor’ or ‘administrator’.
[content_control status="logged_in" allowed_roles="editor, administrator"]Your exclusive content here[/content_control]
Using the shortcode to display a custom message to users who do not meet the specified conditions. In this case, the message is displayed to users who are not logged in.
[content_control status="logged_out" message="You must be logged in to view this content."]Your exclusive content here[/content_control]
Using the shortcode to exclude certain user roles from viewing the content. Here, the content is hidden from users with the ‘subscriber’ role.
[content_control status="logged_in" excluded_roles="subscriber"]Your exclusive content here[/content_control]
PHP Function Code
In case you have difficulties debugging what causing issues with [content_control]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'content_control', [ $this, 'content_control' ] );
Shortcode PHP function:
function content_control( $atts, $content = '' ) {
// Deprecated.
$deprecated_atts = shortcode_atts( [
'logged_out' => null, // @deprecated 2.0.
'roles' => null, // @deprecated 2.0.
], $atts );
$atts = shortcode_atts( [
'status' => 'logged_in', // 'logged_in' or 'logged_out
'allowed_roles' => null,
'excluded_roles' => null,
'class' => '',
'message' => $this->container->get_option( 'defaultDenialMessage', '' ),
], $this->normalize_empty_atts( $atts ), 'content_control' );
// Handle old args.
if ( isset( $deprecated_atts['logged_out'] ) ) {
$atts['status'] = (bool) $deprecated_atts['logged_out'] ? 'logged_out' : 'logged_in';
}
if ( isset( $deprecated_atts['roles'] ) && ! empty( $deprecated_atts['roles'] ) ) {
$atts['allowed_roles'] = $deprecated_atts['roles'];
}
$user_roles = [];
$match_type = 'any';
// Normalize args.
if ( ! empty( $atts['excluded_roles'] ) ) {
$user_roles = $atts['excluded_roles'];
$match_type = 'exclude';
} elseif ( ! empty( $atts['allowed_roles'] ) ) {
$user_roles = $atts['allowed_roles'];
$match_type = 'match';
}
// Convert classes to array.
$classes = ! empty( $atts['class'] ) ? explode( ' ', $atts['class'] ) : [];
$classes[] = 'content-control-container';
// @deprecated 2.0.0
$classes[] = 'jp-cc';
if ( user_meets_requirements( $atts['status'], $user_roles, $match_type ) ) {
$classes[] = 'content-control-accessible';
// @deprecated 2.0.0
$classes[] = 'jp-cc-accessible';
$container = '<div class="%1$s">%2$s</div>';
} else {
$classes[] = 'content-control-not-accessible';
// @deprecated 2.0.0
$classes[] = 'jp-cc-not-accessible';
$container = '<div class="%1$s">%3$s</div>';
}
$classes = implode( ' ', $classes );
return sprintf( $container, esc_attr( $classes ), do_shortcode( $content ), do_shortcode( $atts['message'] ) );
}
Code file location:
content-control/content-control/classes/Controllers/Shortcodes.php
Conclusion
Now that you’ve learned how to embed the Content Control 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.
Leave a Reply