从POST中的每个快捷代码中提取属性值

时间:2014-12-14 作者:Lionheart

我在帖子中有多个短代码:

[section title="first"]
[section title="second"]
[section title="third"]
我想从外部函数中提取所有标题值(即第一、第二、第三个值),这就是我要做的:

$pattern = get_shortcode_regex();
preg_match(\'/\'.$pattern.\'/s\', $post->post_content, $matches);
    if (is_array($matches) && $matches[2] == \'section\') {
        $attribureStr = str_replace (" ", "&", trim ($matches[3]));
        $attribureStr = str_replace (\'"\', \'\', $attributeStr);
        $attributes = wp_parse_args ($attributeStr);
        echo $attributes["title"];
    }
问题是代码只从第一个短代码中提取值。如何让它适用于帖子中的每个短代码?

1 个回复
最合适的回答,由SO网友:birgire 整理而成

方法#1

如果可用,我将使用:

shortcode_atts_{$shortcode}
筛选以收集给定短代码的属性。

Example:

$text = \'
    [gallery]
    [gallery ids="1,2" link="file"]
    [gallery ids="3"]
    [caption id="attachment_6" align="alignright" width="300"]
\';

if( class_exists( \'WPSE_CollectShortcodeAttributes\' ) )
{
    $o = new WPSE_CollectShortcodeAttributes;
    $out = $o->init( $shortcode = \'gallery\', $text )->get_attributes();
    print_r( $out );
}
其中,我们的助手类定义为:

class WPSE_CollectShortcodeAttributes
{
    private $text      = \'\';
    private $shortcode = \'\';
    private $atts      = array();

    public function init( $shortcode = \'\', $text = \'\' )
    {
        $this->shortcode = esc_attr( $shortcode );
        if( shortcode_exists( $this->shortcode ) 
            && has_shortcode( $text, $this->shortcode ) 
        )
        {
            add_filter( "shortcode_atts_{$this->shortcode}", 
                array( $this, \'collect\' ), 10, 3 );
            $this->text = do_shortcode( $text );
            remove_filter( "shortcode_atts_{$this->shortcode}", 
                array( $this, \'collect\' ), 10 );
        }
        return $this;
    }

    public function collect( $out, $pair, $atts )
    {
        $this->atts[] = $atts;
        return $out;
    }

    public function get_attributes()
    {
        return $this->atts;
    }
}
我们示例的输出是:

Array
(
    [0] => Array
        (
            [0] => 
        )

    [1] => Array
        (
            [ids] => 1,2
            [link] => file
            [orderby] => post__in
            [include] => 1,2
        )

    [2] => Array
        (
            [ids] => 3
            [orderby] => post__in
            [include] => 3
        )

)
所以对于gallery shortcode,我们还获得了一些额外的属性。

方法#2

但并非所有短代码都支持上述过滤器。在这种情况下,您可以尝试以下操作:

/**
 * Grab all attributes for a given shortcode in a text
 *
 * @uses get_shortcode_regex()
 * @uses shortcode_parse_atts()
 * @param  string $tag   Shortcode tag
 * @param  string $text  Text containing shortcodes
 * @return array  $out   Array of attributes
 */

function wpse172275_get_all_attributes( $tag, $text )
{
    preg_match_all( \'/\' . get_shortcode_regex() . \'/s\', $text, $matches );
    $out = array();
    if( isset( $matches[2] ) )
    {
        foreach( (array) $matches[2] as $key => $value )
        {
            if( $tag === $value )
                $out[] = shortcode_parse_atts( $matches[3][$key] );  
        }
    }
    return $out;
}

Example:

$text = \'
    [gallery]
    [gallery ids="1,2" link="file"]
    [gallery ids="3"]
    [caption id="attachment_6" align="alignright" width="300"][/caption]
\';
$out = wpse172275_get_all_attributes( \'gallery\', $text );
print_r( $out );
输出:

Array
(
    [0] => 
    [1] => Array
        (
            [ids] => 1,2
            [link] => file
        )

    [2] => Array
        (
            [ids] => 3
        )

)
我希望你能根据自己的需要修改这个。

结束

相关推荐

Content between shortcodes

我想知道是否有一种方法可以在短代码之间获取内容并将其输入到字符串中,例如,我希望能够拥有[短代码]Hello World![/shortcode]输入一个名为$ShortCodeText的字符串,我该怎么做?