2016-08-26 4 views
0

私はカスタムペイパルボタンを生成するカスタムショートコードを使用しています&は、特定のパラメータをpaypalチェックアウトフォームに渡します。str_replaceでif/else文を使用してショートコードで変数を追加する

私はstr_replace関数を使用して、ショートコードで定義された属性をHTMLフォームに渡しています。

ショートコードの属性として定義されていない場合、既定のスタイルクラス&の既定のタイトルを設定したいと思います。

マイPHP:

function paypal_button_func($attrs){ 


$class=$attrs['class']; //Added button style class variable 
$title=$attrs['title']; //Added button title variable 



if(isset($atts['title'])) //add button title to the form html 
{ 
return $html=str_replace('[title]',$title,$html); 
} 

//This sets the default title if not defined 
else { 
    return $html=str_replace ('[title]','SIGN UP NOW',$html); 
} 

そして、これは私のhtml

<input type="submit" class="[class]" value="[title]" /> <!-- updated class and value for addtl shortcode parameters --> 

私はショートがHTMLに属性を渡すために、次のコードを使用してして成功したが、問題は、もしあります属性は短コードで定義されていないので、[クラス]と[タイトル]を値として出力しますが、これは起こりたいものではありません。

$html=str_replace('[title]',$title,$html); //add button title assigned to the form  
$html=str_replace('[class]',$class,$html); //replaces class assigned to the form 

答えて

1

単に、私はあなたのVARSを作成する際のif /他の速記を使用することをお勧め正しく交換を初期化することを確認してください:

$class = isset($attrs['class']) ? $attrs['class'] : 'default-class-name'; //Added button style class variable 
$title = isset($attrs['title']) ? $attrs['title'] : 'SIGN UP NOW'; //Added button title variable 

あなたのhtmlは、常に交換を取得します。この方法で、どちらかattrsに値をまたはデフォルト値。

も私はあなたがおそらく$attrsなく$atts

幸運をしたいisset($atts['title'])にチェックタイプミスを持って気づきました!

+0

その短い手は完全に働いた。最後の何時間も私の画面を見ていて、それは私が過ぎて間違っていた。助けてくれてありがとう!!! –

関連する問題