2016-05-29 3 views
0

プラグインを作成しました。プラグイン設定値をショートコード属性として挿入

プラグインの設定の1つは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は、この名前で、今のショートコードを使用することができます。 wordpress shortcode api

EDIT

[shortcodeName] 

は、パラメータの取り扱いに関する詳細は、ワードプレスのドキュメントのリンクを参照してくださいああ申し訳ありませんが、私はポイントビットを逃したと思います。あなたができることは、プラグインのURL設定を クッキーに保存し、ショートコードのクッキー値を確認することです。

0

ショートコードがurl属性でない場合、ショートコードの戻り値出力にデフォルト設定の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>'; 
} 

すでにその後、ショートコールバック関数を見つけて、このようにATTSを変更するプラグインやテーマでのショートを構築する場合、このよう

第三者のショートコードコールバック名が見つからない場合は、プラグインファイルにチェックインして、登録されているすべてのショートコードをコールバックでチェックしてください。

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 

)*/ 

  1. プラグインで、あなたがthiredパーティーショートの変更をしたいと管理しない場合は削除ショートは、以前thiredパーティのプラグインを宣言し、ファイルのプラグインの一番上に追加します。

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

  2. 削除後に同じショートタグを再作成しますが、独自のコールバック名を使用して、この

    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. 
    } 
    
ようthiredパーティのショートと同じ働き
関連する問題