Nhựt (WordPress Developer)

Nhựt
1 năm trước

WooCommerce: Thêm nút chia sẻ Twitter và Facebook Like không dùng plugin

Có rất nhiều plugin hỗ trợ việc hiển thị nút chia sẻ lên mạng xã hội, nhưng nếu ban chỉ muốn hiển thị chia sẻ Twitter và Facebook Like, và không sử dụng WordPress Plugin thì đây là giải pháp dành cho bạn.

Thêm nút chia sẻ Twitter và Facebook Like không dùng plugin

Lưu ý:

Thêm nút chia sẻ Twitter và Facebook Like không dùng plugin

/**
 * Social Media Buttons - WooCommerce Single Product Page
 * Twitter: https://publish.twitter.com/?buttonType=TweetButton&widget=Button
 * Facebook: https://developers.facebook.com/docs/plugins/like-button/
 */
 
add_action( 'woocommerce_single_product_summary', 'nhut_me_fb_twitter_single_product', 6 );
 
function nhut_me_fb_twitter_single_product() {
 
    echo '<div>';
 
    echo '<a href="https://twitter.com/share?ref_src=twsrc%5Etfw" class="twitter-share-button" data-hashtags="apieceofsicily" data-related="apieceofsicily" data-show-count="false">Tweet</a>'; // Tham khảo từ link Twitter docs
 
    echo '<div class="fb-like" data-href="' . get_permalink() . '" data-width="" data-layout="button_count" data-action="like" data-size="small" data-share="true"></div>'; // Tham khảo từ link Facebook docs, nhưng giữ lại get_permalink()
 
    echo '</div>';
}
 
add_action( 'wp_footer', 'nhut_me_twitter_facebook_js', 9999 );
 
function nhut_me_twitter_facebook_js() {
 
    if ( is_product() ) {
 
        echo '<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>'; // Tham khảo từ link Twitter docs
 
        echo '<div id="fb-root"></div><script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v6.0"></script>'; // Tham khảo từ link Facebook docs
 
    }
}