A Cozmoslabs Product

Yes or No conditional shortcode

This function adds a Yes or No conditional shortcode.

Shortcode:

[yes_no_display field=”{custom_field_3}”]
This is displayed only if field = Yes
[/yes_no_display]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Yes No shortcode. Tags: contitional, shortcode, yes, no
 * Use like so: [yes_no_display field="{custom_field_3}"]This is displayed only if field = Yes.[/yes_no_display]
 */
 
add_shortcode( 'yes_no_display', 'wppbc_is_field_yes_or_no' );
function wppbc_is_field_yes_or_no( $atts, $content ) {
   extract(
      shortcode_atts( array( 'field' => '' ), $atts )
   );
 
    $user_id = get_current_user_id();
    $meta_key = trim( $atts['field'],"{}");
    $field = get_user_meta( $user_id, $meta_key, true );
 
   if ( $field == 'Yes' || $field == 'yes' ) {
      return $content;
   }
 
   return;
}

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket