Proper after_setup_theme and wp_head cleanup

Im trying to clean up code in my theme a little bit, remove some of the code that is not used and basically do things the right way, to decrease load time etc.

Currently I have this code:

Read More
add_action('after_setup_theme', 'rm_theme_setup');

// start theme setup
function rm_theme_setup() {
    add_action('init', 'rm_head_cleanup'); // * 1) calling the function below
    add_action('wp_head', 'rm_remove_recent_comments_style', 1);
    add_filter('gallery_style', 'rm_remove_gallery_style');

    rm_add_theme_support(); // * 2) calling the function to add theme support
}

1) This is the wp_head cleanup part: Basically Im calling this function in the theme_setup function above. Is this the correct way to do things?

function rm_head_cleanup() {
    remove_action('wp_head', 'feed_links_extra', 3);
    remove_action('wp_head', 'feed_links', 2);
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    remove_action('wp_head', 'index_rel_link');
    remove_action('wp_head', 'parent_post_rel_link', 10, 0);
    remove_action('wp_head', 'start_post_rel_link', 10, 0);
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_head', 'wp_shortlink_wp_head');
    add_filter('style_loader_src', 'rm_remove_wp_ver_css_js');
    add_filter('style_loader_tag', 'rm_clean_style_tags');
    add_filter('script_loader_src', 'rm_remove_wp_ver_css_js');
}

2) Then later in the code I have this function, to add theme support. When you check the theme setup function, im just doing this: rm_add_theme_support();. Is this the correct way to do it, or should I just copy everything from this fuction and paste it to theme_setup?

function rm_add_theme_support() {
add_theme_support('post-thumbnails');

// remove the default thumbnails
update_option('thumbnail_size_h', 0);
update_option('thumbnail_size_w', 0);
update_option('medium_size_h', 0);
update_option('medium_size_w', 0);
update_option('large_size_h', 0);
update_option('large_size_w', 0);

// set_post_thumbnail_size(200, 200, true);
add_image_size('rm-slide', 960, 500);
add_image_size('rm-service-image', 380, 9999);
add_image_size('rm-service-thumbnail', 80, 60, true);

add_theme_support('menus');

register_nav_menus(
    array(
        'main-nav' => 'Main menu'
    )
);

}

Thanks for all the help, hopefully this is not too much code.

Related posts

1 comment

  1. What you should do:

    • Read Where to put my code: plugin or functions.php? and apply the lessons to your theme.

      Themes are for presentation only. Remove everything not related to presentation: do not touch link and meta elements in the header. That’s plugin territory.

    • Remove redundant code. add_theme_support('menus'); is not necessary, if you call register_nav_menus(). update_option() should not be called on every page load. Check the existing options first, and change it only if you have to.

    • add_theme_support('post-thumbnails'); is questionable, if you remove the thumbnail size. Why don’t you just change it to 80x60? Then the thumbnails will still be available after a theme switch. This could be done with the other sizes too.

    should I just copy everything from this function and paste it to theme_setup?

    No, keep separate tasks in separate functions or classes. Do not mix different tasks just to save some lines of code. It is easier to disable specific features with a plugin, when you follow the rule Separation of concerns. rm_add_theme_support() does too much already, because menus are not related to image sizes.

Comments are closed.