2016-05-29 47 views
0

我写了一个插件。插入插件设置值作为简码属性

插件的一个设置是URL /页面。

我希望我的客户端能够继续使用主题的页面构建器,以创建任何按钮,这些按钮将链接到在插件设置中输入的URL。

客户端可以为他们创建的每个按钮手动输入URL,但这会很乏味,如果插件设置中的URL发生更改,将会变得非常痛苦。

所以,我希望能够使用插件的URL设置值作为第三方按钮短代码的URL属性。

这可能吗?例如:

[button url="{get the plugin's URL setting}"][/button] 

回答

0

绝对有可能。

您需要将短代码功能添加到您的插件中。最好使用唯一的名称来避免冲突(不是'按钮')。 在你的插件PHP文件添加功能并注册其与add_shortcode功能,像这样:

function shortcodeName_function($atts) { 
    // add attribute handling and get the 'url' parameter 
    $output = '<button a href="'. $url . '" class="button">'; 
    return $output; 
} 


add_shortcode('shortcodeName', 'shortcodeName_function'); 

现在u可以使用现在的短代码,这个名字,当插件被激活。

[shortcodeName] 

见WordPress的文件以获得更多信息的参数处理的链接:wordpress shortcode api

编辑:不好意思啊,我想我错过了点位。您可以做的是 - 将插件url设置存储在Cookie中,并检查短代码中的cookie值。

0

当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方简码的任何变化,并在你的插件管理

  1. 删除简码先前声明thired党的插件,并添加文件的插件顶部。

    remove_shortcode('button'); // https://developer.wordpress.org/reference/functions/remove_shortcode/

  2. 重新创建相同的简码标签之后删除,但用自己的回调名和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. 
    } 
    
工作同