Creating shortcode question

Hi everyone!

I am reading Brad Williams’ Professional WordPress Designer and Development book.

add_shortcode('hs', 'halloween_store_shortcode');
function halloween_store_shortcode ($atts, $content=null)
    //setting defaults

Eventually, within the content we insert a shortcode, such as this one: [hs show=price].

I am having hard time with understanding what shortcode_atts and extract functions do, although I have read their definitions in Codex and

  1. shortcode_atts:
    Combine user attributes with known attributes and fill in defaults when needed.


  • what are user attributes? Does it refer to attributes that are sent from shortcode macros, e.g. [hs show=price]?
  • What are ‘known attributes’ since I did not define any attributes prior to this code?
  • “combine”? how does it combine them? Into any array?

I appreciate your help. I could simply memorize it, but I don’t think that memorizing it will help me become good at WordPress.

and extract()
Import variables from an array into the current symbol table.

I assume shortcode_atts returns an array. “imports variables into current SYMBOL TABLE”?

What is a symbol table?

I appreciate any help with helping me understand that piece of code. Below is a full function definition to give you some context:

function halloween_store_shortcode($atts, $content = null)
    global $post;
    //it is like setting the defaults
    "show" => '' //sets attribute default
    ), $atts ));
    //load options array
    $hween_options_arr = get_option('halloween_options');
    //load product data
    $hween_product_data = get_post_meta($post->ID,'_halloween_product_data', true);
    if ($show == 'sku')
        $hs_show = !empty($hween_product_data['sku']) ? $hween_product_data['sku'] : '';
    elseif ($show == 'price')
        $hs_show = $hween_options_arr['currency_sign'];
        $hs_show = !empty($hween_product_data['price']) ? $hs_show . $hween_product_data['price'] : '';
    elseif ($show == 'weight')
        $hs_show = !empty($hween_product_data['weight']) ? $hween_product_data['weight'] : '';
    elseif ($show == 'color')
        $hs_show = !empty($hween_product_data['color']) ? $hween_product_data['color'] : '';
    elseif ($show == 'inventory' && !empty($hween_options_arr['show_inventory']))
        $hs_show = !empty($hween_product_data['inventory']) ? $hween_product_data['inventory'] : '';
    //return the shortcode value to display
    return $hs_show;

extract does very simple thing, it creates separated variables from array items. Key of each item becomes variable name and value of item turns into value of that variable.

$array = ['name' => 'John', 'age' => 32];
echo $name; //John
echo $age; //32

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.