Trang chủ WordpressWordpress Development Thêm tính năng bắt buộc cài plugin khi kích hoạt theme

Thêm tính năng bắt buộc cài plugin khi kích hoạt theme

bởi Thạch Phạm
2 bình luận 3,6K views

Tham gia nhóm hỗ trợ WordPress

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Nếu bạn đã từng sử dụng qua nhiều theme trả phí chắc chắn đã từng bắt gặp các thông báo yêu cầu cài và kích hoạt các plugin mà họ khuyến khích cài. Sở dĩ hay xảy ra trường hợp này là do các theme đó có sử dụng một vài plugin để thêm tính năng cho website của mình, hoặc cần nên cài các plugin riêng của họ để nó hoạt động tốt nhất.

Nếu bạn muốn tìm hiểu cách làm tính năng như thế thì ở bài này mình sẽ hướng dẫn cách bạn thêm tính năng yêu cầu cài plugin cần thiết khi kích hoạt một theme nào đó; bằng cách sử dụng script TGM Plugin Activation. Đây cũng là script mà hiện nay các nhà cung cấp theme đều sử dụng cho giải pháp này.

Trang chủ & Hướng dẫn

Tích hợp TGM Plugin Activation

Đầu tiên là bạn tải script của nó về tại đây, giải nén và upload file class-tgm-plugin-activation.php nằm ngang hàng với file functions.php của theme. upload-tgm-class   Kế đó, mở file functions.php của theme ra và gọi file class-tgm-plugin-activation.php bằng hàm required_once() ngay đầu file.

Khuyến mãi Black Friday

require_once dirname( __FILE__ ) . ‘/class-tgm-plugin-activation.php’;

require-tgmclass-1 Thế là chúng ta đã xong phần chuẩn bị, bây giờ chúng ta sẽ đi qua bước làm việc với một số đoạn code để thêm danh sách plugin cần kích hoạt.

Làm việc với TGM Plugin Activation

Mọi code bên dưới đều viết vào file functions.php, nói chung là bạn require file class-tgm-plugin-activation.php ở đâu thì viết code ở đó. Để làm việc với script này, ta sẽ cần tạo một function và hook nó vào một action hook tên là tgmpa_register.

add_action( ‘tgmpa_register’, ‘my_plugin_activation’ );
function my_plugin_activation() {

// Chút nữa sẽ viết code ngay bên dưới

}

Ở trong function này, chúng ta chỉ cần viết lại hai biến array $plugins và $configs liệt kê ra các tham số cần thiết, chẳng hạn như mình muốn thêm một plugin bắt buộc khi kích hoạt theme thì sẽ thêm như sau:


$plugins = array(

// Gọi một plugin nào đó ở bên ngoài
array(
‘name’ => ‘TGM New Media Plugin’, // Tên của plugin
‘slug’ => ‘tgm-new-media-plugin’, // Tên thư mục plugin
‘source’ => ‘https://s3.amazonaws.com/tgm/tgm-new-media-plugin.zip’, // Link tải plugin – direct link
‘required’ => true, // Nếu đặt là true thì plugin này sẽ không bắt buộc phải cài mà chỉ ở dạng Recommend.
‘external_url’ => ‘https://github.com/thomasgriffin/New-Media-Image-Uploader’, // Nếu bạn cài plugin ở bên ngoài, không phải từ WordPress.Org thì thêm URL của trang plugin vào.
),
// Gọi một plugin trong thư viện WordPress.org/plugins
array(
‘name’ => ‘BuddyPress’,
‘slug’ => ‘buddypress’, //Tên slug của plugin trên URL
‘required’ => false,
),

); // end $plugins

them-plugin-tgm   Như vậy là bạn đã thêm xong hai plugin mẫu và yêu cầu người dùng cài đặt khi kích hoạt theme này lên. Khoan hãy xem kết quả vì chưa có gì đâu, chúng ta cần phải làm việc với biến $config nếu bạn cần tùy chỉnh lại các thông báo. Cách sử dụng y hệt biến $plugins nhé.

$config = array(
‘default_path’ => ”,
‘menu’ => ‘tgmpa-install-plugins’, // Menu slug.
‘has_notices’ => true, // Có hiển thị thông báo hay không
‘dismissable’ => true, // Nếu đặt false thì người dùng không thể hủy thông báo cho đến khi cài hết plugin.
‘dismiss_msg’ => ”, // Nếu ‘dismissable’ là false, thì tin nhắn ở đây sẽ hiển thị trên cùng trang Admin.
‘is_automatic’ => false, // Nếu là false thì plugin sẽ không tự động kích hoạt khi cài xong.
‘message’ => ”,
‘strings’ => array(
‘page_title’ => __( ‘Install Required Plugins’, ‘tgmpa’ ),
‘menu_title’ => __( ‘Install Plugins’, ‘tgmpa’ ),
‘installing’ => __( ‘Installing Plugin: %s’, ‘tgmpa’ ), // %s = plugin name.
‘oops’ => __( ‘Something went wrong with the plugin API.’, ‘tgmpa’ ),
‘notice_can_install_required’ => _n_noop( ‘This theme requires the following plugin: %1$s.’, ‘This theme requires the following plugins: %1$s.’ ), // %1$s = plugin name(s).
‘notice_can_install_recommended’ => _n_noop( ‘This theme recommends the following plugin: %1$s.’, ‘This theme recommends the following plugins: %1$s.’ ), // %1$s = plugin name(s).
‘notice_cannot_install’ => _n_noop( ‘Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.’, ‘Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.’ ), // %1$s = plugin name(s).
‘notice_can_activate_required’ => _n_noop( ‘The following required plugin is currently inactive: %1$s.’, ‘The following required plugins are currently inactive: %1$s.’ ), // %1$s = plugin name(s).
‘notice_can_activate_recommended’ => _n_noop( ‘The following recommended plugin is currently inactive: %1$s.’, ‘The following recommended plugins are currently inactive: %1$s.’ ), // %1$s = plugin name(s).
‘notice_cannot_activate’ => _n_noop( ‘Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.’, ‘Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.’ ), // %1$s = plugin name(s).
‘notice_ask_to_update’ => _n_noop( ‘The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.’, ‘The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.’ ), // %1$s = plugin name(s).
‘notice_cannot_update’ => _n_noop( ‘Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.’, ‘Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.’ ), // %1$s = plugin name(s).
‘install_link’ => _n_noop( ‘Begin installing plugin’, ‘Begin installing plugins’ ),
‘activate_link’ => _n_noop( ‘Begin activating plugin’, ‘Begin activating plugins’ ),
‘return’ => __( ‘Return to Required Plugins Installer’, ‘tgmpa’ ),
‘plugin_activated’ => __( ‘Plugin activated successfully.’, ‘tgmpa’ ),
‘complete’ => __( ‘All plugins installed and activated successfully. %s’, ‘tgmpa’ ), // %s = dashboard link.
‘nag_type’ => ‘updated’ // Determines admin notice type – can only be ‘updated’, ‘update-nag’ or ‘error’.
)
); // end $config

Sau đó ngay bên dưới end $config, bạn cần thêm một đối tượng để kích hoạt hai biến này như sau:

tgmpa( $plugins, $config );

Toàn bộ code đầy đủ:


/* TGM Plugin Activation */
require_once dirname( __FILE__ ) . ‘/class-tgm-plugin-activation.php’;

add_action( ‘tgmpa_register’, ‘my_plugin_activation’ );
function my_plugin_activation() {

$plugins = array(

// Gọi một plugin nào đó ở bên ngoài
array(
‘name’ => ‘TGM New Media Plugin’, // Tên của plugin
‘slug’ => ‘tgm-new-media-plugin’, // Tên thư mục plugin
‘source’ => ‘https://s3.amazonaws.com/tgm/tgm-new-media-plugin.zip’, // Link tải plugin – direct link
‘required’ => true, // Nếu đặt là true thì plugin này sẽ không bắt buộc phải cài mà chỉ ở dạng Recommend.
‘external_url’ => ‘https://github.com/thomasgriffin/New-Media-Image-Uploader’, // Nếu bạn cài plugin ở bên ngoài, không phải từ WordPress.Org thì thêm URL của trang plugin vào.
),
// Gọi một plugin trong thư viện WordPress.org/plugins
array(
‘name’ => ‘BuddyPress’,
‘slug’ => ‘buddypress’, //Tên slug của plugin trên URL
‘required’ => false,
),

); // end $plugins

$config = array(
‘default_path’ => ”,
‘menu’ => ‘tgmpa-install-plugins’, // Menu slug.
‘has_notices’ => true, // Có hiển thị thông báo hay không
‘dismissable’ => true, // Nếu đặt false thì người dùng không thể hủy thông báo cho đến khi cài hết plugin.
‘dismiss_msg’ => ”, // Nếu ‘dismissable’ là false, thì tin nhắn ở đây sẽ hiển thị trên cùng trang Admin.
‘is_automatic’ => false, // Nếu là false thì plugin sẽ không tự động kích hoạt khi cài xong.
‘message’ => ”,
‘strings’ => array(
‘page_title’ => __( ‘Install Required Plugins’, ‘tgmpa’ ),
‘menu_title’ => __( ‘Install Plugins’, ‘tgmpa’ ),
‘installing’ => __( ‘Installing Plugin: %s’, ‘tgmpa’ ), // %s = plugin name.
‘oops’ => __( ‘Something went wrong with the plugin API.’, ‘tgmpa’ ),
‘notice_can_install_required’ => _n_noop( ‘This theme requires the following plugin: %1$s.’, ‘This theme requires the following plugins: %1$s.’ ), // %1$s = plugin name(s).
‘notice_can_install_recommended’ => _n_noop( ‘This theme recommends the following plugin: %1$s.’, ‘This theme recommends the following plugins: %1$s.’ ), // %1$s = plugin name(s).
‘notice_cannot_install’ => _n_noop( ‘Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.’, ‘Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.’ ), // %1$s = plugin name(s).
‘notice_can_activate_required’ => _n_noop( ‘The following required plugin is currently inactive: %1$s.’, ‘The following required plugins are currently inactive: %1$s.’ ), // %1$s = plugin name(s).
‘notice_can_activate_recommended’ => _n_noop( ‘The following recommended plugin is currently inactive: %1$s.’, ‘The following recommended plugins are currently inactive: %1$s.’ ), // %1$s = plugin name(s).
‘notice_cannot_activate’ => _n_noop( ‘Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.’, ‘Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.’ ), // %1$s = plugin name(s).
‘notice_ask_to_update’ => _n_noop( ‘The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.’, ‘The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.’ ), // %1$s = plugin name(s).
‘notice_cannot_update’ => _n_noop( ‘Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.’, ‘Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.’ ), // %1$s = plugin name(s).
‘install_link’ => _n_noop( ‘Begin installing plugin’, ‘Begin installing plugins’ ),
‘activate_link’ => _n_noop( ‘Begin activating plugin’, ‘Begin activating plugins’ ),
‘return’ => __( ‘Return to Required Plugins Installer’, ‘tgmpa’ ),
‘plugin_activated’ => __( ‘Plugin activated successfully.’, ‘tgmpa’ ),
‘complete’ => __( ‘All plugins installed and activated successfully. %s’, ‘tgmpa’ ), // %s = dashboard link.
‘nag_type’ => ‘updated’ // Determines admin notice type – can only be ‘updated’, ‘update-nag’ or ‘error’.
)
); // end $config
tgmpa( $plugins, $config );

}

Lưu lại và xem kết quả nhé: tgm_notice Xong rồi đấy, chúc các bạn thành công. :D

Đánh giá nội dung này
2 bình luận

Có thể bạn quan tâm

Hiện tại blog tạm đóng bình luận vì mình cần tập trung thời gian vào cập nhật bài viết. Bình luận sẽ mở ra cho đến khi mình sẵn sàng.