Trang chủ Blog Website Share Code Thêm nút like và share cho theme wordpress

Thêm nút like và share cho theme wordpress

Tạo nút like share cho website wordpress
Tạo nút like share cho website wordpress
Tạo nút like share cho website wordpress

Hello mọi người. Nay share đoạn code giúp tạo nút like và share lên facebook cho website wordpress.

Facebook đã và đang là nền tảng được rất nhiều người sử dụng. Từ trẻ em cho tới người già :v.

Luyên thuyên vài câu cho bài nó có chữ.

Oke zô thẳng code.

Tạo 2 nút like share trên facebook

Bỏ code bên dưới vào function.php

add_action( 'woocommerce_before_add_to_cart_quantity', 'isures_likeshare_btn_func' );
 
function isures_likeshare_btn_func() {
 
   echo '<div class="isures-btn--like_wrap">';

   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>';
 
   echo '</div>';
}
 

Ở trên mình sử dụng hook woocommerce_before_add_to_cart_quantity nên vị trí hiển thị của 2 nút này sẽ nằm trước quantity add to cart button.

Nếu mọi người không thích có thể tạo thành shortcode = cách thay thế dòng 1 ở trên bằng

add_shortcode( 'isures_likeshare', 'isures_likeshare_btn_func' );

và sử dụng shortcode [isures_likeshare] đặt ở nơi nào bạn muốn.

Ngoài ra có thể tham khảo các hook khác tại trang chi tiết sản phẩm ở đây

Thêm mã script của facebook vào footer

Chèn code dưới vào function.php

add_action( 'wp_footer', 'isures_likeshare_reg_script', 9999 );
 
function isures_likeshare_reg_script() {
 
   if ( is_product() ) {
 
      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>';
 
   }
}

Ok như thế là xong rồi. chúc mọi người thành công. <3

5/5 - (100 bình chọn)
Bài viết liên quan