Domanda

I use a shortcode to display my images on my Wordpress template.

[photo id="13927101803" caption="Some text"] // Flickr
[photo id="mqEqHAC3rK"] // Instagram, no caption

Because I write my posts in Markdown, it's kind of boring to write these [attr="ibutes"] when I have 10 photos or more to include. That's why I'm looking to simplify these photos includes to have something like that :

[13927101803 "Caption"] // Flickr
[mqEqHAC3rK] // Instagram

// Causes a conflict with the links in Markdown [this is](http://a-link)… ?

When I save a post, it runs a hook which detects the shortcodes, call the Flickr/Instagram API to get the source image url and stock it into the database (with an array of each photo for each post).

Here is my current code to catch the shortcodes: http://pastebin.com/TVEQKudg

Any way to simplify these calls? Or another idea to call it quickly and don't use these long shortcodes?

È stato utile?

Soluzione

Ok here you go:

/\[(\d{11}|\w{10})(?:\s*"([^"]+)")?\]\/

Lets say you have this content:

Simple testing [13927101801 "Caption"] [mqEqHAC3rK "caption"]Simple testing Simple testing Simple testinga Simple testing Simple testinga Simple testing Simple testinga [13927101801]

Then you could do this:

$content = do_shortcode(get_the_content());
preg_match_all('/\[(\d{11}|\w{10})(?:\s*"([^"]+)")?\]\/', $content, $matches);

$matches = array_slice($matches, 1);
$matches = call_user_func_array( 'array_map', array_merge(array(NULL), $matches) );

var_dump($matches);

Output:

array (size=3)
  0 => 
    array (size=2)
      0 => string '13927101801' (length=11) // <-- ID
      1 => string 'Caption' (length=7)      // <-- Caption content
  1 => 
    array (size=2)
      0 => string 'mqEqHAC3rK' (length=10) // <-- ID
      1 => string 'caption' (length=7)     // <-- Caption content
  2 => 
    array (size=2)
      0 => string '13927101801' (length=11) // <-- ID
      1 => string '' (length=0)           // <-- No Caption content

And you will get the array of id and (Optional) caption

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top