- Xóa mọi ký tự shortcode từ chuỗi.
echo strip_shortcodes(get_the_content());
|
- Thực thi shortcode trong template.
<?php do_shortcode('[test_shortcode]'); ?>
|
- Kích hoạt shortcode trong widget text.
add_filter('widget_text', 'do_shortcode');
|
- Sửa đổi thuộc tính của mọi shortcode. Cú pháp: shortcode_atts_{shortcode_name} cho phép chúng ta thay đổi thuộc tính bằng cách chỉ định shortcode
Ví dụ sau dùng để sửa shortcode 'testfilter'.
add_filter('shortcode_atts_testfilter','filter_sc',10,3);
function filter_sc($out, $pairs, $atts){
if($out['age']>22) $out['name']='Khanh';
else $out['age'] = '21';
return $out;
}
|
Giải thích:
* $out: giá trị thuộc tính cuối cùng cho shortcode, là kết quả giữa thuộc tính mặc định và thuộc tính của người dùng.
* $pairs: giá trị thuộc tính mặc định của shortcode.
* $atts: giá trị thuộc tính cung cấp bởi người dùng.
Lưu ý: hãy chắc chắn bạn có gọi hàm 'shortcode_atts' trong hàm liên kết vào shortcode.
Xóa và tạo lại shortcode:
Để xóa một shortcode chúng ta có hàm remove_shortcode. ie:
remove_shortcode('jo_customers_testimonials_slider');
|
Bạn sẽ cần gọi hàm này sau khi đã duyệt theme, do đó chúng ta gọi trong hook 'wp_loaded'.
function overwrite_shortcode()
{
function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
$content = "";
$loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );
$postsLoop = new WP_Query( $loopArgs );
$content = "";
$content .= '...';
$content .= get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
$content .= '...';
$content .= '...';
wp_reset_query();
return $content;
}
remove_shortcode('jo_customers_testimonials_slider');
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );
}
add_action( 'wp_loaded', 'overwrite_shortcode' );
|
Hoặc sau hook 'after_setup_theme'.
add_action( 'after_setup_theme', 'calling_child_theme_setup' );
function calling_child_theme_setup() {
remove_shortcode( 'parent_shortcode_function' );
add_shortcode( 'shortcode_name', 'child_shortcode_function' );
}
function child_shortcode_function( $atts) {
...
}
|
Unregistered version