問題描述
我建立了一個外掛來在我的 WordPress 網站新增一些短碼。但我是一個 PHP 新手,所以我相信它可能有一些錯誤或方法來最佳化它。
工作正常,顯然沒有問題。記憶體使用量為 00.04MByte 。請你看看嗎非常感謝你!
add_action( 'wp_enqueue_scripts', 'prefix_add_my_stylesheet' );
function prefix_add_my_stylesheet() {
// Respects SSL, Style.css is relative to the current file
wp_register_style( 'prefix-style', plugins_url('style.css', __FILE__) );
wp_enqueue_style( 'prefix-style' );
}
/* Adsesnse Shortcode */
function get_adsense($atts) {
return 'THE_ADSENSE_SCRIPT_GOES_HERE
';
}
add_shortcode('adsense', 'get_adsense');
/*Padding Shortcodes*/
function get_pad5($atts) {
return '<div class="pad5"></div>';
}
add_shortcode('pad5', 'get_pad5');
function get_pad10($atts) {
return '<div class="pad10"></div>';
}
add_shortcode('pad10', 'get_pad10');
function get_pad20($atts) {
return '<div class="pad20"></div>';
}
add_shortcode('pad20', 'get_pad20');
function get_pad25($atts) {
return '<div class="pad25"></div>';
}
add_shortcode('pad25', 'get_pad25');
function get_pad50($atts) {
return '<div class="pad50"></div>';
}
add_shortcode('pad50', 'get_pad50');
function get_pad75($atts) {
return '<div class="pad75"></div>';
}
add_shortcode('pad75', 'get_pad75');
function get_pad100($atts) {
return '<div class="pad100"></div>';
}
add_shortcode('pad100', 'get_pad100');
function get_clr($atts, $content=null) {
return '<div class="clr">' . $content . '</div>';
}
add_shortcode( 'clr', 'get_clr' );
/* Alert Boxes */
function get_alertblue($atts, $content=null) {
return '<div class="ultra_alert_box ultra_box-blue">' . $content . '</div>';
}
add_shortcode( 'alertblue', 'get_alertblue' );
function get_alertgreen($atts, $content=null) {
return '<div class="ultra_alert_box ultra_box-green">' . $content . '</div>';
}
add_shortcode( 'alertgreen', 'get_alertgreen' );
function get_alertyellow($atts, $content=null) {
return '<div class="ultra_alert_box ultra_box-yellow">' . $content . '</div>';
}
add_shortcode( 'alertyellow', 'get_alertyellow' );
function get_alertred($atts, $content=null) {
return '<div class="ultra_alert_box ultra_box-red">' . $content . '</div>';
}
add_shortcode( 'alertred', 'get_alertred' );
function get_alertgray($atts, $content=null) {
return '<div class="ultra_alert_box ultra_box-gray">' . $content . '</div>';
}
add_shortcode( 'alertgray', 'get_alertgray' );
/* Style Boxes */
function get_stylegreen($atts, $content=null) {
return '<div class="ultra_style_box ultra_style-green">' . $content . '</div>';
}
add_shortcode( 'stylegreen', 'get_stylegreen' );
function get_styleblue($atts, $content=null) {
return '<div class="ultra_style_box ultra_style-blue">' . $content . '</div>';
}
add_shortcode( 'styleblue', 'get_styleblue' );
function get_styleyellow($atts, $content=null) {
return '<div class="ultra_style_box ultra_style-yellow">' . $content . '</div>';
}
add_shortcode( 'styleyellow', 'get_styleyellow' );
function get_stylered($atts, $content=null) {
return '<div class="ultra_style_box ultra_style-red">' . $content . '</div>';
}
add_shortcode( 'stylered', 'get_stylered' );
function get_stylegray($atts, $content=null) {
return '<div class="ultra_style_box ultra_style-gray">' . $content . '</div>';
}
add_shortcode( 'stylegray', 'get_stylegray' );
function get_stylewhite($atts, $content=null) {
return '<div class="ultra_style_box ultra_style-white">' . $content . '</div>';
}
add_shortcode( 'stylewhite', 'get_stylewhite' );
最佳解決方案
有一個非常重要的程式設計原則:DRY – 不要重複你自己。每當你意識到你正在重複幾乎相同的工作,試圖寫一個抽象。
對於您的 pad*短碼,這意味著:
function get_padding( $atts )
{
$args = shortcode_atts(
array( 'num' => 10 ),
$atts
);
return str_repeat( ' ', (int) $args['num'] );
}
add_shortcode('pad', 'get_padding');
現在你可以使用這個短碼 [pad]:
[pad num=5]
[pad num=10]
[pad num=12000]
[pad num=18]
優點是:如果你決定改變你的短碼的內在邏輯,那麼你只需觸控一個功能,而不是十幾個。
另見 this answer 。
還有另一種可能性:對於多個短碼使用一個回撥。這使用 the third parameter 每個短程式碼回撥獲取。
function multipass( $atts, $content = '', $shortcode = '' )
{
$args = shortcode_atts(
array (
'name' => 'example'
),
$atts
);
$name = esc_attr( $args['name'] );
$content = esc_textarea( $content );
if ( 'textarea' === $shortcode )
return "<textarea name='$name'>$content</textarea>";
if ( 'input' === $shortcode )
return "<input name='$name' value='$content' />";
}
add_shortcode( 'textarea', 'multipass' );
add_shortcode( 'input', 'multipass' );
Related
-
Organizing shortcodes. How to display all of them and their attributes?
-
What characters are allowed as a shortcode tag and how should they be sanitized?
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。