Hướng dẫn tìm bug wordpress plugin (phần 2)

TigerDao

Administrator
Thành viên BQT
Tiếp tục loạt bài hướng dẫn tìm bug wordpress plugin trong phần 2 này mình trình bày tổng quan wordpress plugin và phân tích plugin Hello Dolly để hiểu thêm về cấu trúc cũng như cách hoạt động của plugin.

Nếu chưa cài môi trường hoặc chưa biết dùng wordpress thì đọc lại bài trước hướng dẫn tìm bug wordpress plugin (phần 1).

Tại sao wordpress lại có tính năng plugin?

WordPress có nguyên tắc cốt lõi đó là không bao giờ sửa đổi core wordpress. Vì sau mỗi lần cập nhật phiên bản mới wordpress sẽ ghi đè nên core. Do đó những tính năng mà người dùng sửa core wordpress mà có sẽ bị mất hoặc hệ thống bị lỗi sau khi cập nhật.

WordPress cho phép ta tạo các plugin từ đơn giản đến phức tạp không khác gì ta sửa code core wordpresss.

Cấu trúc plugin​


Cấu trúc của một plugin wordpress gồm 2 thành phần chính:

  • Plugin Header
  • Code

<?php /** * Plugin Name: Name Plugin */ function hello_world() { echo 'Hello World!'; } hello_world(); ?>

Phần chú thích ngay đầu đoạn code là Plugin header, phần này cung cấp thông tin cần thiết cho wordpress core. Và phần còn lại là phần code, phần thực thi chính của wordpress plugin.

Plugin header​


Plugin header là thành phần không thể thiếu của một plugin. Nó chứa các trường cần thiết giúp wordpress nhận biết được đâu là code plugin đâu không phải. Plugin header tối thiểu phải chứa trường sau

/** * Plugin Name: YOUR PLUGIN NAME */

Trường Plugin Name giúp cho wordpress tìm được plugin và hiển thị chúng lên trang web quản trị. Ngoài trường đó ra thì plugin header còn nhiều trường khác nữa.

  • Plugin Name: (required) The name of your plugin, which will be displayed in the Plugins list in the WordPress Admin.
  • Plugin URI: The home page of the plugin, which should be a unique URL, preferably on your own website. This must be unique to your plugin. You cannot use a WordPress.org URL here.
  • Description: A short description of the plugin, as displayed in the Plugins section in the WordPress Admin. Keep this description to fewer than 140 characters.
  • Version: The current version number of the plugin, such as 1.0 or 1.0.3.
  • Requires at least: The lowest WordPress version that the plugin will work on.
  • Requires PHP: The minimum required PHP version.
  • Author: The name of the plugin author. Multiple authors may be listed using commas.
  • Author URI: The author’s website or profile on another website, such as WordPress.org.
  • License: The short name (slug) of the plugin’s license (e.g. GPLv2). More information about licensing can be found in the WordPress.org guidelines.
  • License URI: A link to the full text of the license (e.g. https://www.gnu.org/licenses/gpl-2.0.html).
  • Text Domain: The gettext text domain of the plugin. More information can be found in the Text Domain section of the How to Internationalize your Plugin page.
  • Domain Path: The domain path lets WordPress know where to find the translations. More information can be found in the Domain Path section of the How to Internationalize your Plugin page.
  • Network: Whether the plugin can only be activated network-wide. Can only be set to true, and should be left out when not needed.

Bây giờ, ta sẽ xem plugin header của plugin Hello Dolly

vim /var/wwww/wp-content/plugins/hello.php

9f7d4aba-fcdd-4fb5-9b17-2a9bea57892f.png


Plugin header của file hello.php chứa các trường:

  • Plugin Name: Tên của plugin.
  • Plugin URI: Link đến trang chủ của plugin.
  • Description: Mô tả về plugin.
  • Version: phiên bản hiện tại của plugin.
  • Author URI: Link đến profile của tác giả.

Bây giờ ta cùng xem những trường này sẽ hiển thị như nào trên trang web.

e9056edd-caef-45f6-8829-3a145ea52208.png


Hook​


Hook được sử dụng trong wordpress để tương tác hoặc thay đổi tính năng của core wordpress. Trong wordpress có 2 loại hook đó là: Actions và Filters. WordPress cung cấp các hook để có thể tương tác với wodpress core. Ngoài ra lập trình viên có thể tự tạo ra hook của mình. Để sự dụng chúng cần tạo một hàm và đăng ký Actions hoặc Filters để sử dụng.

Actions cho phép thêm dữ liệu hoặc thay đổi cách hoạt động của WordPress. Các hành động sẽ chạy tại một điểm cụ thể trong quá trình thực thi WordPress Core, plugin và themes. Các hàm callback của Action cho phép ta thực hiện các tác vụ mong muốn như: hiển thị cái gì đó ra màn hình, hoặc thay đổi database, vân vân. Hàm callback của actions không có dữ liệu trả về.

Filters cho phép thay đổi dữ liệu trong quá trình thực thi WordPress Core, plugin và themes. Hàm callback của filters nhận đầu vào là một biến, sửa đổi nó, trả lại cho chương trình. Hàm callback của filters yêu cầu kết quả trả về.

Sử khác biệt giữa Actions và Filters​

  • Actions: nhận dữ liệu vào và sử dụng nó tùy mục đích lập trình viên và không có kết quả trả về (giống functions).
  • Filters: nhận dữ liệu vào và thực hiện các tác vụ liên quan và sau đó trả lại cho chương trình (giống procedure).

Phân tích plugin Hello Dolly​


Mỗi khi cài mới wordpress thì sẽ có 2 plugin được cài theo nhưng không active. Trong đó có plugin Hello Dolly được sử dụng cho người mới muốn tìm hiểu về plugin. Plugin Hello Dolly sẽ hiển thị ngẫu nhiên một đoạn trong lời bài hát Hello, Dolly.

<?php /** * @package Hello_Dolly * @version 1.7.2 */ /* Plugin Name: Hello Dolly Plugin URI: http://wordpress.org/plugins/hello-dolly/ Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page. Author: Matt Mullenweg Version: 1.7.2 Author URI: http://ma.tt/ */ function hello_dolly_get_lyric() { /** These are the lyrics to Hello Dolly */ $lyrics = "Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong I feel the room swayin' While the band's playin' One of our old favorite songs from way back when So, take her wrap, fellas Dolly, never go away again Hello, Dolly Well, hello, Dolly It's so nice to have you back where you belong You're lookin' swell, Dolly I can tell, Dolly You're still glowin', you're still crowin' You're still goin' strong I feel the room swayin' While the band's playin' One of our old favorite songs from way back when So, golly, gee, fellas Have a little faith in me, fellas Dolly, never go away Promise, you'll never go away Dolly'll never go away again"; // Here we split it into lines. $lyrics = explode( "n", $lyrics ); // And then randomly choose a line. return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] ); } // This just echoes the chosen line, we'll position it later. function hello_dolly() { $chosen = hello_dolly_get_lyric(); $lang = ''; if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) { $lang = ' lang="en"'; } printf( '<p ><span >%s </span><span dir="ltr"%s>%s</span></p>', __( 'Quote from Hello Dolly song, by Jerry Herman:' ), $lang, $chosen ); } // Now we set that function up to execute when the admin_notices action is called. add_action( 'admin_notices', 'hello_dolly' ); // We need some CSS to position the paragraph. function dolly_css() { echo " <style type='text/css'> #dolly { float: right; padding: 5px 10px; margin: 0; font-size: 12px; line-height: 1.6666; } .rtl #dolly { float: left; } .block-editor-page #dolly { display: none; } @media screen and (max-width: 782px) { #dolly, .rtl #dolly { float: none; padding-left: 0; padding-right: 0; } } </style> "; } add_action( 'admin_head', 'dolly_css' );

Phần Plugin header đã được phân tích ở trên nên không phân tích lại nữa. Tiếp đến là phân tích đoạn code của plugin này.

Trong plugin Hello Dolly chưa 2 hàm:

  • hello_dolly_get_lyric : Chọn ngãu nhiên các câu hát để hiển thị ra màn hình
  • dolly_css: Chỉnh CSS khi hiển thị ra màn hình.

Điểm đáng chú ý trong đoạn code trên là 2 câu lệnh: add_action( 'admin_notices', 'hello_dolly' );add_action( 'admin_head', 'dolly_css' );. Đây là 2 câu lệnh đăng ký actions với wordpress core với 2 action tương ứng là admin_noticesadmin_head. Khi sự kiện admin_notices, admin_head xảy ra thì sẽ gọi đến các hàm tương ứng hello_dolly, dolly_css.

  • admin_head: Kích hoạt cho tất cả các phần header của trang admin.
  • admin_notices: Hiển thị thông báo cho admin.

Để biến thêm thông tin truy cập vào link sau: https://codex.wordpress.org/Plugin_API/Action_Reference

Dưới là hình ảnh khi đã kích hoạt plugin.

29b52b26-9cbe-464e-ba71-850624dba533.png


Trước khi kết thúc phần 2 ở đây mình xin tóm tắt lại:

  • Cấu trúc wordpress plugin gồm 2 phần: plugin header, code.
  • WordPress plugin chủ yếu sử dụng hook để tương tác qua lại giữ các thành phần.

The post Hướng dẫn tìm bug wordpress plugin (phần 2) appeared first on DEVVN.
 

Similar threads

Thành viên mới

Thành viên trực tuyến

Không có thành viên trực tuyến.
Top