WordPress – trying to create shortcode

I’m trying to do shortcode with default attributes.

Here’s my code:

Read More
function custom_list_item_shortcode($attributes, $content = null ) {

extract( shortcode_atts( array(
    'icon' => '',
    'color' => ''
), $attributes ) );


$html = '<div class="listbox-item">';

//--If there is an icon show icon
if($icon != ''){
    $html .=  '<div class="listbox-icon"><i class="fa fa-'.$icon.' icon-'.$color.'"></i></div>';
}

$html .=  '<div class="listbox-content">'.$content.'</div>';
$html .= '</div>';
return  $html;
}
add_shortcode('icon-list', 'custom_list_item_shortcode');

What I want to achieve is that some things (for example font size etc.) are default and class is not displayed in firebug, but when someone wants to change font size, they can just type

[icon-list icon="globe" size="22"][/icon-list]

Any ideas?

Related posts

1 comment

  1. Suppose you want size to have a default value of 22:

    function custom_list_item_shortcode($attributes, $content = null ) {
    
      extract( shortcode_atts( array(
          'icon' => '',
          'color' => '',
          'size' => '22'
      ), $attributes ) );    
    
      $html = '<div class="listbox-item" style="font-size:'.$size.'px">';
    
      //--If there is an icon show icon
      if($icon != ''){
          $html .=  '<div class="listbox-icon"><i class="fa fa-'.$icon.' icon-'.$color.'"></i></div>';
      }
    
      $html .=  '<div class="listbox-content">'.$content.'</div>';
      $html .= '</div>';
      return  $html;
    }
    
    add_shortcode('icon-list', 'custom_list_item_shortcode');
    

    Read: https://codex.wordpress.org/Shortcode_API

Comments are closed.