fbpx

Plugins WordPress là gì? Plugins gồm những thành phần nào?

Chào bạn tôi là Nam Digital, tôi truyền thông và cung cấp các giải pháp về Web Development, Digital Marketing. Như đã nói ở Series Block Themes, để học thứ gì nhanh nhất thì theo Nam mình cần có sự ghi chép tỉ mỉ, sau đó thường xuyên làm và “xem đi xem lại”, từ đó kiến thức sẽ là của mình.

Nam đã đạt được thành công đó với Block Editor, giờ thì Nam tiếp tục lấn sang phần phát triển Plugins WordPress

Plugins là gì?

Định nghĩa đơn giản từ nhà WordPress.org như sau

Plugins are packages of code that extend the core functionality of WordPress. WordPress plugins are made up of PHP code and can include other assets such as images, CSS, and JavaScript.

WordPress org

Cơ bản thì plugins là 1 gói code giúp gia tăng tính năng cho các chức năng lõi của WordPress, tức là nó có tính mở rộng, không phải là ghi đè và thay thế core, bởi những phiên bản WordPress sẽ thường xuyên được cập nhật theo thời gian, nên Plugins cần mang tính kế thừa và mở rộng từ khung đó.

Điều này cho thấy việc lập trình Plugins không hề dễ, mà có những thách thức không hề nhỏ, bởi bạn sẽ phải “bó vào khuôn khổ”, tìm hiểu sâu về các hàm được sử dụng trong WordPress, từ đó ứng dụng để tạo những chức năng dành riêng cho mình.

Ví dụ của một plugins

Hello Dolly là một plugins được tạo từ 100 dòng code và khá đơn giản, mục đích của nó là đẩy ra ngoài WordPress Admin ngẫu nhiên một dòng của lời bài hát Hello Dolly.

function hello_dolly() {
	        $chosen = hello_dolly_get_lyric();
	        $lang   = '';
	        if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) {
	                $lang = ' lang="en"';
	        }
	
	        printf(
	                '<p id="dolly"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span></p>',
	                __( 'Quote from Hello Dolly song, by Jerry Herman:', 'hello-dolly' ),
	                $lang,
	                $chosen
	        );
	}
	
	// Now we set that function up to execute when the admin_notices action is called.
	add_action( 'admin_notices', 'hello_dolly' );

Như bạn thấy thì hàm hello_dolly đã đẩy vào action hooks admin_notices để hiển thị thông báo theo ý muốn.

Plugins gồm những thành phần nào?

Khai báo thông tin header

Một plugins đúng chuẩn cần phải được khai báo một cách rõ ràng, đầy đủ, chứa những thông tin về tác giả, tên Plugins, phiên bản PHP hỗ trợ….

/*
 * Plugin Name:       My Basics Plugin
 * Plugin URI:        https://example.com/plugins/the-basics/
 * Description:       Handle the basics with this plugin.
 * Version:           1.10.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            John Smith
 * Author URI:        https://author.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://example.com/my-plugin/
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */

Trên đây là ví dụ của 1 plugins mẫu

Activation / Deactivation Hooks

Gói mở rộng có thể On/Off linh hoạt ở WordPress Dashboard, chính vì vậy mà 2 hooks Activation và Deactivated cần được sử dụng linh hoạt để đưa những tính năng mong muốn khi chạy Plugins

/**
 * Register the "book" custom post type
 */
function pluginprefix_setup_post_type() {
	register_post_type( 'book', ['public' => true ] ); 
} 
add_action( 'init', 'pluginprefix_setup_post_type' );


/**
 * Activate the plugin.
 */
function pluginprefix_activate() { 
	// Trigger our function that registers the custom post type plugin.
	pluginprefix_setup_post_type(); 
	// Clear the permalinks after the post type has been registered.
	flush_rewrite_rules(); 
}
register_activation_hook( __FILE__, 'pluginprefix_activate' );

Ví dụ ở đây, khi activate thì hàm pluginprefix_setup_post_type() được chạy, đồng thời flush_rewire_rules(); chạy theo nhằm tạo route mới cho loại nội dung tên là book được khởi tạo

Khi deactived, thì ta lại hủy đăng ký loại nội dung book và flush rewire rules một lần nữa

/**
 * Deactivation hook.
 */
function pluginprefix_deactivate() {
	// Unregister the post type, so the rules are no longer in memory.
	unregister_post_type( 'book' );
	// Clear the permalinks to remove our post type's rules from the database.
	flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );

Uninstall hooks hoặc uninstall.php

Một số plugins khi kích hoạt sẽ mở rộng bảng dữ liệu (mysql), mà khi deactivated thì dữ liệu vẫn giữ nguyên mà không bị xóa, chính vì vậy mà Uninstall hooks được tạo ra nhằm xóa hoàn toàn dấu tích của Plugins đó ra khỏi hệ thống WordPress

// if uninstall.php is not called by WordPress, die
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    die;
}

$option_name = 'wporg_option';

delete_option( $option_name );

// for site options in Multisite
delete_site_option( $option_name );

// drop a custom database table
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}mytable" );

Như bạn đã thấy thì trong uninstall.php thậm chí còn có phần xóa bảng dữ liệu được tạo mới từ plugins, điều này sẽ giúp dữ liệu được dọn sạch khi uninstall

Array

Nam là 1 Growth Hacker, Developer đam mê với sự nghiệp phát triển web