‘print_admin_styles’ is a WordPress filter that allows you to control whether or not to print the admin styles on the website’s backend. By default, it’s set to true
, meaning the admin styles will be printed.
Usage
To use this filter, add a custom function to your theme or plugin’s functions.php
file and hook it to the print_admin_styles
filter. Here’s a code example:
function custom_print_admin_styles( $print ) { // Your custom logic here return $print; } add_filter( 'print_admin_styles', 'custom_print_admin_styles' );
Parameters
- $print (bool): Specifies whether to print the admin styles or not. Default is
true
.
Examples
Disable Admin Styles
function disable_admin_styles( $print ) { return false; } add_filter( 'print_admin_styles', 'disable_admin_styles' );
In this example, we disable the admin styles by returning false
from the disable_admin_styles()
function.
Disable Admin Styles for Non-Admin Users
function disable_admin_styles_for_non_admins( $print ) { if ( ! current_user_can( 'manage_options' ) ) { return false; } return $print; } add_filter( 'print_admin_styles', 'disable_admin_styles_for_non_admins' );
In this example, we disable the admin styles for non-admin users by checking their capability and returning false
if they don’t have the required permission.
Enable Admin Styles on Specific Pages
function enable_admin_styles_on_specific_pages( $print ) { global $pagenow; if ( 'my-custom-page.php' == $pagenow ) { return true; } return $print; } add_filter( 'print_admin_styles', 'enable_admin_styles_on_specific_pages' );
In this example, we enable the admin styles on a specific page (my-custom-page.php
) by checking the current page and returning true
if it matches.
Disable Admin Styles on Mobile Devices
function disable_admin_styles_on_mobile( $print ) { if ( wp_is_mobile() ) { return false; } return $print; } add_filter( 'print_admin_styles', 'disable_admin_styles_on_mobile' );
In this example, we disable the admin styles on mobile devices by using the wp_is_mobile()
function and returning false
if it’s a mobile device.
Toggle Admin Styles Based on Custom Option
function toggle_admin_styles_based_on_option( $print ) { $admin_styles_enabled = get_option( 'custom_admin_styles_enabled', true ); return $admin_styles_enabled; } add_filter( 'print_admin_styles', 'toggle_admin_styles_based_on_option' );
In this example, we toggle the admin styles based on a custom option called custom_admin_styles_enabled
. If the option is set to true
, the admin styles will be printed; otherwise, they won’t be printed.