當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.
}
工作同