2016-05-29 49 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. 
    } 
    
工作同