web-dev-qa-db-ja.com

ショートコード属性のデフォルト

私は自分自身のショートコードを実装するためにこの構造を使います:

function column_with_icon($atts, $content = null) {
    extract(shortcode_atts(array(
        'icon' => 'onebit_03'
    ), $atts));

    return '<div class="column3">
    <img class="features-icon" alt="icon" src="' . get_bloginfo("template_url") . '/images/icons/' . $icon . '.png"/>
                            <div class="feature-content">' . $content . $icon . '</div>';
}

add_shortcode('column-icon', 'column_with_icon');

私はこのようなコンテンツを追加しようとすると:

[column-icon icon="onebit_13"]
<h3>What is Lorem ipsum?</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<a href="#" class="main-theme-link">Learn more</a></div>
[/column-icon]

'icon'属性のデフォルト値は上書きされず、 'onebit_13'ではなく 'onebit_03'と等しくなります。これについて何ができますか?

UPDATEこれが、データを取得して表示するためのコードです。

function skible_load_special_offer_section() {
    $content = get_option("prc_speciall_offer_text", true);
    if (get_option("prc_show_special_offer_section")) {
        echo stripslashes(do_shortcode('<!--Start Special offer (in border) content-->
                    <div class="border-top"></div>
                    <div class="border-middle">
                        <div class="special-offer-area">
                            <div class="special-offer-image-left">
                                <img alt="image" src="' . get_bloginfo("template_url") . ' /colors/magic_night/special-offer-image.png"/>
                            </div>
                            <div class="special-offer-content-right">' . $content . '</div>
            <div class = "clear"></div>
            </div>
            </div>
            <div class = "border-bottom"></div>
            <!--End Special offer (in border) content-->'));
    }
}
1
Sergei Basharov

私はあなたの設定で何が悪くなるのかわかりません。明らかにショートコードは正しく登録されていますが、特定のケースでは正しく解析されないことがあります。

これは、ショートコードがどのように実行のために解析されるかをおおまかに再現するテストコードです。

$content = get_option("prc_speciall_offer_text", true);
$pattern = '/'.get_shortcode_regex().'/s';
preg_replace_callback( $pattern, 'var_dump', $content );

配列出力は、get_shortcode_regex()仕様に従って、以下のものと一致する必要があります。

1/6 - ダブル[[]]を使ってショートコードをエスケープするための追加の[or]

2 - ショートコード名

3 - ショートコード引数リスト

4 - 自己閉鎖/

5 - 内容を折り返すときのショートコードの内容。

これはそれが私のためにダンプするものです:

array
  0 => string '[column-icon icon="onebit_13"]
<h3>What is Lorem ipsum?</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<a href="#" class="main-theme-link">Learn more</a></div>
[/column-icon]' (length=389)
  1 => string '' (length=0)
  2 => string 'column-icon' (length=11)
  3 => string ' icon="onebit_13"' (length=17)
  4 => string '' (length=0)
  5 => string '
<h3>What is Lorem ipsum?</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<a href="#" class="main-theme-link">Learn more</a></div>
' (length=345)
  6 => string '' (length=0)
1
Rarst