簡體   English   中英

簡化Wordpress短代碼(或:“自定義短代碼”)

[英]Simplify a Wordpress shortcode (or: “custom shortcode”)

我使用短代碼在我的Wordpress模板上顯示我的圖像。

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

因為我在Markdown中寫了我的帖子,當我有10張或更多照片要包含時,寫這些[attr =“ibutes”]有點無聊。 這就是為什么我想要簡化這些照片包括有類似的東西:

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

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

當我保存帖子時,它運行一個檢測短代碼的鈎子,調用Flickr / Instagram API獲取源圖像URL並將其存入數據庫(每個帖子的每張照片的數組)。

這是我目前捕獲短代碼的代碼: http//pastebin.com/TVEQKudg

有什么方法來簡化這些電話? 或者另一個想法是快速調用它並且不使用這些長短代碼?

好的,你去吧:

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

假設你有這樣的內容:

簡單測試[13927101801“標題”] [mqEqHAC3rK“標題”]簡單測試簡單測試簡單測試簡單測試簡單測試簡單測試簡單測試[13927101801]

然后你可以這樣做:

$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);

輸出:

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

您將獲得id和(可選) caption的數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM