settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How can I clean unnecessary code in WordPress wp_head file?

+2 votes

Hi, I would like to clean my code in my WordPress theme.

I know that in wp_head there are some functions which trigger lines of code - not very useful so they can be deleted;

Can you please share how can I clean my wp_head from unnecessary code and maybe share some additional cleaning WP tricks...

Thanks

asked in Web Development category by user andrew

1 Answer

+1 vote

First, you must know that if you don't use a selfmade theme or child theme the below shared changes will be overwritten with the next theme update!

To prevent this: - create a child theme + use the functions.php file there or make a small plugin out of the code below:

//custom functions:
remove_action('wp_head', 'rsd_link'); // Display the link to the Really Simple Discovery service endpoint, EditURI link
remove_action('wp_head', 'wlwmanifest_link'); // Display the link to the Windows Live Writer manifest file.
remove_action('wp_head', 'wp_generator'); // Display the XHTML generator that is generated on the wp_head hook, WP version
remove_action('wp_head', 'print_emoji_detection_script', 7); //Removes the emoji code
remove_action('wp_print_styles', 'print_emoji_styles'); //Removes the emoji code 2
remove_action('wp_head', 'feed_links_extra', 3); // Display the links to the extra feeds such as category feeds
remove_action('wp_head', 'feed_links', 2); // Display the links to the general feeds: Post and Comment Feed
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // prev link
remove_action('wp_head', 'start_post_rel_link', 10, 0); // start link
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // Display relational links for the posts adjacent to the current post.
remove_action('wp_head', 'index_rel_link'); // index link

// Remove pingback link  
if (!is_admin()) {
    function link_rel_buffer_callback($buffer) {
        $buffer = preg_replace('/(<link.*?rel=("|\')pingback("|\').*?href=("|\')(.*?)("|\')(.*?)?\/?>|<link.*?href=("|\')(.*?)("|\').*?rel=("|\')pingback("|\')(.*?)?\/?>)/i', '', $buffer);
        return $buffer;
    }

    function link_rel_buffer_start() {
        ob_start("link_rel_buffer_callback");
    }

    function link_rel_buffer_end() {
        ob_flush();
    }

    add_action('template_redirect', 'link_rel_buffer_start', -1);
    add_action('get_header', 'link_rel_buffer_start');
    add_action('wp_head', 'link_rel_buffer_end', 999);
}

Please read the comments - every line of code is explained there.

answered by user john7
...