Using WordPress ‘print_footer_scripts’ PHP filter

The ‘print_footer_scripts’ filter allows you to control whether footer scripts are printed on your WordPress site or not.

Usage

To use this filter, add the following code to your theme’s functions.php file:

add_filter( 'print_footer_scripts', 'your_function_name' );

function your_function_name( $print ) {
    // Your code here
    return $print;
}

Parameters

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

Examples

add_filter( 'print_footer_scripts', 'disable_scripts_on_page' );

function disable_scripts_on_page( $print ) {
    if ( is_page( 'no-scripts' ) ) {
        return false;
    }
    return $print;
}

This code disables the printing of footer scripts on a page with the slug ‘no-scripts’.

add_filter( 'print_footer_scripts', 'disable_scripts_for_guests' );

function disable_scripts_for_guests( $print ) {
    if ( ! is_user_logged_in() ) {
        return false;
    }
    return $print;
}

This code disables the printing of footer scripts for users who are not logged in.

add_filter( 'print_footer_scripts', 'disable_scripts_on_mobile' );

function disable_scripts_on_mobile( $print ) {
    if ( wp_is_mobile() ) {
        return false;
    }
    return $print;
}

This code disables the printing of footer scripts on mobile devices.

add_filter( 'print_footer_scripts', 'disable_scripts_in_category' );

function disable_scripts_in_category( $print ) {
    if ( is_single() && has_category( 'no-scripts' ) ) {
        return false;
    }
    return $print;
}

This code disables the printing of footer scripts for posts in the ‘no-scripts’ category.

Disable Footer Scripts on Custom Post Type Archives

add_filter( 'print_footer_scripts', 'disable_scripts_on_cpt_archive' );

function disable_scripts_on_cpt_archive( $print ) {
    if ( is_post_type_archive( 'your_custom_post_type' ) ) {
        return false;
    }
    return $print;
}

This code disables the printing of footer scripts on the archive pages for the custom post type ‘your_custom_post_type’.