If you like me, you most likely use a lot of the same functions in WordPress each time you create a new theme. Functions for adding theme support or enqueuing files are part of every theme developers toolkit. Recently while looking into CSS reset / normalize files I started thinking about creating a functions.php reset file.

There are so many functions it is likely you either already have your own custom prefilled functions file or run the risk or forgetting to add functions.

I decided to go over my own reset functions.php file and compare them with 40+ free WordPress themes. I quickly realized the idea of a global reset functions.php file was doomed to fail. Each site is different with unique requirements. This leads theme developers too often enable as many options as possible.

My goal was the opposite. I wanted to enable only the bare minimum while removing anything I was not using.

In this article, I’m going to share my reset functions file and briefly go over what each function is doing.

Clean wp_head

I start by removing what I don’t need from the wp_head function. Most important is removing the WordPresss version information. WebDesignViews has a great article that explains in details what each function does and how to remove them. Make sure to double check if you actually use the function before removing it.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Head Clean Up */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('wp_head', 'rest_output_link_wp_head', 10 );

Add Theme Support

WordPress comes with a ton of features but often you have to specifically enable support for certain features. I actually prefer this approach when creating new themes over enabling everything by default and having to remove a lot of unwanted code.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Add theme support */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

add_theme_support( 'title-tag' );
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) );
add_theme_support( 'automatic-feed-links' );

add_theme_support( 'post-thumbnails' ); 
// add_image_size( 'name', 9999, 9999 );

Remove ?ver jQuery

Ever view your page source and notice a lot of your CSS and jQuery end with a ?ver= query? Removing query strings from URLS will help to ensure static resources get cached. Adding this function to your functions file will help cache those files resulting in better performance.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Remove ?ver From Enqueued Files */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function wpfr_remove_ver_query( $src ){
    return remove_query_arg( 'ver', $src );
}
add_filter( 'script_loader_src', 'wpfr_remove_ver_query' );
add_filter( 'style_loader_src', 'wpfr_remove_ver_query' );

Custom Login Error Message

By default when logging into WordPress with the incorrect details WordPress will actually tell you which part of your username / password combo was incorrect. Enter the wrong username and WordPress will return ‘Error, invalid username’. Login with the wrong password and WordPress will return ‘Error, password is incorrect’. Bruteforce hack attempts can try different usernames until it gets the password incorrect message, confirming they now know your username. To get around this simply change the login message to always be the same message.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Custom Login Error Message */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function wpfr_remove_login_errors(){
  return 'Login unsuccessful, please try again!';
}
add_filter( 'login_errors', 'wpfr_remove_login_errors' );

Add Shortcode Support to Widget Areas

Shortcodes provide an easy way to embed custom functionality to your website. Unfortunately, by default WordPress sidebar widgets don’t accept shortcodes. Adding this line to your functions file will enable shortcode support in WordPress widget areas.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Add Shortcode Support to Widgets */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

add_filter('widget_text','do_shortcode');

Enqueue

Loading CSS or JS to your theme should always be done using the wp_enqueue functions. This includes your style.css, a required file for every WordPress theme. This means at very least you’d need to enqueue your style.css file.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Enqueue Stylesheet */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function wpfr_load_scripts() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'wpfr_load_scripts' );

Honourable Mentions

As mentioned previously WordPress has a lot of features. I tried to strip this down to the absolute bare minimum, realistically though you going to want to add menus, sidebars etc.

// Add translation support
load_theme_textdomain( 'myth', get_template_directory() . '/languages' );

// Register a menu position
register_nav_menu( 'main', 'Main Menu' );

// Register a sidebar widget area
function wpfr_widgets_init() {

	register_sidebar( array(
		'name' => __( 'Sidebar', 'wpfr' ),
		'id' => 'sidebar',
		'description' => __( 'Main Sidebar Widget', 'wpb' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

}

add_action( 'widgets_init', 'wpfr_widgets_init' );

// Add theme support for post formats
add_theme_support( 'post-formats', array('aside','image','video','quote','link',) );

// Set max width for content & embedded content
lobal $content_width;
if (!isset($content_width)) {
    $content_width = 1168;
}

Final Reset Functions.php File

Hopefully, this has helped you, feel free to add your thoughts and suggestions for what should be included in a reset functions.php file. Below is everything together excluding the honourable mentions.

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Head Clean Up */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('wp_head', 'rest_output_link_wp_head', 10 );


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Add theme support */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

add_theme_support( 'title-tag' );
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) );
add_theme_support( 'automatic-feed-links' );

add_theme_support( 'post-thumbnails' ); 
// add_image_size( 'name', 9999, 9999 );


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Remove ?ver From Enqueued Files */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function wpfr_remove_ver_query( $src ){
    return remove_query_arg( 'ver', $src );
}
add_filter( 'script_loader_src', 'wpfr_remove_ver_query' );
add_filter( 'style_loader_src', 'wpfr_remove_ver_query' );



/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Custom Login Error Message */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function wpfr_remove_login_errors(){
  return 'Login unsuccessful, please try again!';
}
add_filter( 'login_errors', 'wpfr_remove_login_errors' );


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Add Shortcode Support to Widgets */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

add_filter('widget_text','do_shortcode');


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* Enqueue Stylesheet */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

function wpfr_load_scripts() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'wpfr_load_scripts' );