Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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.

share|improve this question
    
What's your actual problem/question? –  Chip Bennett Jan 15 at 22:29
    
@ChipBennett Please read everything. Basically Im not sure about calling the rm_head_cleanup function in the after_setup_theme. And also about calling this: rm_add_theme_support(); –  Richard Mišenčík Jan 15 at 22:34
2  
Richard, that isn't a real question though. Questions are things that can have actual concrete answers. The only real answer anybody here will give you is that all that code is mostly unnecessary. You don't need to remove all those actions and filters and only hurt your site by doing such. –  Otto Jan 15 at 22:40
    
@Otto The question is, is this the right way or should it be done differently? I know its maybe not a specific question but I would like to understand how this works. So which ones I dont need? And which ones are hurting the site? –  Richard Mišenčík Jan 16 at 10:14
add comment

1 Answer

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.

share|improve this answer
    
Yeah I know about that but Im not going to create a plugin for everything, I have theme specific code there, and thats what there is functions.php file for. And I dont like to depend on a plugin to do all this stuff. update_option is only run after_theme_setup or not? Well Im also remove the other sizes, and then add my custom one also to keep the name consintency. Okay so where should I put the register menus then? Im using a custom function to display the wp_nav_menu, could i put the register_menus there? –  Richard Mišenčík Jan 16 at 10:23
    
@RichardMišenčík You asked for a clean-up. Removing non-theme code is the first step. after_theme_setup is not a theme installation hook, it is called on every page load. You can change existing sizes, that’s better than removing the default sizes which might be used by plugins too. Register the menu in a separate function. –  toscho Jan 16 at 11:20
    
Yeah but wp_head cleanup function not code cleanup. Okay but when you check here: codex.wordpress.org/Plugin_API/Action_Reference/… there is also add_theme_support, registering menus etc. in the after_theme_setup function. Im removing the default sizes because they are being created automatically from the images you upload, correct? I dont need these sizes, they are just waste of space and regarding plugins, Im not using any plugin so nothing will be used by plugins. So should I hook register_menus to add_action(init) like the head_cleanup()? Thanks. –  Richard Mišenčík Jan 16 at 11:47
    
@RichardMišenčík Please, please ignore code samples from Codex! They are very often questionable and show at best, how it could be done, not how it should. You can change the default sizes and use those. I see no need for custom sizes. Menus can be registered on wp_loaded. See also this example (a little bit outdated). –  toscho Jan 16 at 20:18
    
can I also change the default names? because when using it with custom post types, its not very good to use thumbail_large or thumbnail_small when the name is not in the reference to the actual size. For menus i changed it like this: if(function_exists('register_nav_menus')) { register_nav_menus( array(...) ) ); }. For the update_option i did something like: if (is_admin() && isset($_GET['activated']) && $pagenow == 'themes.php') { ... so i only delete and register the new thumbnail sizes on when the theme is activated. –  Richard Mišenčík Jan 17 at 13:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.