0

i have been working on a shortcode and I am having trouble passing $atts to it.

i can't figure out what i am doing wrong.

To say it another way, i cannot update 'input_product_image_ID' to a different value from it's default value of 13 by inserting a new value into the shortcode.

for example this shortcode would still return 13, not 12:

[product_picture input_product_image_ID="12"]

My code:

function article_image_insert_shortcode($atts) {
   extract(shortcode_atts(array(
      'input_product_image_ID' => 13,
   ), $atts));


$product_id = $input_product_image_ID;


// get product page url which is stored as caption 
$product_page_url           = get_post_field('post_excerpt', $product_id );

// get and insert image 
$product_image              = wp_get_attachment_image( $product_id , full_size );

// get image alt text which serves as the image title 
$product_description_alt    = get_post_meta($product_id , '_wp_attachment_image_alt', true);


// get image caption where link is stored


$thumb_img = get_post( get_post($product_id) );

// assign caption 

$product_aff_url            = $thumb_img->post_content;


$article_image_insert_shortcode = /* do some stuff */;

return $article_image_insert_shortcode;


}

add_shortcode('product_picture', 'article_image_insert_shortcode');
  • Yes because you are defining input_product_image_ID to 13 in shortcode function itself. – Robert hue Dec 24 '14 at 10:56
  • im not sure that is it, shouldn't that value update from shortcode atts? – Peter Vijeh Dec 24 '14 at 11:11
  • for example, this shortcode defines $atts similarly to mine, and yet it still updates function random_picture($atts) { extract(shortcode_atts(array( 'width' => 400, 'height' => 200, ), $atts)); return '<img src="http://lorempixel.com/'. $width . '/'. $height . '" />'; } add_shortcode('picture', 'random_picture'); – Peter Vijeh Dec 24 '14 at 11:15
  • Okay, use attribute name in lower case input_product_image_id instead of input_product_image_ID – Robert hue Dec 24 '14 at 11:16
  • thanks that did it--- I can't use caps like that in atts? in any case I appreciate your help!! – Peter Vijeh Dec 24 '14 at 11:21
0

To add to the answers in the comments from RH, I found this in the codex (it's kind of burried deep):

  • Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.

This is from: http://codex.wordpress.org/Function_Reference/add_shortcode#Parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.