Học WordPress + AI / WordPress cho người VIỆT !
WPShare247

Học wordpress cho người mới bắt đầu

  • Bắt đầu
    • Học WordPress
    • Elementor
    • WooCommerce
    • Tạo WordPress
    • Học WordPress kiểu mới 2025
    • Học PHP cơ bản
    • Tạo Website bằng WordPress
    • Quản trị WordPress
    • Sửa lỗi WordPress
    • Theme wordpress
    • Plugin WordPress
    • Hàm PHP
    • Seo WordPress
    • Bảo mật WordPress
    • Html & Css
    • Javascript và jQuery
    • Hướng dẫn lập trình
  • Video
    • Video TikTok
    • Video Shorts
  • Kho plugin
  • Kho Prompt
  • Tải Web Miễn Phí
  • Công cụ
    • Kiểm tra web WordPress
    • WordPress theme gì?
    • Check IP Website
    • Kiểm tra Tên Miền
    • Kiểm Tra SSL
    • Tạo File Disavow Google
    • Tạo QR Code
  • Giới thiệu
    • Khuyến Mãi
  • Đăng nhập
X
☰

Trang chủ » Hướng dẫn xử lý AJAX trong WordPress cho người mới bắt đầu – WordPress Tutorial

Hướng dẫn xử lý AJAX trong WordPress cho người mới bắt đầu – WordPress Tutorial

Ngày đăng: Xem: 831
Không biết code vẫn tự Thiết Kế Website bằng AI chuẩn chỉ trong 30 phút, Claude có tốt hơn ChatGPT? 09/08/2026 Xem: 1

Không biết code vẫn tự Thiết Kế Website bằng AI chuẩn chỉ trong 30 phút, Claude có tốt hơn ChatGPT?

Chuyển Website từ Localhost XAMPP lên Hosting mà không lỗi Database 09/08/2026 Xem: 1

Chuyển Website từ Localhost XAMPP lên Hosting mà không lỗi Database

Fix lỗi WordPress 7.x không tích hợp được AI KEY: Anthropic, Google, OpenAI 28/05/2026 Xem: 94

Fix lỗi WordPress 7.x không tích hợp được AI KEY: Anthropic, Google, OpenAI

Full code hướng dẫn trong VIDEO

Rê vào vùng code rồi chọn Copy

add_filter('the_content', 'wpshare247_add_like_button', 1);
function wpshare247_add_like_button($content){
    $post_id = get_the_ID();
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            jQuery(".wpshare247-like").click(function(e) {
                var wp_ajax_url = '<?php echo admin_url( 'admin-ajax.php' ); ?>';

                jQuery.ajax({
                    url: wp_ajax_url,
                    type: 'POST',
                    data:  {
                                action: "wpshare247_add_like_button",
                                post_id: <?php echo $post_id; ?>
                            },
                    dataType: 'json',
                    success: function(data, textStatus, jqXHR){ 
                        console.log(data); //return false;
                        var post_like = data.post_like;
                        jQuery(".wpshare247-like span").text(post_like);

                    },
                    error: function(jqXHR, textStatus, errorThrown){
                    
                    }          
                });

                return false;
            });
        });
    </script>
    <?php
    $post_like = get_post_meta( $post_id, 'wpshare247_post_like', true );;

    $btn_html = '<div id="wpshare247-buttons"><a class="wpshare247-like" href="#">Thích (<span>'.$post_like.'</span>)</a></div>';

    $new_content = $content . $btn_html;

    return $new_content;
}


//------------- Ajax wpshare247_action_load_more --------------------------
function wpshare247_add_like_button_display(){
    header("Content-Type: application/json", true);
    
    $arr_response = array();
    
    //_REQUEST - 1 => Nhận dữ liệu từ Ajax Javascript
    $post_id = $_REQUEST['post_id'];


    //_ACTION - 2 => Xử lý tại đây
    $post_like = get_post_meta( $post_id, 'wpshare247_post_like', true );
    $post_like++;

    update_post_meta( $post_id, 'wpshare247_post_like', $post_like );

    //_RESPONSE - 3 => Trả kết quả về cho Ajax Javascript
    
    $arr_response = array(
                            'post_like' => $post_like
                        );
    wp_send_json($arr_response);
    die();
    
}
add_action( 'wp_ajax_wpshare247_add_like_button', 'wpshare247_add_like_button_display' );
add_action('wp_ajax_nopriv_wpshare247_add_like_button', 'wpshare247_add_like_button_display');

 

Shares
ChatGPT ChatGPT
Share
Share
Pin
Về WPShare247

Học wordpress cho người mới bắt đầu.

WPShare247 là blog chia sẻ các kiến thức về WordPress, sửa chữa lỗi wordpress, giới thiệu theme và plugin wordpress mới nhất. Đào tạo khóa học thiết kế Website bằng WordPress.

Menu
  • Trang chủ
  • Giới thiệu
  • Blog
  • Plugin WordPress
  • Liên hệ
Chủ đề wordpress
  • Sửa lỗi WordPress
  • Plugin WordPress
  • Theme wordpress
  • Seo WordPress
  • WooCommerce

Copyright © 2026 WPSHARE247. All Rights Reserved.
Một sản phẩm của WEB 366

  • Thiết kế web bởi Website366.com
  • Thiết kế website chuyên nghiệp TBAY.VN
  • Thiết kế WEB AI