1

Is there a clean way to get a shortcode's attribute value outside of Wordpress's default behaviour? I am working on a single script outside a Wordpress installation, where I only do include('../wp-load.php'); at the beginning.

In my case, I am looking to get the "caption" attribute from the [caption] shortcode and have found the best way to be is as follows, but I'm very unhappy with this.

Original post's content:

[caption id="attachment_1" align="alignleft" width="150" caption="I'm a wicked cool banana"]<img src="http://banana.dev/uploads/2013/07/banana.jpg" alt="Banana" title="Banana" width="150" height="150" class="size-full wp-image-1" />[/caption]

My script to get the caption from the shortcode:

// prepare full content
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);

// test the HTML and get the part inside
if (preg_match("/<p class=\"wp-caption-text\">(.*)<\\/p>/", $content, $matches)) {
  $caption = $matches[1];
}

But this can't be the best way, can it?

Please note that I'm not looking to get the image's attributes, but the shortcode's attributes.

EDIT 1

As s_ha_dum suggested, I tried getting the shortcode with get_shortcode_regex(), but then I get the following:

array (size=7)
  0 => 
    array (size=1)
      0 => string '[caption id="attachment_1" align="alignleft" width="150" caption="I'm a wicked cool banana"]<img src="http://banana.dev/uploads/2013/07/banana.jpg" alt="Banana" title="Banana" width="150" height="150" class="size-full wp-image-1" />[/caption]' (length=394)
  1 => 
    array (size=1)
      0 => string '' (length=0)
  2 => 
    array (size=1)
      0 => string 'caption' (length=7)
  3 => 
    array (size=1)
      0 => string ' id="attachment_1" align="alignleft" width="150" caption="I'm a wicked cool banana"' (length=206)
  4 => 
    array (size=1)
      0 => string '' (length=0)
  5 => 
    array (size=1)
      0 => string '<img src="http://banana.dev/uploads/2013/07/banana.jpg" alt="Banana" title="Banana" width="150" height="150" class="size-full wp-image-1" />' (length=169)
  6 => 
    array (size=1)
      0 => string '' (length=0)

Which means I still have to Regex $matches[3] to get my desired result.

2

Don't try to parse the shortcodes and then regex the HTML. Use get_shortcode_regex() to parse the raw post content:

$content = $content_post->post_content;
preg_match_all("/$pattern/",$content,$matches);

Then crawl $matches to find your shortcode data. Use shortcode_parse_atts($matches[3][0]) (note $matches[3][0] to give the first element within the matched shortcode's attributes) to pull apart the attribute string.

Reference:
https://wordpress.stackexchange.com/a/73461/21376

  • But I'd still have to use regex to find the caption="I'm a wicked cool banana" bit. Is that correct? – Bondt Jun 1 '15 at 15:57
  • No, not if the caption is a shortcode attribute. Did you try the code at all> – s_ha_dum Jun 1 '15 at 16:50
  • As a matter of fact I did, yes. Please see my edit above. – Bondt Jun 2 '15 at 8:49
  • Thanks for adding the shortcode_parse_atts there. That did it! I updated your answer, so it works. – Bondt Jun 2 '15 at 11:55

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.