Using WordPress ‘is_plugin_page()’ PHP function

The is_plugin_page() WordPress PHP function determines whether the current admin page is generated by a plugin.

Usage

$is_plugin_page = is_plugin_page();

If the current admin page is generated by a plugin, it returns true. Otherwise, it returns false.

Parameters

  • None

More information

See WordPress Developer Resources: is_plugin_page

Examples

Display a message on plugin-generated admin pages

This code checks if the current admin page is generated by a plugin and displays a message accordingly.

if (is_plugin_page()) {
    echo 'This admin page is generated by a plugin.';
} else {
    echo 'This admin page is not generated by a plugin.';
}

Enqueue a script only on plugin-generated admin pages

This code enqueues a custom script only on admin pages generated by a plugin.

function my_custom_script() {
    if (is_plugin_page()) {
        wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__));
    }
}
add_action('admin_enqueue_scripts', 'my_custom_script');

Apply custom CSS to plugin-generated admin pages

This code applies custom CSS only to admin pages generated by a plugin.

function my_custom_css() {
    if (is_plugin_page()) {
        echo '<style>.plugin-generated-page { background-color: lightblue; }</style>';
    }
}
add_action('admin_head', 'my_custom_css');

Redirect users away from plugin-generated admin pages

This code redirects users to the dashboard if they are not allowed to access plugin-generated admin pages.

function restrict_plugin_page_access() {
    if (is_plugin_page() && !current_user_can('manage_options')) {
        wp_safe_redirect(admin_url());
        exit;
    }
}
add_action('admin_init', 'restrict_plugin_page_access');

Log plugin-generated admin page visits

This code logs the visit of plugin-generated admin pages.

function log_plugin_page_visit() {
    if (is_plugin_page()) {
        $current_user = wp_get_current_user();
        $log_message = sprintf('User %s visited a plugin-generated admin page.', $current_user->user_login);
        error_log($log_message);
    }
}
add_action('admin_init', 'log_plugin_page_visit');