HTML WordPress etiketleri dönüştürmek için normal ifade

2 Cevap php

Ben normal bir PHP dosyasına wordpress okuyorum bu HTML var. Bu, geçerli HTML değil ama wordpress img etiketi almak dışarı [caption] şerit ve bir <p> etiketi gibi yazısı ile bir div içine koymak gerekiyordu. Ama ben wordpress ile, PHP bunu yapmak istiyorum.

[caption id="" align="alignnone" width="190" caption="my caption"]html image tag would got here but got stripped[/caption]

Nasıl bir normal ifadede bu yapardı. Ben nasılsa bir şekilde burada preg_match_callback kullanarak düşünüyorum.

2 Cevap

Bu wordpress kod çalışma yapalım için ihtiyacınız olan tüm iki dosya wp-includes dizinindeki plugin.php ve shortcodes.php gibi görünüyor.

require 'wordpress/wp-includes/plugin.php';
require 'wordpress/wp-includes/shortcodes.php';
add_shortcode('caption', 'handleCaption');
$content = '[caption id="foo" style="bar"]Marry had a little lamb[/caption]whose fleece was white as snow';
do_shortcode($content);

function handleCaption($attributes, $content='') {
    var_dump($attributes, $content);
}

baskılar

array(2) {
  ["id"]=>
  string(3) "foo"
  ["style"]=>
  string(3) "bar"
}
string(23) "Marry had a little lamb"

İşte muhtemel belgeli exp var.

$res = preg_match_all('/\[caption(.*?)\](.*?)\[\/caption\]/iU',
'[caption id="" align="alignnone" width="190" caption="my caption"]html image tag would got here but got stripped[/caption]',
$matches);


var_dump($res, $matches);

Çıktı

int(1)
array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(122) "[caption id="" align="alignnone" width="190" caption="my caption"]html image tag would got here but got stripped[/caption]"
  }
  [1]=>
  array(1) {
    [0]=>
    string(57) " id="" align="alignnone" width="190" caption="my caption""
  }
  [2]=>
  array(1) {
    [0]=>
    string(46) "html image tag would got here but got stripped"
  }
}