Bài viết hướng dẫn cách tạo một plugin cho phép tự động tạo ra các quot ngẫu nhiên mỗi lần page được load. Bạn có thể ứng dụng plugin này trong việc tạo ra các quot các câu nói nổi tiếng, câu châm ngôn của các nhân vật lịch sử. Rất thú vị
I. Bắt đầu
Với một plugin nào cũng cần đoạn mã dưới đây để khai báo với wordpress rằng có 1 plugin trong hệ thống
1 2 3 4 5 6 7 8 9 10 | <?php /* Plugin Name: Random Quote Version: 2.0 Plugin URI: https://sharecodeweb.net Description: Loads a Random Quote from custom post types Author: TH Author URI: https://sharecodeweb.net */ ?> |
II.Đăng ký một kiểu Custom Post
Trước khi chúng ta sử dụng plugin random quot thì chúng ta phải khởi tạo một bài viết kiểu Custom. Đoạn mã dưới đây cho phép admin có thể tùy chỉnh trong khu vực admin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php add_action( 'init', 'random_quote' ); function random_quote() { register_post_type( 'random_quote', array( 'labels' => array( 'name' => __( 'Random Quotes' ), 'singular_name' => __( 'Random Quote' ) ), 'public' => true, 'has_archive' => true, ) ); } ?> |
III. Tạo giao diện trong khu vực admin
Với 2 hàm project_edit_columns
và project_custom_columns
chúng ta có thể tạo ra một giao diện cho phép admin có thể chỉnh sửa các quot của mình.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php add_filter("manage_edit-random_quote_columns", "project_edit_columns"); function project_edit_columns($columns) { $columns = array( "cb" => "<input type=\"checkbox\" />", "title" => "Person", "description" => "Quote", ); return $columns; } add_action("manage_posts_custom_column", "project_custom_columns"); function project_custom_columns($column) { global $post; switch ($column) { case "description": the_excerpt(); break; } } ?> |
Khi xong chúng ta được như thế này
IV. Lấy 1 Random Quot từ Database
Với lớp WP_Query
chúng ta có thể lấy một random quot từ một custom post. Biến $quo sẽ giúp chúng ta tạo ra chuỗi được quot lại với tên của người đã nói câu nói đó, kiểu như này
“I never think of the future. It comes soon enough.”
~ Albert Einstein
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php function ab_arq_generate() { // Retrieve one random quote $args = array( 'post_type' => 'random_quote', 'posts_per_page' => 1, 'orderby' => 'rand' ); $query = new WP_Query( $args ); // Build output string $quo = ''; $quo .= $query->post->post_title; $quo .= ' said "'; $quo .= $query->post->post_content; $quo .= '"'; return $quo; } ?> |
V. Đăng ký Quot với WordPress
1 2 3 4 5 6 7 8 9 | <?php function ab_arq_change_bloginfo( $text, $show ) { if( 'description' == $show ) { $text = ab_arq_generate(); } return $text; } add_filter( 'bloginfo', 'ab_arq_change_bloginfo', 10, 2 ); ?> |
VI. Hoàn thành
Cuối cùng plugin của chúng ta chỉ gồm 1 file với đoạn mã hoàn chỉnh dưới đây
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php /* Plugin Name: Random Quote Version: 2.0 Plugin URI: https://sharecodeweb.net Description: Loads a Random Quote from custom post types Author: TH Author URI: https://sharecodeweb.net */ // Register custom post type add_action( 'init', 'ab_arq_random_quote' ); function ab_arq_random_quote() { register_post_type( 'random_quote', array( 'labels' => array( 'name' => __( 'Random Quotes' ), 'singular_name' => __( 'Random Quote' ) ), 'public' => true, 'has_archive' => true, ) ); } // Create admin interface add_filter("manage_edit-random_quote_columns", "ab_arq_project_edit_columns"); function ab_arq_project_edit_columns($columns) { $columns = array( "cb" => "<input type=\"checkbox\" />", "title" => "Person", "description" => "Quote", ); return $columns; } add_action("manage_posts_custom_column", "ab_arq_project_custom_columns"); function ab_arq_project_custom_columns($column) { global $post; switch ($column) { case "description": the_excerpt(); break; } } // Main function to get quotes function ab_arq_generate() { // Retrieve one random quote $args = array( 'post_type' => 'random_quote', 'posts_per_page' => 1, 'orderby' => 'rand' ); $query = new WP_Query( $args ); // Build output string $quo = ''; $quo .= $query->post->post_title; $quo .= ' said "'; $quo .= $query->post->post_content; $quo .= '"'; return $quo; } // Helper function function ab_arq_change_bloginfo( $text, $show ) { if( 'description' == $show ) { $text = ab_arq_generate(); } return $text; } // Override default filter with the new quote generator add_filter( 'bloginfo', 'ab_arq_change_bloginfo', 10, 2 ); ?> |
Vậy là xong. bạn có thể dùng plugin vào mọi nơi, mọi chỗ, tô thêm cho wordpress của bạn thêm đẹp hơn nhé
You must log in to post a comment.