Using WordPress ‘print_head_scripts’ PHP filter

The ‘print_head_scripts’ filter is used to control whether WordPress should print the head scripts on your website. This filter can be useful when you need to conditionally load scripts or prevent scripts from being loaded in certain situations.

Usage

To use this filter, you need to hook a function to it using add_filter(). The function should return a boolean value, either true (to print head scripts) or false (to prevent printing head scripts).

function my_function($print) {
    // Your custom logic goes here
    return $print;
}
add_filter('print_head_scripts', 'my_function');

Parameters

  • $print (bool): Whether to print the head scripts. Default is true.

Examples

Disable head scripts on a specific page

Scenario: You want to prevent the head scripts from being printed on a specific page with the ID of 42.

function disable_head_scripts_on_page_42($print) {
    if (is_page(42)) {
        return false;
    }
    return $print;
}
add_filter('print_head_scripts', 'disable_head_scripts_on_page_42');

Explanation: This code checks if the current page is the one with ID 42, and if so, it returns false to prevent printing head scripts. Otherwise, it returns the original $print value.

Disable head scripts for logged-out users

Scenario: You want to prevent the head scripts from being printed for logged-out users.

function disable_head_scripts_for_logged_out($print) {
    if (!is_user_logged_in()) {
        return false;
    }
    return $print;
}
add_filter('print_head_scripts', 'disable_head_scripts_for_logged_out');

Explanation: This code checks if the current user is not logged in, and if so, it returns false to prevent printing head scripts. Otherwise, it returns the original $print value.

Disable head scripts on custom post type archive pages

Scenario: You want to prevent the head scripts from being printed on all archive pages of a custom post type called “events”.

function disable_head_scripts_on_events_archive($print) {
    if (is_post_type_archive('events')) {
        return false;
    }
    return $print;
}
add_filter('print_head_scripts', 'disable_head_scripts_on_events_archive');

Explanation: This code checks if the current page is an archive page for the “events” custom post type, and if so, it returns false to prevent printing head scripts. Otherwise, it returns the original $print value.

Disable head scripts on mobile devices

Scenario: You want to prevent the head scripts from being printed for mobile devices.

function disable_head_scripts_on_mobile($print) {
    if (wp_is_mobile()) {
        return false;
    }
    return $print;
}
add_filter('print_head_scripts', 'disable_head_scripts_on_mobile');

Explanation: This code checks if the current user is browsing your website using a mobile device, and if so, it returns false to prevent printing head scripts. Otherwise, it returns the original $print value.

Disable head scripts on a custom template

Scenario: You want to prevent the head scripts from being printed on a custom template called “template-no-scripts.php”.

function disable_head_scripts_on_custom_template($print) { 
if (is_page_template('template-no-scripts.php')) { 
return false; 
} 
return $print; 
} 
add_filter('print_head_scripts', 'disable_head_scripts_on_custom_template');

Explanation: This code checks if the current page is using the custom template “template-no-scripts.php”, and if so, it returns `false` to prevent printing head scripts. Otherwise, it returns the original `$print` value.