Home > Software design >  How to show the default value for shortcode if an attribute is not added to the shortcode
How to show the default value for shortcode if an attribute is not added to the shortcode

Time:06-17

I have below a shortcode on my page.

[singletextutm url="/about-us/" name="Know more"  ] 

I am using the below code in the function.php and it's working

function singletextutmlink($atts, $content='') {

    $atts = shortcode_atts(
        array(
            'url' => '',
            'name' => '',
            'class' => '',
            'icon'=> '',
        ), $atts);
    
    if ($atts['icon'] == 0) {
        $ctaicon ='';
    }
    else {
          $ctaicon ='<i ></i>';
    }
    
 return $ctaicon;

}
add_shortcode( 'singletextutm', 'singletextutmlink');

Now my issue with the below code. I haven't added an icon attribute to my shortcode because I have to set the default icon attribute. I mean if the icon attribute is not added in the shortcode then still it should run the else condition.

if ($atts['icon'] == 0) {
        $ctaicon ='';
    }
    else {
          $ctaicon ='<i ></i>';
    }

The expected output in the comment

[singletextutm url="/about-us/" name="Know more"  icon="0"] // it will display empty $ctaicon

[singletextutm url="/about-us/" name="Know more"  ] // Not set icon but still run else conditon  <i ></i>

[singletextutm url="/about-us/" name="Know more"  icon="1"] // it will display <i ></i>

CodePudding user response:

You can try

$atts = shortcode_atts(
    array(
        'url' => '',
        'name' => '',
        'class' => '',
        'icon'=> null,
    ), $atts);
$ctaicon = (!is_null($atts['icon']) && $atts['icon'] == 0) ? '' : '<i ></i>';

First param of shortcode_atts means the default k-v, it set the default value of icon to null now, so that only if someone add the icon attribute to 0 it will return ''.

CodePudding user response:

It totally work on my side -

add_shortcode( 'singletextutm', 'singletextutmlink');

function singletextutmlink($atts, $content='') {
    $atts = shortcode_atts( array(
        'url' => '',
        'name' => '',
        'newtab' => '',
        'class' => '',
        'icon'=> '',
    ), $atts);
    
    if ($atts['icon'] == 1) {
        $ctaicon =' <i ></i>';
    }
    
    if ($atts['newtab'] == true) {
        $newtab ='target="_blank"';
    }
    
    $button = "<a href='".$atts['url']."' ".$newtab." class='".$atts['class']."'>".$atts['name'].$ctaicon."</a>";
    return $button;
}

With this shortcode -

[singletextutm url="https://www.example.com/about-us/" newtab="true" name="Know more"  icon="1"]

enter image description here

CodePudding user response:

You need to set a default in your shortcode_atts. 'icon' => '0' This sets the icon to be the character (not the integer) '0'. Then when you're comparing values, you want to look for the character 0 and not the integer.

Also, according to WPCS, you should be using strict comparisons === or !==

I don't see any use of your other parameters url etc in this code, but I assume this is just to understand the issue with the icon param?

function singletextutmlink( $atts ) {

    $atts = shortcode_atts(
        array(
            'url'   => '',
            'name'  => '',
            'class' => '',
            'icon'  => '0',
        ),
        $atts,
        'singletextutm'
    );

    if ( '0' === $atts['icon'] ) {
        $ctaicon = '';
    } else {
        $ctaicon = '<i ></i>';
    }

    return $ctaicon;

}
add_shortcode( 'singletextutm', 'singletextutmlink' );

CodePudding user response:

This is the source code forshortcode_atts().

Its logic states that it filters the keys in the $atts array by the whitelist of keys in your hardcoded array AND if any of the whitelisted keys are not found in $atts, then they will be added to the output array with its hardcoded default value. Any elements with keys not mentioned in the hardcoded whitelist array will be excluded from the returned $out array.

All values passed in from the parsed shortcode string will be strings, so you can treat them as such when making conditions/comparisons.

To ensure that the icon value defaults to present the fint awesome icon, set the hardcoded default value to 1.

To avoid hard-to-read concatenation, use sprintf() with placeholders; this will make future maintenance easier too.  I'll use loose/truthy comparisons to determine if values should be included in the returned output string.

function singletextutmlink( $atts ) {
    $atts = shortcode_atts(
        [
            'url'   => '',
            'name'  => '',
            'class' => '',
            'icon'  => '1',
        ],
        $atts,
        'singletextutm'
    );

    return sprintf(
        "<a%s%s>%s%s</a>"
        $atts['url'] ? " href=\"{$atts['url']}\"" : '',
        $atts['class'] ? " class=\"{$atts['class']}\"" : '',
        $atts['name'],
        $atts['icon'] ? ' <i ></i>' : ''
    );
}
  • Related