Làm cách nào để sử dụng AJAX trong WordPress từng bước?

Ajax đã nhanh chóng trở thành một công nghệ web phổ biến, bạn sẽ thấy nó được sử dụng trên hầu hết các trang web. Tính năng chính của Ajax là nó có thể quản lý các hoạt động của cơ sở dữ liệu mà không cần tải lại trang web. Điều này có nghĩa là bạn có thể lấy dữ liệu từ cơ sở dữ liệu và hiển thị nó trên giao diện người dùng mà không cần phải làm mới trang

Đó là một cách nhanh chóng và mượt mà để hiển thị nội dung và kết quả là Ajax hiện được sử dụng theo nhiều cách khác nhau trên trang web, chẳng hạn như gửi nhận xét blog, thích bài đăng và tải tệp lên. Bạn thậm chí có thể làm cho trang web của mình được Ajaxified hoàn toàn, để mỗi trang trên trang web của bạn sẽ tải không đồng bộ

Với sự phổ biến của Ajax, hầu hết các nền tảng CMS hàng đầu đều sử dụng nó trong kiến ​​trúc của họ. WordPress cũng không khác. Trên thực tế, WordPress sử dụng Ajax theo cách rất mạnh mẽ và dễ dàng, và hôm nay tôi sẽ chỉ cho bạn cách bạn có thể sử dụng Ajax trong WordPress bằng một ví dụ thực tế. Trước khi chúng ta bắt đầu, tôi cho rằng bạn đã có một số kiến ​​thức về các phương thức jQuery Ajax và các hook WordPress, vì chúng ta sẽ cần cả hai

Nếu bạn đang tìm kiếm một số hướng dẫn phát triển WordPress cơ bản, hãy xem Phát triển plugin WordPress cho người mới bắt đầu

Read Me Later'; $content .= $html; } return $content; } 2, nơi dữ liệu sẽ được gửi để xử lý

  • Móc hành động Ajax được gọi là
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    3. Bạn cần nối một chức năng tùy chỉnh vào đó, chức năng này sẽ được thực thi trong lệnh gọi Ajax
  • Hãy tạo một plugin để hiểu cách thức hoạt động của nó. Giả sử chúng tôi muốn phát triển một plugin cho phép người dùng lưu các bài đăng trên blog yêu thích của họ ở một khu vực nhất định để họ có thể đọc chúng sau này. Tính năng này hữu ích cho các blog kiểu tạp chí cung cấp nhiều nội dung hàng ngày. Nó sẽ cho phép người dùng đã đăng nhập lưu các bài đăng thú vị trong khu vực chỉ dành cho thành viên để họ có thể quay lại và đọc chúng sau này

    Vì vậy, plugin 'Đọc tôi sau' của chúng tôi sẽ thực hiện một số việc

    • Đầu tiên, chúng tôi sẽ tạo một liên kết ở cuối mỗi nội dung bài đăng trên blog
    • Khi người dùng nhấp vào liên kết, ID của bài đăng của liên kết sẽ lưu trong bảng cơ sở dữ liệu usermeta mà không cần tải lại trang
    • Cuối cùng, chúng tôi sẽ tạo một tiện ích để hiển thị các bài đăng trên blog dựa trên ID bài đăng được lưu trong cơ sở dữ liệu

    Hiểu rồi?

    Chuẩn bị tệp plugin và thiết lập thư mục của chúng tôi

    Trước tiên, chúng tôi sẽ tạo một thư mục có tên là

    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    4 bên trong thư mục plugin của bản cài đặt WordPress chính của chúng tôi. Thư mục này sẽ chứa tất cả các tệp và thư mục con cần thiết cho plugin của chúng tôi. Bây giờ bên trong thư mục
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    4, chúng ta sẽ cần tạo hai thư mục khác có tên là
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    6 và
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    7

    Sau đó, chúng tôi cần tạo bốn tệp mà tôi đã liệt kê bên dưới với các phần mở rộng tệp thích hợp

    • /**
       * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
       *
       * @param string $content
       * @returns string
       */
      public function rml_button[ $content ] {   
          // Show read me later link only when the user is logged in
          if[ is_user_logged_in[] && get_post_type[] == post ] {
              $html .= 'Read Me Later';
              $content .= $html;
          }
          return $content;       
      }
      
      8
    • /**
       * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
       *
       * @param string $content
       * @returns string
       */
      public function rml_button[ $content ] {   
          // Show read me later link only when the user is logged in
          if[ is_user_logged_in[] && get_post_type[] == post ] {
              $html .= 'Read Me Later';
              $content .= $html;
          }
          return $content;       
      }
      
      9
    • // Setup filter hook to show Read Me Later link
      add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
      add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
      
      0
    • // Setup filter hook to show Read Me Later link
      add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
      add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
      
      1

    Hai tệp đầu tiên sẽ trực tiếp đi vào thư mục plugin chính. Các tệp

    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    6 và
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    7 sẽ được đặt lần lượt trong các thư mục
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    6 và
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    7

    Bây giờ chúng ta sẽ điền vào tệp

    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    8. Bao gồm tiêu đề plugin

    /**
     * Plugin Name: Read Me Later
     * Plugin URI: //github.com/iamsayed/read-me-later
     * Description: This plugin allow you to add blog posts in read me later lists using Ajax
     * Version: 1.0.0
     * Author: Sayed Rahman
     * Author URI: //github.com/iamsayed/
     * License: GPL3
     */
    

    Mã này rất quan trọng vì nó được sử dụng để xác định đó là một plugin cho WordPress. Sau đoạn mã trên, chúng ta sẽ tạo lớp plugin chính có tên là

    // Setup filter hook to show Read Me Later link
    add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
    add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
    
    7

    // Setup filter hook to show Read Me Later link
    add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
    add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
    
    8

    Bao gồm các tệp JS và CSS cần thiết

    Tiếp theo, chúng tôi cần đăng ký và liệt kê các tệp JavaScript và CSS của mình bằng các móc nối WordPress thích hợp. Chúng tôi sẽ tạo một số phương thức để thực hiện bước này. Sao chép mã này vào lớp

    // Setup filter hook to show Read Me Later link
    add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
    add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
    
    7

    /*
     * Action hooks
     */
    public function run[] {     
        // Enqueue plugin styles and scripts
        add_action[ ‘plugins_loaded’, array[ $this, 'enqueue_rml_scripts' ] ];
        add_action[ ‘plugins_loaded’, array[ $this, 'enqueue_rml_styles' ] ];      
    }   
    /**
     * Register plugin styles and scripts
     */
    public function register_rml_scripts[] {
        wp_register_script[ 'rml-script', plugins_url[ 'js/read-me-later.js', __FILE__ ], array['jquery'], null, true ];
        wp_register_style[ 'rml-style', plugins_url[ 'css/read-me-later.css' ] ];
    }   
    /**
     * Enqueues plugin-specific scripts.
     */
    public function enqueue_rml_scripts[] {        
        wp_enqueue_script[ 'rml-script' ];
    }   
    /**
     * Enqueues plugin-specific styles.
     */
    public function enqueue_rml_styles[] {         
        wp_enqueue_style[ 'rml-style' ]; 
    }
    

    Mã này khá tự giải thích. Ở đây, chúng tôi đã tạo một phương thức công khai có tên là

    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    0. Trong phương pháp này, chúng tôi đã đăng ký các tệp
    // Setup filter hook to show Read Me Later link
    add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
    add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
    
    0 và
    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    2 bằng các chức năng WordPress thích hợp

    Hai phương pháp tiếp theo

    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    3 và
    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    4 được sử dụng để liệt kê JavaScript và biểu định kiểu của chúng tôi. Chúng ta cũng đã tạo một phương thức
    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    5, phương thức này sẽ chứa tất cả các hook hành động [và bộ lọc] của chúng ta

    Nếu bạn chưa quen với WordPress, bạn có thể xem Enqueuing Scripts and Styles in WordPress của Younes Rafie hoặc tìm kiếm WordPress codex để tìm hiểu cách đăng ký và xử lý các tệp JavaScript và CSS đúng cách

    Tạo liên kết đọc cho tôi sau bên dưới mỗi bài đăng

    Bây giờ chúng ta cần tạo một liên kết Read Me Later dưới mỗi bài đăng trên blog. Bằng cách nhấp vào liên kết, người dùng có thể chọn bài đăng đó sẽ được lưu trong danh sách 'Đọc cho tôi sau'. Sau khi họ nhấp vào, liên kết sẽ biến mất khỏi bài đăng và ID bài đăng sẽ được lưu trong cơ sở dữ liệu. Có hai cân nhắc khi chúng tôi tạo liên kết

    • Chỉ người dùng đã đăng nhập mới thấy liên kết
    • Liên kết sẽ chứa ID bài đăng 'có liên quan' để sử dụng sau này

    Để đạt được điều này, hãy thêm chức năng sau vào lớp

    // Setup filter hook to show Read Me Later link
    add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
    add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
    
    7

    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    

    Ở đây, chúng tôi đã kiểm tra cả xem người dùng đã đăng nhập chưa và loại bài đăng có phải là bài đăng không. Sau khi kiểm tra điều này, chúng tôi tạo liên kết. Lưu ý rằng chúng tôi sử dụng thuộc tính dữ liệu HTML5 để chứa ID của bài đăng blog có thể được truy xuất bằng chức năng

    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    7. Vì liên kết sẽ được đặt bên trong vòng lặp bài đăng, đây là chức năng chính xác mà chúng tôi cần

    Để đặt liên kết dưới mỗi bài đăng trên blog, hãy thêm mã dưới đây vào bên trong phương thức

    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    5

    ________số 8_______

    Điều này sẽ lọc đoạn trích bài đăng và đặt liên kết bên trong vòng lặp. Giờ đây, khi bạn đăng nhập vào trang web WordPress của mình và duyệt trang chủ [hoặc trang hiển thị bài đăng của bạn], bạn sẽ thấy liên kết ‘Đọc tôi sau’ ở cuối mỗi bài đăng

    Xác định URL Ajax

    Khi bạn định thực hiện lệnh gọi Ajax, bạn sẽ cần gửi yêu cầu tới tệp

    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    2, đây là một phần của lõi WordPress. Tệp này chịu trách nhiệm xử lý và xử lý tất cả các yêu cầu Ajax của bạn trong bối cảnh WordPress. KHÔNG sử dụng URL trực tiếp của đường dẫn tệp. Thay vào đó, hãy sử dụng
    jQuery[document].ready[ function[]{         
        jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
            e.preventDefault[];
            var rml_post_id = jQuery[this].data[ 'id' ];    
            jQuery.ajax[{
                url : readmelater_ajax.ajax_url,
                type : 'post',
                data : {
                    action : 'read_me_later',
                    post_id : rml_post_id
                },
                success : function[ response ] {
                    jQuery['.rml_contents'].html[response];
                }
            }];
            jQuery[this].hide[];            
        }];     
    }];
    
    0 sẽ xuất đúng URL. Vấn đề duy nhất khi làm điều này là bạn không thể đặt bất kỳ hàm PHP nào bên trong JavaScript. Vì vậy, chúng tôi cần một mẹo nhỏ, xem mã bên dưới

    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    

    Ở đây, chúng tôi sử dụng một chức năng gọi là

    jQuery[document].ready[ function[]{         
        jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
            e.preventDefault[];
            var rml_post_id = jQuery[this].data[ 'id' ];    
            jQuery.ajax[{
                url : readmelater_ajax.ajax_url,
                type : 'post',
                data : {
                    action : 'read_me_later',
                    post_id : rml_post_id
                },
                success : function[ response ] {
                    jQuery['.rml_contents'].html[response];
                }
            }];
            jQuery[this].hide[];            
        }];     
    }];
    
    1. Phải mất ba đối số

    1. jQuery[document].ready[ function[]{         
          jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
              e.preventDefault[];
              var rml_post_id = jQuery[this].data[ 'id' ];    
              jQuery.ajax[{
                  url : readmelater_ajax.ajax_url,
                  type : 'post',
                  data : {
                      action : 'read_me_later',
                      post_id : rml_post_id
                  },
                  success : function[ response ] {
                      jQuery['.rml_contents'].html[response];
                  }
              }];
              jQuery[this].hide[];            
          }];     
      }];
      
      2, trình xử lý đăng ký của tập lệnh
      // Setup filter hook to show Read Me Later link
      add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
      add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
      
      0
    2. Một chuỗi sẽ hoạt động như một đối tượng JavaScript
    3. Một mảng là dữ liệu thực tế mà chúng tôi muốn chuyển từ JavaScript của mình

    Vì vậy, nếu chúng ta viết

    jQuery[document].ready[ function[]{         
        jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
            e.preventDefault[];
            var rml_post_id = jQuery[this].data[ 'id' ];    
            jQuery.ajax[{
                url : readmelater_ajax.ajax_url,
                type : 'post',
                data : {
                    action : 'read_me_later',
                    post_id : rml_post_id
                },
                success : function[ response ] {
                    jQuery['.rml_contents'].html[response];
                }
            }];
            jQuery[this].hide[];            
        }];     
    }];
    
    4, nó sẽ xuất ra giá trị của
    jQuery[document].ready[ function[]{         
        jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
            e.preventDefault[];
            var rml_post_id = jQuery[this].data[ 'id' ];    
            jQuery.ajax[{
                url : readmelater_ajax.ajax_url,
                type : 'post',
                data : {
                    action : 'read_me_later',
                    post_id : rml_post_id
                },
                success : function[ response ] {
                    jQuery['.rml_contents'].html[response];
                }
            }];
            jQuery[this].hide[];            
        }];     
    }];
    
    0, nói cách khác, URL của tệp
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    2. Chúng tôi sẽ sử dụng nó trong phần JavaScript

    Đừng quên đặt đoạn mã trên bên trong phương thức

    wp_localize_script[ 'rml-script', 'readmelater_ajax', array[ 'ajax_url' => admin_url['admin-ajax.php']] ];
    
    3 mà chúng ta đã tạo trước đó

    Thêm JavaScript và cuộc gọi Ajax đầu tiên của bạn

    Bây giờ là lúc để tạo cuộc gọi Ajax của chúng tôi. Mở tệp

    // Setup filter hook to show Read Me Later link
    add_filter[ 'the_excerpt', array[ $this, 'rml_button' ] ];
    add_filter[ 'the_content', array[ $this, 'rml_button' ] ];
    
    0 từ thư mục
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    6 của chúng tôi. Thêm mã dưới đây

    jQuery[document].ready[ function[]{         
        jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
            e.preventDefault[];
            var rml_post_id = jQuery[this].data[ 'id' ];    
            jQuery.ajax[{
                url : readmelater_ajax.ajax_url,
                type : 'post',
                data : {
                    action : 'read_me_later',
                    post_id : rml_post_id
                },
                success : function[ response ] {
                    jQuery['.rml_contents'].html[response];
                }
            }];
            jQuery[this].hide[];            
        }];     
    }];
    

    Trong đoạn mã trên, chúng tôi đã tạo một chức năng sẽ được gọi khi người dùng nhấp vào liên kết 'Đọc tôi sau'. Bên trong chức năng này, chúng tôi lấy ID bài đăng bằng phương thức dữ liệu và lưu trữ nó vào biến 'rml_post_id'. Sau đó, chúng tôi đã thực hiện cuộc gọi Ajax của mình bằng cách sử dụng jQuery '$. phương thức ajax[]’. Phương thức này có một số thuộc tính như chúng tôi đã đề cập trước đó trong bài viết này. Hãy để tôi giải thích từng cái một

    jQuery[this].hide[];
    
    0 chứa URL của tệp
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    2. Hãy nhớ cách chúng tôi xác định
    jQuery[document].ready[ function[]{         
        jQuery['#content'].on['click', 'a.rml_bttn', function[e] { 
            e.preventDefault[];
            var rml_post_id = jQuery[this].data[ 'id' ];    
            jQuery.ajax[{
                url : readmelater_ajax.ajax_url,
                type : 'post',
                data : {
                    action : 'read_me_later',
                    post_id : rml_post_id
                },
                success : function[ response ] {
                    jQuery['.rml_contents'].html[response];
                }
            }];
            jQuery[this].hide[];            
        }];     
    }];
    
    4 trong bước trước? . Yêu cầu Ajax của chúng tôi sẽ được gửi đến đó để xử lý

    jQuery[this].hide[];
    
    3 cho biết liệu yêu cầu sẽ được gửi bằng phương thức HTTP
    jQuery[this].hide[];
    
    4 hay
    jQuery[this].hide[];
    
    5. Chúng tôi sử dụng phương pháp
    jQuery[this].hide[];
    
    5 ở đây, vì chúng tôi đặt nó là
    jQuery[this].hide[];
    
    7

    jQuery[this].hide[];
    
    8 chứa dữ liệu chúng tôi muốn gửi bằng lệnh gọi Ajax. Ở đây, dữ liệu của chúng tôi là một đối tượng dưới dạng các cặp khóa-giá trị.
    jQuery[this].hide[];
    
    9 chứa ID bài đăng và
    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    0 chứa
    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    1 là hậu tố của móc
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    3. Chúng tôi sẽ xác định hook hành động Ajax và chức năng gọi lại của nó trong bước tiếp theo

    Cái cuối cùng là

    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    3 chứa hàm ẩn danh. Nó sẽ kích hoạt khi cuộc gọi Ajax kết thúc

    Đảm bảo rằng liên kết

    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    4 của bạn được bọc bằng thẻ div có thuộc tính
    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    5
    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    6 nếu không thì jQuery sẽ không hoạt động

    Bây giờ chúng ta cần gỡ bỏ link

    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    7 ngay sau khi người dùng click vào để người dùng không thể lưu 2 lần 1 bài viết. Để đạt được điều này, chúng tôi đã thêm đoạn mã sau vào sau phương thức
    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    8

    jQuery[this].hide[];
    

    Thao tác này sẽ xóa liên kết 'Đọc tôi sau' khi người dùng nhấp vào liên kết đó

    Móc hành động Ajax

    Bây giờ cho phần quan trọng

    Cho đến nay, chúng tôi đã tạo liên kết

    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    7 và kết nối nó với Ajax. Nhưng liên kết chưa làm gì cả, vì chúng tôi chưa viết bất kỳ mã phía máy chủ nào để xử lý yêu cầu Ajax. Khi người dùng nhấp vào liên kết, chúng tôi cần lưu ID bài đăng đó vào cơ sở dữ liệu và sau đó hiển thị các bài đăng ở giao diện người dùng dựa trên thông tin cơ sở dữ liệu

    Để thực hiện loại xử lý phía máy chủ này, WordPress cung cấp cho chúng tôi hai hook hành động,

    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    0 và
    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    1. Cái đầu tiên sẽ chỉ hoạt động đối với người dùng đã đăng nhập và cái thứ hai sẽ hữu ích khi người dùng chưa đăng nhập. Vì plugin ví dụ của chúng tôi được thiết kế chỉ dành cho người dùng đã đăng nhập, nên chúng tôi sẽ sử dụng plugin đầu tiên. Lưu ý rằng
    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    2 là hậu tố của móc
    /**
     * Adds a read me later button at the bottom of each post excerpt that allows logged in users to save those posts in their read me later list.
     *
     * @param string $content
     * @returns string
     */
    public function rml_button[ $content ] {   
        // Show read me later link only when the user is logged in
        if[ is_user_logged_in[] && get_post_type[] == post ] {
            $html .= 'Read Me Later';
            $content .= $html;
        }
        return $content;       
    }
    
    3 và bạn có thể đặt tên cho nó theo ý muốn

    Thêm đoạn mã sau vào bên trong phương thức

    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    4

    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    

    Điều duy nhất bạn cần cẩn thận với đoạn mã trên là đảm bảo rằng hậu tố hook Ajax của bạn khớp với giá trị của thuộc tính

    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    0 trong phương thức
    // Setup Ajax action hook
    add_action[ 'wp_ajax_read_me_later', array[ $this, 'read_me_later' ] ];
    
    8 của bạn [đã xem ở bước trước]. Bạn có thể nhận thấy rằng chúng tôi đặt cùng một tên cho chức năng gọi lại để chúng tôi có thể nhớ nó dễ dàng. Bây giờ chúng tôi sẽ xác định chức năng gọi lại của chúng tôi

    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    

    Đoạn mã trên nên được đặt bên trong lớp plugin chính của chúng tôi. Hãy để tôi giải thích những gì tôi đã làm ở đây

    Đầu tiên, chúng tôi đã lưu trữ ID bài đăng trong biến

    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    7. Sau đó, chúng tôi đã khai báo một mảng trống có tên là
    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    8

    Sau đó, chúng tôi kiểm tra xem có trường nào có khóa

    public function read_me_later[] {
        $rml_post_id = $_POST['post_id']; 
        $echo = array[];       
        if[get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ] !== null ] {
            $value = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
        }
    
        if[ $value ] {
            $echo = $value;
            array_push[ $echo, $rml_post_id ];
        }
        else {
            $echo = array[ $rml_post_id ];
        }
    
        update_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', $echo ];
        $ids = get_user_meta[ wp_get_current_user[]->ID, 'rml_post_ids', true ];
    }
    
    9 trong bảng
    // Query read me later posts
    $args = array[ 
        'post_type' => 'post',
        'orderby' => 'DESC', 
        'posts_per_page' => -1, 
        'numberposts' => -1,
        'post__in' => $ids
    ];
    
    $rmlposts = get_posts[ $args ];
    if[ $ids ] :
        global $post;
        foreach [ $rmlposts as $post ] :
            setup_postdata[ $post ];
            $img = wp_get_attachment_image_src[ get_post_thumbnail_id[] ]; 
        ?>          
            

    Chủ Đề