Skip to content
Joshua Nelson edited this page Nov 17, 2025 · 7 revisions

Scripts To Footer - Developer Documentation

Scripts-To-Footer strives to be developer-friendly, allowing you the ability to customize the behavior through various settings and filters.

Overview

You have several options for customizing the plugin's behavior:

  • Metabox Control: Use the metabox found on posts and pages to exclude a specific post/page from the plugin (meaning scripts will not be moved).
  • Settings Page: Via the settings page (Settings > Scripts to Footer) you can select specific non-post/page templates that you wish to exclude from the plugin, thus leaving scripts on those pages per the defaults for your WordPress site. These include: the blog page, most archives, and search results page.
  • jQuery Header Option: Via the settings page you can force jQuery to remain in the header. This is a common script that can cause issues when not in the header in some cases. If you have something breaking on your site (a slider, a menu, et cetera) after activating Scripts to Footer, check this box. If that doesn't work, you likely have other scripts that need to remain in the header. Consider using the stf_exclude_scripts filter below.
  • Filters and Actions: Use the filters and actions below to customize Scripts to Footer for your site with advanced logic.

Actions

stf_init

When it fires: During plugin initialization, after the plugin object is constructed.

Parameters: None

Usage: Use this action to perform custom initialization tasks when the Scripts to Footer plugin loads.

add_action( 'stf_init', 'my_custom_stf_init_function' );
function my_custom_stf_init_function() {
    // Your custom initialization code here
}

Filters

Core Filters

stf_exclude_scripts

Purpose: Filter the array of script slugs that are printed in the head instead of the footer.

Parameters:

  • $scripts (array) - Array of script handles to keep in header

Default: Empty array (or contains 'jquery' if "Keep jQuery in Header" setting is enabled)

The stf_exclude_scripts filter is your primary tool for keeping specific scripts in the header. You can force jQuery to remain in the header via the settings page (Settings > Scripts to Footer). However, if you require more than jQuery to be in the header, or want to modify the header scripts, you can use this filter:

add_filter( 'stf_exclude_scripts', 'stf_custom_header_scripts', 10, 1 );
function stf_custom_header_scripts( $scripts ) {
    $scripts[] = 'backbone'; // replace 'backbone' with the script slug
    return $scripts;
}

Note: These scripts will only be placed in the header if they are enqueued properly for the page in question, otherwise they will be ignored. Dependencies will also be included automatically.

stf_include

Purpose: The main filter, return true to run the plugin on the page, false or null to not.

Parameters:

  • $include (bool) - Whether to run the plugin (default: true)

The stf_include filter is the main filter that is utilized to run the plugin and it defaults to true. While you can use it to filter the result, it's better to use one of the other more specific filters.

add_filter( 'stf_include', 'custom_stf_include_logic' );
function custom_stf_include_logic( $include ) {
    // Disable on specific page
    if ( is_page( 'checkout' ) ) {
        return false;
    }
    
    // Disable for logged-in administrators
    if ( current_user_can( 'administrator' ) ) {
        return false;
    }
    
    return $include;
}

Post Type and Template Filters

stf_{$post_type}

Purpose: If is_singular() (or is_page()) returns true, then use this filter with $post_type replaced with the post type slug.

Parameters:

  • $include (bool) - Whether to include this post type
  • $post_id (int) - Post ID

Available filters:

  • stf_page - For pages
  • stf_post - For posts
  • stf_{custom_post_type} - For any custom post type

For instance, for typical posts, use stf_post or for typical pages use stf_page. Return true to include, false to prevent plugin from running.

The stf_{$post_type} is similar to other filters, however it comes with an extra parameter: the post id. For instance, to turn the plugin off on a specific page you could use the metabox (go to the edit screen, make sure your Screen Options are showing the metabox, and then check the box), or to do it programmatically:

add_filter( 'stf_page', 'stf_exclude_my_page', 10, 2 );
function stf_exclude_my_page( $value, $page_id ) {
    if( $page_id == 23 ) {
        return false;
    } else {
        return $value;
    }
}

// Exclude posts in specific category
add_filter( 'stf_post', 'exclude_posts_by_category', 10, 2 );
function exclude_posts_by_category( $include, $post_id ) {
    if ( has_category( 'no-footer-scripts', $post_id ) ) {
        return false;
    }
    
    return $include;
}

stf_{$type} - Template-Specific Filters

Purpose: Use this filter similar to stf_{$post_type} for any other page types, typically archives.

Parameters:

  • $include (bool) - Whether to include this template

Available filters:

  • stf_home - For the blog page
  • stf_search - For the search results page
  • stf_404 - For 404 error pages
  • stf_archive - Generic archive pages (fallback for archives that don't fall into the above)
  • stf_author_archive - For the author archives
  • stf_category_archive - For the category archives
  • stf_post_tag_archive - For the tag archives
  • stf_{taxonomy}_archive - Custom taxonomy archives (refer to custom taxonomy support below)
  • stf_{post_type}_archive - Custom post type archives (refer to custom post type support below)

The stf_{$type} filter occurs within the stf_include filter and returns true to run the plugin, false to do nothing. For example, if you wanted to filter the author archives to skip a specific user:

add_filter( 'stf_author_archive', 'stf_exclude_my_author' );
function stf_exclude_my_author( $value ) {
    $author_id = get_query_var( 'author' );
    $author_name = get_query_var( 'author_name' );
    if( 23 == $author_id || 'bob' == $author_name ) { // replace with the author id and name, use both in case the name isn't set or it changes
        return false;
    } else {
        return $value;
    }
}

// Disable on search pages during high traffic
add_filter( 'stf_search', 'disable_on_search' );
function disable_on_search( $include ) {
    // Disable on search pages between 9 AM and 5 PM
    $current_hour = (int) current_time( 'H' );
    if ( $current_hour >= 9 && $current_hour <= 17 ) {
        return false;
    }
    
    return $include;
}

// Disable on product archives
add_filter( 'stf_product_archive', '__return_false' );

Configuration Filters

scripts_to_footer_post_types

Purpose: Define which post types support the plugin's metabox and functionality.

Parameters:

  • $post_types (array) - Array of post type slugs

Default: array( 'page', 'post' ) The scripts_to_footer_post_types filter can be used to change the post types the plugin is applied to (it applies to pages and posts by default).

For example, if you have a custom post type called "project" you could add support for the Scripts-to-Footer metabox via the post type filter like this:

function stf_add_cpt_support( $post_types ) {
    $post_types[] = 'project';
    
    return $post_types;
}
add_filter( 'scripts_to_footer_post_types', 'stf_add_cpt_support' );

Or, if you wanted to remove support for the post type you can use this code:

function stf_remove_post_type( $post_types ) {
    if( is_array( $post_types ) && isset( $post_types['post'] ) )
        unset( $post_types['post'] );
    
    return $post_types;
}
add_filter( 'scripts_to_footer_post_types', 'stf_remove_post_type' );

scripts_to_footer_taxonomies

Purpose: Define which taxonomies are supported for archive pages.

Parameters:

  • $taxonomies (array) - Array of taxonomy slugs

Default: array( 'category', 'post_tag' )

The scripts_to_footer_taxonomies filter can be used to change the taxonomies the plugin is applied to (it applies to categories and post tags by default), in regards to the taxonomy archives.

For example, if you have a custom taxonomy called "department" you could add support for the Scripts-to-Footer functionality via the taxonomy filter like this:

function stf_add_tax_support( $taxonomies ) {
    $taxonomies[] = 'department';
    
    return $taxonomies;
}
add_filter( 'scripts_to_footer_taxonomies', 'stf_add_tax_support' );

Or, if you wanted to remove support for the post_tag type you can use this code:

function stf_remove_post_tag_support( $taxonomies ) {
    if( is_array( $taxonomies ) && isset( $taxonomies['post_tag'] ) )
        unset( $taxonomies['post_tag'] );
    
    return $taxonomies;
}
add_filter( 'scripts_to_footer_taxonomies', 'stf_remove_post_tag_support' );

Deprecated Filters

Note: As of version 0.6 the following filters are deprecated and, while currently supported, they may not be supported in future release, use at your own risk:

scripts_to_footer_exclude_page (Deprecated)

Purpose: Used to return a true value to exclude a page/post from the plugin. Now only runs if is_singular() or is_page() returns true.

Status: See the stf_include filter for the replacement.