Content Restriction / Visibility Shortcode

Hide and Show Content with the Content Shortcode #

The following shortcode can be used to restrict content, so that it can be seen only by groups/b2b/b2c/users

[b2bking_content show_to=b2b]Your content here...[/b2bking_content]

The parameter show_to is mandatory. This parameter can contain multiple items, comma-separated. The parameter supports 5 elements:

  • b2b
  • b2c
  • loggedout
  • b2bking group IDs (e.g. 71, 343, 5884. Each business group has its own ID, which is visible in the link of the group's page when you click edit group)
  • usernames e.g: admin, user, mike.johnson, mazda.213

More examples:

[b2bking_content show_to=b2c,loggedout,admin]Your content here...[/b2bking_content]

In this example: b2c users, logged out users, and the user "admin" will see content

[b2bking_content show_to=123,b2c,loggedout,admin]Your content here...[/b2bking_content]

In this example: b2b users in the group with ID 123, b2c users, logged out users and the user admin will see the content.

[b2bking_content show_to=123,14,88]Your content here...[/b2bking_content]

In this example: b2b users in groups 123,14 and 88 will see the content.

How to apply CSS for B2B / B2C users only #

The shortcode above will not be applicable to all situations. An often encountered scenario would be a need to apply CSS for only particular users.

You can use the following snippet added to functions.php or any snippets plugin

// Apply CSS for B2B users only
add_action('wp_head','b2b_only_css');
function b2b_only_css(){
   // check that user is B2B
   if (get_user_meta(get_current_user_id(), 'b2bking_b2buser', true) === 'yes'){
      ?>
      <style>
      .your_css_here{
         background:red;
      }
      </style>
    <?php
      }
}

// Apply CSS for B2C and Logged Out users only
add_action('wp_head','b2c_only_css');
function b2c_only_css(){
   // check that user is not B2B
   if (get_user_meta(get_current_user_id(), 'b2bking_b2buser', true) !== 'yes'){
      ?>
      <style>
      .your_css_here{
         background:red;
      }
      </style>
    <?php
      }
}

Powered by BetterDocs

Leave a Reply

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