Home Ask Login Register

Developers Planet

Your answer is one click away!

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?


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>';

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

        $output = '<div class="defaultshortcodecontent"></div>';

   return $output;

add_shortcode( 'easy_options', 'easy_options_shortcodes' );

Post Status

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


Leave an answer

Quote of the day: live life