当shortcode没有属性url时,您可以在shortcode返回输出中提供默认设置url。
像这样
add_shortcode('button', 'shortcode_function_button');
function shortcode_function_button($atts){
$attr = shortcode_atts(array(
'url' => get_option('button_default_url') , // get your setting default url if shortcode have not
), $atts); // attr url="http://example.com" then it use default
// setting url
return '<a href="'.esc_url($attr['url']).'" class="button">Button</a>';
}
如果已经建立插件或主题简码,然后找到简码回调函数,改变ATT以这种方式。
如果在查找第三方短代码回调名称时遇到问题,请在您的插件文件中检查所有带回调的注册短代码。
global $shortcode_tags;
print_r($shortcode_tags);
// show all shortcodes with callback
/*Array
(
[embed] => __return_false
[wp_caption] => img_caption_shortcode
[caption] => img_caption_shortcode
[gallery] => gallery_shortcode
[playlist] => wp_playlist_shortcode
[audio] => wp_audio_shortcode
[video] => wp_video_shortcode
[button] => button_shortcode
)*/
如果你不想在thired方简码的任何变化,并在你的插件管理
删除简码先前声明thired党的插件,并添加文件的插件顶部。
remove_shortcode('button');
// https://developer.wordpress.org/reference/functions/remove_shortcode/
重新创建相同的简码标签之后删除,但用自己的回调名和thired党的简码像这样
add_shortcode('button', 'your_own_callback');
function your_own_callback($atts, $content){
$attr = shortcode_atts(array(
'url' => get_option('button_default_url') , // Use same atts thired party using atts
), $atts);
return button_shortcode($attr, $content); // Use thired party callback function name.
}
工作同