Wordpress 短代码单引号和双引号问题
Wordpress Shortcodes Single & Double Quotes Issue
我在 Wordpress Gutenberg 编辑器中使用自定义 HTML 块添加我的短代码。
[tagged_heading heading_tag="h2" description="Lorem ipsum <span data-attribute='1'></span> sodales dui."]
更新页面后一切正常,但在重新加载编辑器后 window 所有单引号,例如 data-attribute
中,更改为双引号。我该如何预防?
我建议更改您的短代码以处理类似这样的事情...
[tagged_heading heading_tag="h2"]
Lorem ipsum <span data-attribute="1"></span> sodales dui.
[/tagged_heading]
您的 PHP 代码可能如下所示:
add_shortcode( 'tagged_heading', function( $attributes, $content, $tag ) {
// Merge default attributes
extract( shortcode_atts( [
'heading_tag' => 'h1'
], $attributes ) );
// Render.
echo '<' . $heading_tag . '>' . $content . '</' . $heading_tag . '>';
} );
找到了适合我的快速修复解决方案。如果单引号放在第一层,单引号内的双引号将保持不变。所以只需交换它们,例如[tagged_heading text='Lorem ipsum dolor sit amet <mark data-tooltip="tooltip"></mark> sodales dui.']
我在 Wordpress Gutenberg 编辑器中使用自定义 HTML 块添加我的短代码。
[tagged_heading heading_tag="h2" description="Lorem ipsum <span data-attribute='1'></span> sodales dui."]
更新页面后一切正常,但在重新加载编辑器后 window 所有单引号,例如 data-attribute
中,更改为双引号。我该如何预防?
我建议更改您的短代码以处理类似这样的事情...
[tagged_heading heading_tag="h2"]
Lorem ipsum <span data-attribute="1"></span> sodales dui.
[/tagged_heading]
您的 PHP 代码可能如下所示:
add_shortcode( 'tagged_heading', function( $attributes, $content, $tag ) {
// Merge default attributes
extract( shortcode_atts( [
'heading_tag' => 'h1'
], $attributes ) );
// Render.
echo '<' . $heading_tag . '>' . $content . '</' . $heading_tag . '>';
} );
找到了适合我的快速修复解决方案。如果单引号放在第一层,单引号内的双引号将保持不变。所以只需交换它们,例如[tagged_heading text='Lorem ipsum dolor sit amet <mark data-tooltip="tooltip"></mark> sodales dui.']