Bạn có một theme WordPress muốn chia sẻ với mọi người nhưng khi cài đặt theme đó thì cần một số plugin kèm theo như : Jetpack, WP-PageNavi, Yet Another Related Posts Plugin,… để có thể hoạt đông một cách hiệu quả nhất. Để người dùng chú ý các plugin cần cài đặt khi cài đặt theme, bạn cần tạo một nhắc nhở ngay khi người dùng “Active” theme đó. Bài viết sau sẽ hướng dẫn bạn làm điều đó.

Cách hoạt động

Đơn giản bạn chỉ cần thêm đoạn code phía dưới vào file functions.php của theme. Khi theme được “Active” thì sẽ tữ kiểm tra xem plugin mà bạn liệt kê trong đó đã được plugin đã cài chưa hoặc đã active chưa và hiện ra thông báo như hình

messages

Plugin chưa cài hoặc chưa active sẽ có thông báo cụ thể

Code

Dán đoạn code sau vào file functions.php của theme.

add_action('admin_notices', 'showAdminMessages');

function showAdminMessages()
{
    $plugins_requires = array(
        'Yoast WordPress SEO' => 'wordpress-seo/wp-seo.php',
        'Akismet' => 'akismet/akismet.php'
    );

    $plugin_messages = array();

    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

    foreach ($plugins_requires as $name => $active_file) {
        $result = validate_plugin( $active_file );

        if(is_wp_error( $result )){
            $plugin_messages[] = 'This theme requires you to <strong>install</strong> the <strong>'.$name.'</strong> plugin';
        }else{
            if(!is_plugin_active( $active_file ))
            {
                $plugin_messages[] = 'This theme requires you to <strong>active</strong> the <strong>'.$name.'</strong> plugin';
            }
        }
    }

    if(count($plugin_messages) > 0)
    {
        echo '<div id="message" class="error">';

            foreach($plugin_messages as $message)
            {
                echo '<p>'.$message.'</p>';
            }

            echo '<p><strong><a href="' . admin_url( 'plugins.php' ) .'" class="button">Check Now</a></strong></p>';

        echo '</div>';
    }
}

Bạn chú ý mảng $plugins_requires là danh sách plugin yêu cầu cài đặt khi cài theme. Cách thêm là ta lấy tên plugin làm phần key, file active của plugin làm phần value.

Ví dụ 'Akismet' => 'akismet/akismet.php', “Akismet” là tên của plugin (nên để đúng tên của plugin để dễ tìm kiếm), akismet/akismet.php là file active của plugin.

File active của plugin là file nằm trong thư mục wp-content/plugins/[ten-plugin]có các dòng comments trên đầu như sau (lấy plugin Akismet làm ví dụ)

/*
Plugin Name: Akismet
Plugin URI: http://akismet.com/?return=true
Description: Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from comment and trackback spam</strong>. It keeps your site protected from spam even while you sleep. To get started: 1) Click the "Activate" link to the left of this description, 2) <a href="http://akismet.com/get/?return=true">Sign up for an Akismet API key</a>, and 3) Go to your Akismet configuration page, and save your API key.
Version: 2.5.9
Author: Automattic
Author URI: http://automattic.com/wordpress-plugins/
License: GPLv2 or later
*/

Khi xác định được file active của plugin, bạn cũng sẽ biết tên chính xác của plugin đó giúp dễ dàng hơn khi người sử dụng theme cần tìm plugin.

Vậy là ta đã làm xong, câu nhắc nhở sau khi active theme sẽ làm người dùng chú ý các plugin mà theme cần để hoạt động hiệu quả. Comment bên dưới nếu có thắc mắc gì.

Các nguồn tham khảo: