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

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

Chúng ta sẽ làm gì?

Bạn có thể đã biết rằng việc sử dụng Ajax trong WordPress hơi khác so với việc sử dụng nó bên ngoài WordPress, bạn sẽ cần xem xét hai điều

  1. URL của tệp WordPress
    /**
     * 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, nơi dữ liệu sẽ được gửi để xử lý
  2. 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: https://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: https://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() ); 
    ?>          
        

" class="rml_img">
0 trong cơ sở dữ liệu của chúng tôi không. Nếu có hàng, chúng tôi lấy giá trị meta bằng cách sử dụng hàm
// 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() ); 
    ?>          
        

" class="rml_img">
1 WordPress và lưu trữ nó trong
// 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() ); 
    ?>          
        

" class="rml_img">
2

Một lần nữa, chúng tôi kiểm tra xem

// 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() ); 
    ?>          
        

" class="rml_img">
2 có tồn tại hay không. Nếu đúng, chúng tôi lưu trữ nó trong mả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 );
}
8 đã khai báo trước đó. Sau đó, chúng tôi đẩy giá trị củ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 );
}
7 vào bên trong mảng bằng cách sử dụng hàm
// 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() ); 
    ?>          
        

" class="rml_img">
6. Nếu không có
// 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() ); 
    ?>          
        

" class="rml_img">
2, thì chúng ta chỉ cần lưu trữ
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 trong
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

/*
 * 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' ); 
}
00 chịu trách nhiệm cập nhật (hoặc tạo, nếu trường chưa được tạo) trường meta với dữ liệu được lưu trữ trong
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

Cuối cùng, chúng tôi lưu trữ trường meta được điền gần đây bằng cách sử dụ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() ); 
    ?>          
        

" class="rml_img">
1 trong
/*
 * 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' ); 
}
03 dưới dạng một mảng

Bây giờ chúng tôi đã có ID bài đăng do người dùng chọn, chúng tôi cần hiển thị các bài đăng đó. Thêm đoạn mã sau

// 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() ); 
    ?>          
        

" class="rml_img">

Ở đây, chúng tôi sử dụng chức năng WordPress

/*
 * 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' ); 
}
04 để lấy tất cả các bài đăng dựa trên sự lựa chọn của người dùng. Tham số bắt buộc duy nhất ở đây là
/*
 * 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' ); 
}
05 chứa mảng ID bài đăng. Cuối cùng, chúng tôi sử dụng
/*
 * 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' ); 
}
06 để nội dung Ajax của chúng tôi sẽ lặp lại chính xác

Đây là mã đầy đủ của hàm

/*
 * 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' ); 
}
07

/*
 * 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' ); 
}
0

Tạo một Widget để đọc cho tôi sau bài viết

Bây giờ chúng tôi cần một tiện ích để hiển thị các bài đăng được lưu bởi người dùng. Chúng tôi sẽ tạo một tiện ích rất cơ bản vì mục đích đơn giản. Tôi sẽ không đi vào chi tiết đầy đủ ở đây, chúng ta chỉ cần mở rộng lớp

/*
 * 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' ); 
}
08 của WordPress để tạo một tiện ích con tùy chỉnh. Hãy làm điều đó, mở 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;       
}
9 và tạo một lớp con có tên là
/*
 * 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' ); 
}
10 mở rộng lớp
/*
 * 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' ); 
}
08

/*
 * 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' ); 
}
1

Tạo phương thức ma thuật

/*
 * 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' ); 
}
12 bên trong lớp để khởi tạo widget của chúng ta

/*
 * 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' ); 
}
2

Ở đây, chúng tôi đã đặt tên và mô tả cho tiện ích sẽ hiển thị trong phần tiện ích bảng điều khiển

Một biểu mẫu tiện ích phụ trợ sẽ được tạo theo phương thức

/*
 * 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' ); 
}
13, như thế này

/*
 * 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' ); 
}
3

Như bạn có thể thấy, biểu mẫu của chúng tôi bao gồm một trường văn bản chứa tiêu đề của tiện ích con. Chúng tôi gán tiêu đề của mình trong biến

/*
 * 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' ); 
}
14.
/*
 * 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' ); 
}
15 và
/*
 * 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' ); 
}
16 lần lượt cung cấp cho trường văn bản của chúng ta một ID và tên duy nhất

Phương thức

/*
 * 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' ); 
}
17 chịu trách nhiệm làm sạch và cập nhật giá trị đầu vào của người dùng

/*
 * 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' ); 
}
4

Phải mất hai tham số

  1. /*
     * 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' ); 
    }
    
    18 chứa giá trị do người dùng nhập bằng biểu mẫu phụ trợ mà chúng tôi đã tạo bằng phương thức
    /*
     * 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' ); 
    }
    
    13
  2. /*
     * 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' ); 
    }
    
    20 ngược lại, nó chứa giá trị trước đó

Bây giờ chúng ta sẽ tạo phương thức

/*
 * 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' ); 
}
21 sẽ hiển thị các bài đăng 'Đọc cho tôi sau' ở giao diện người dùng

/*
 * 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' ); 
}
5

Ở đây chúng ta sử dụng hàm

/*
 * 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' ); 
}
04 để hiển thị bài viết. Khá giống với phương pháp
/*
 * 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' ); 
}
07

Đừng quên bao gồm 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;       
}
9 bằng cách thêm đoạn mã sau vào đầu 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

/*
 * 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' ); 
}
6

Bảo mật các cuộc gọi Ajax của bạn

Trong khi làm việc với Ajax, bạn nên thực hiện các bước cần thiết để đảm bảo an toàn cho mã của mình. Nếu bạn sắp nhận bất kỳ dữ liệu nào từ người dùng, hãy khử trùng dữ liệu đó trước khi lưu vào cơ sở dữ liệu. Sử dụng

/*
 * 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' ); 
}
26 để kiểm tra xem yêu cầu có đến từ đúng vị trí và được thực hiện bởi người dùng được xác thực hay không. Ở đây tôi sẽ chỉ cho bạn cách sử dụng WordPress
/*
 * 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' ); 
}
26 trong cuộc gọi Ajax

Đầu tiên, chúng ta sẽ tạo một nonce bằng cách sử dụng phương thức

/*
 * 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' ); 
}
28 và chuyển nó từ JavaScript. Để đạt được điều này, hãy sử dụng mã từ phương pháp
wp_localize_script( 'rml-script', 'readmelater_ajax', array( 'ajax_url' => admin_url('admin-ajax.php')) );
3

/*
 * 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' ); 
}
7

Và thay thế nó bằng mã dưới đây

/*
 * 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' ); 
}
8

Bây giờ chúng ta có thể truy cập giá trị nonce từ JavaScript của mình bằng cách sử dụng

/*
 * 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' ); 
}
30. Thêm thuộc tính bảo mật theo phương thức
// Setup Ajax action hook
add_action( 'wp_ajax_read_me_later', array( $this, 'read_me_later' ) );
8 trong tệp JavaScript của bạn, như bên dưới

/*
 * 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' ); 
}
9

JavaScript cuối cùng của chúng tôi sẽ trông như thế này

/**
 * 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;       
}
0

Cuối cùng, chúng ta cần kiểm tra nonce trong lệnh gọi lại Ajax của mình. Chúng tôi sẽ sử dụng chức năng

/*
 * 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' ); 
}
32 để đạt được điều này. Thêm đoạn mã sau vào đầu phương thức
/*
 * 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' ); 
}
07 mà chúng ta đã tạo trướ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;       
}
1

Điều này có hai đối số. Đầu tiên là khóa chúng tôi đã tạo bằng cách sử dụng

/*
 * 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' ); 
}
28. Thứ hai là thuộc tính
/*
 * 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' ); 
}
35 mà chúng tôi đã chuyển từ JavaScript

Nếu nonce không chính xác hoặc không được đặt, lệnh gọi Ajax sẽ chết. Bằng cách này, tập lệnh của chúng tôi sẽ chặn các yêu cầu Ajax không hợp lệ

Phần kết luận

Trong hướng dẫn này, chúng tôi đã tạo một hệ thống nơi người dùng có thể lưu các bài đăng yêu thích của họ vào danh sách và đọc chúng sau này. Bạn luôn có thể thêm nhiều tính năng hơn vào nó, chẳng hạn như tạo một trang khác để hiển thị tất cả các bài đăng đã lưu, khả năng thêm trang vào danh sách hoặc thêm bài đăng từ các loại bài đăng tùy chỉnh chẳng hạn. Bạn thậm chí có thể tạo trang cài đặt bảng điều khiển để định cấu hình tất cả các tùy chọn. Tùy thuộc vào bạn và loại tính năng bạn muốn tạo cho người dùng của mình

Như bạn có thể thấy, thật dễ dàng để sử dụng Ajax trong WordPress. Lần đầu tiên có thể khó khăn, nhưng một khi bạn hiểu cách thực hiện việc này, nó sẽ hoạt động và trông rất tuyệt. WordPress Hook ở khắp mọi nơi và chúng làm cho cuộc sống của bạn dễ dàng hơn. Tôi hy vọng bạn đã học được một số kỹ thuật hữu ích từ hướng dẫn này, giờ hãy chơi với Ajax và tạo ra những thứ bạn yêu thích

Chia sẻ bài viết này

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

Rahman đã nói

Tôi là nhà phát triển web, nhà văn và người đam mê web. Trọng tâm chính của tôi là WordPress, vì tôi thích tạo các chủ đề, plugin và các trang web WordPress tùy chỉnh. Thông thường tôi thích phát triển và sau đó chia sẻ kinh nghiệm của mình

Làm cách nào để thêm tệp AJAX trong WordPress?

Create HTML Form.

Upload File

/* Create Security nonce */ Cách sử dụng quản trị viên
Vì lõi WordPress đã sử dụng Ajax để tăng sức mạnh cho các tính năng phụ trợ khác nhau của nó nên bạn có thể sử dụng các chức năng tương tự để sử dụng Ajax trên WordPress. Tất cả những gì bạn cần làm là đăng ký một hành động, trỏ hành động đó đến trang web của bạn quản trị-ajax. tệp php và xác định cách bạn muốn nó trả về giá trị .

Làm cách nào để gọi URL AJAX trong WordPress?

nhấp chuột (hàm () { $. ajax({ url. ajax_script. ajaxurl, dữ liệu. ({hoạt động. 'function1'}), thành công. hàm(dữ liệu){ $('#result'). html(dữ liệu);