Ajax có được sử dụng trong wordpress không?

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

Ajax có được sử dụng trong wordpress không?

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 gửi dữ liệu để xử lý
  2. Móc hành động Ajax 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;       
    }
    
    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

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

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

/**
 * 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

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à

/**
 * 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;       
}
00. 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à
/**
 * 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;       
}
02 của mình bằng các chức năng WordPress thích hợp

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

/**
 * 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;       
}
03 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;       
}
04 đượ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
/**
 * 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;       
}
05, 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 hàm 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

/**
 * 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;       
}
07. 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ã bên dưới vào bên trong phương thức ________ 205

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

/**
 * 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

Ở đây, chúng tôi sử dụng một chức năng 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;       
}
21. Phải mất ba đối số

  1. /**
     * 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;       
    }
    
    22, 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

/**
 * 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;       
}
24, nó sẽ xuất ra giá trị của
/**
 * 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;       
}
20, 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

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

/**
 * 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

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

/**
 * 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;       
}
40 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
/**
 * 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;       
}
24 trong bước trước? . Yêu cầu Ajax của chúng tôi sẽ được gửi đến đó để xử 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;       
}
43 cho biết liệu yêu cầu sẽ được gửi bằng phương thức HTTP
/**
 * 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;       
}
44 hay
/**
 * 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;       
}
45. Chúng tôi sử dụng phương phá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;       
}
45 ở đây, vì chúng tôi đặ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;       
}
47

/**
 * 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;       
}
48 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ị.
/**
 * 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;       
}
49 chứa ID bài đăng 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;       
}
40 chứa
/**
 * 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;       
}
41 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à

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

/**
 * 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;       
}
44 của bạn được bọc bằng thẻ div có thuộc tính
/**
 * 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;       
}
45
/**
 * 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;       
}
46 nếu không thì jQuery sẽ không hoạt động

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

/**
 * 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;       
}
47 ngay sau khi người dùng click vào để người dùng không thể save 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
/**
 * 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;       
}
48

/**
 * 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

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

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

/**
 * 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;       
}
50 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;       
}
51. 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
/**
 * 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;       
}
52 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 phương thứ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;       
}
54

/**
 * 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

Đ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

/**
 * 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;       
}
40 trong phương thứ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;       
}
48 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

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

Đ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

/**
 * 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;       
}
57. Sau đó, chúng tôi đã khai báo một mảng trống 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;       
}
58

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

/**
 * 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;       
}
59 trong bảng
/**
 * 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;       
}
70 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 hàm
/**
 * 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;       
}
71 WordPress và lưu trữ nó trong
/**
 * 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;       
}
72

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

/**
 * 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;       
}
72 có tồn tại hay không. Nếu đúng, chúng tôi lưu trữ nó trong mảng
/**
 * 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;       
}
58 đã khai báo trước đó. Sau đó, chúng tôi đẩy giá trị của
/**
 * 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;       
}
57 vào bên trong mảng bằng cách sử dụng hàm
/**
 * 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;       
}
76. Nếu không 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;       
}
72, thì chúng ta chỉ cần lưu trữ
/**
 * 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;       
}
57 trong
/**
 * 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;       
}
58

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

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

/**
 * 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;       
}
71 trong
/**
 * 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;       
}
903 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

/**
 * 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

Ở đây, chúng tôi sử dụng chức năng 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;       
}
904 để 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à
/**
 * 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;       
}
905 chứa mảng ID bài đăng. Cuối cùng, chúng tôi sử dụng
/**
 * 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;       
}
906 để 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

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

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

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

/**
 * 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;       
}
908 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à
/**
 * 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;       
}
910 mở rộng lớ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;       
}
908

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

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

/**
 * 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;       
}
912 bên trong lớp để khởi tạo tiện ích của chúng tôi

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

Ở đâ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 bằng phương phá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;       
}
913, 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;       
}
93

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 chỉ định tiêu đề của mình trong biến

/**
 * 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;       
}
914.
/**
 * 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;       
}
915 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;       
}
916 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

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

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

Phải mất hai tham số

  1. /**
     * 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;       
    }
    
    918 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 phá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;       
    }
    
    913
  2. /**
     * 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;       
    }
    
    920 ngược lại, nó chứa giá trị trước đó

Bây giờ chúng ta sẽ tạo phương thứ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;       
}
921 sẽ hiển thị các bài đăng 'Đọc cho tôi sau' ở giao diện người dùng

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

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

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

Đừ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

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

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

/**
 * 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;       
}
926 để 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
/**
 * 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;       
}
926 trong cuộc gọi Ajax

Đầu tiên, chúng ta sẽ tạo một nonce bằng phương thứ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;       
}
928 và chuyển nó từ JavaScript. Để đạt được điều này, hãy sử dụng mã từ phương phá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;       
}
03

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

Và thay thế nó bằng mã dưới đâ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;       
}
98

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

/**
 * 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;       
}
930. Thêm một thuộc tính bảo mật trong phương thứ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;       
}
48 trong tệp JavaScript của bạn, như bên dưới

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

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

/**
 * 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;       
}
932 để đạt được điều này. Thêm đoạn mã sau vào đầu phương thứ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;       
}
907 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

/**
 * 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;       
}
928. Thứ hai là thuộc tính
/**
 * 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;       
}
935 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ệ

Sự 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

Ajax có được sử dụng trong wordpress không?

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

AJAX ở đâu trong WordPress?

Mọi yêu cầu AJAX đều đi qua admin-ajax. tệp php trong thư mục wp-admin .

Làm cách nào để đưa AJAX vào WordPress?

Đây là quy trình sử dụng Ajax trong WordPress trông như thế nào. Người dùng kích hoạt một yêu cầu Ajax, lần đầu tiên yêu cầu này được chuyển đến quản trị viên-ajax. php trong thư mục wp-admin. Yêu cầu Ajax cần cung cấp ít nhất một phần dữ liệu (sử dụng phương thức GET hoặc POST)

AJAX có được sử dụng với php không?

Bắt đầu sử dụng AJAX ngay hôm nay . Tập lệnh máy chủ sẽ được viết bằng PHP . Nếu bạn muốn tìm hiểu thêm về AJAX, hãy truy cập hướng dẫn AJAX của chúng tôi.

Làm cách nào để sử dụng tìm kiếm AJAX trong WordPress?

Cài đặt .
Tải ajax-search-lite lên thư mục /wp-content/plugins/
Kích hoạt plugin thông qua menu 'Plugin' trong WordPress
Đặt mã ngắn từ cài đặt vào mẫu hoặc trang đăng của bạn