user2538833 February 2016

Conditional statement for WordPress Shortcodes

I have two shortcodes setup: [easy_options id="companyName"] and [easy_options id="companyLogo"]

I'm wanting WordPress to check if [easy_options id="companyLogo"] holds data (an image upload), if not display [easy_options id="companyName"] (plain text)...

Is this possible? If so could anyone please point me in the right direction?

Answers


Jalpa February 2016

You need to put condition to check ID like below.

function your_short_code_function($atts) {
    $options = shortcode_atts(array(
       'id' => ''
    ), $atts, "easy_options");

   if($options['id'] == "companyLogo") {
       //an image upload
   }
}


PHPExpert February 2016

You can do it by using switch case as follow:

function easy_options_shortcodes( $atts ) {
extract( shortcode_atts( array(
    'id' => 'companyName' //cosidering a defaultvalue

), $atts ) );

switch( $id ){
    case 'companyName': 
        $output = '<div class="classcompanyName"></div>';
        break;

    case 'companyLogo': 
        $output = '<div class="companyLogo"></div>';
        break;

    default:
        $output = '<div class="defaultshortcodecontent"></div>';
        break;
  }

   return $output;
}

add_shortcode( 'easy_options', 'easy_options_shortcodes' );

Post Status

Asked in February 2016
Viewed 1,705 times
Voted 13
Answered 2 times

Search




Leave an answer