Skip to content
Joshua Nelson edited this page Oct 15, 2015 · 6 revisions

Scripts-To-Footer strives to be developer-friendly, allowing you the ability to customize the behavior.

  1. You can utilize the metabox found on posts and pages to exlcude a specific post/page from the plugin (meaning scripts will not be moved).
  2. 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.
  3. 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.
  4. Use the filters below to customize Scripts to Footer for your site.

Filters

The following filters are available:

  • stf_exclude_scripts Filter the array of script slugs that are printed in the head instead of the footer
  • stf_include The main filter, return true to run the plugin on the page, false or null to not.
  • stf_{$post_type} If is_singular() (or is_page()) returns true, then use this filter with $post_type replaced with the post type slug. 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. Refer to custom post type support below for use with custom post types.
  • stf_{$type} Use this filter similar to stf_{$post_type} for any other page types, typically archives. Available filters:
    • stf_home For the blog page,
    • stf_search For the search results page,
    • stf_author_archive For the author archives,
    • stf_category_archive For the category archives,
    • stf_post_tag_archive For the tag archives,
    • stf_{$tax->name}_archive and stf_{$post_type}_archive Refer to the custom post type/taxonomy support below before using the taxonomy or post type archive filters.
    • stf_archive This is the fallback for an archive that doesn't fall into the above. It will only run if the archive is not one of the ones listed above.

Refer to the Keeping Scripts in the Header section below for use of stf_exclude_scripts filter use. 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.

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_exlude_my_author' );
function stf_exlude_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 chages
		return false;
	} else {
		return $value;
	}
}

The stf_{$post_type} is similar to stf_($type}, 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 programmically:

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

Deprecated: 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 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. See the stf_include filter for the replacement.

Keeping 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 the stf_exclude_scripts 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.

Custom Post Type Support

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' );

Custom Taxonomy Support

The scripts_to_footer_taxonomies filter can be used to change the taxonomies the plugin is applied to (it applies to pages and posts 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 metabox via the post type 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' );