Menu

RTCXpression

Close

Function Snippets for WordPress – Fewer Plugins are Needed

function snippets One of the best things you can do with WordPress is to replace as many plugins as possible with some type of function manager. You should try keep the number of plugins as low as possible. Every plugin is a potential vulnerability and adds to the amount of code that has to be executed each time WordPress loads a page.

Many plugins can be reduced to snippets you can either add to your theme’s function file or to another dedicated plugin (a function manager). I prefer the dedicated plugin method – you’re basically combining multiple plugins into one. Code Snippets is my current favorite.

Replace Plugins with Function Snippets

Instead of using a lot of small plugins, you can copy the code to a function and add it to your function snippets. This only works well if they’re simple plugins, consisting of simple functions.

I use a snippet instead of a plugin to display AdSense ads. It’s a lot easier to edit code than to fill in the blanks in someone else’s plugin.

Function Snippets

I can’t show you everything I use. Some functions are very specific to my server environment. Here’s one to add certain items to the head of each page:

function head_additions() {
    $output = '<meta name="robots" content="noarchive,noodp,noydir" />' . "n" .
    '<meta name="google-site-verification" content="Google Webmaster Tools code" />' . "n" .
    '<meta name="msvalidate.01" content="Bing Webmaster Tools code" />' . "n" .
    '<meta name="p:domain_verify" content="Pinterest Code" />' . "n";
    echo $output;
}
add_action('wp_head','head_additions', 1);

Here’s another to remove certain items from the head of each page:

remove_action('wp_head', 'adjacent_posts_rel_link'); // Removes the relational links for the posts adjacent to the current post.
remove_action('wp_head', 'feed_links', 2); // Removes links to the general feeds: Post and Comment Feed
remove_action('wp_head', 'feed_links_extra', 3); // Removes the links to the extra feeds such as category feeds
remove_action('wp_head', 'index_rel_link'); // Removes the index link
remove_action('wp_head', 'parent_post_rel_link'); // Removes the prev link
remove_action('wp_head', 'rsd_link'); // Removes the link to the Really Simple Discovery service endpoint, EditURI link
remove_action('wp_head', 'start_post_rel_link'); // Removes the start link
remove_action('wp_head', 'wlwmanifest_link'); // Removes the link to the Windows Live Writer manifest file.
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);


Here’s one to disable pingbacks from post to post on the same site:

function disable_self_pingbacks(&$links) {
    foreach ($links as $l => $link)
        if ( 0 === strpos($link, get_option('home') ) )
            unset($links[$l]);
}
add_action('pre_ping', 'disable_self_pingbacks');

How many WordPress Plugins do you have Installed?

If you can lower your dependency on plugins, you’ll be doing well. I have about 15 I really can’t convert to function snippets.

I’ll be using all of them when I convert my self-hosted WordPress software to a static site generator. As a static site generator, the number of plugins really doesn’t matter.

By:
August 16, 2016

Categories:
Web Development

Previous and Next Articles:

« »

You May Also Like:

Comments:

Your comment will appear below the form when it's approved. When the page redisplays after hitting the send button (it can take a few seconds), your comment has been sent.

When replying to someone else's comment, please start the comment with "@" and the name so I can put it in the right place.

Books by William James Asberry
Comments Policy
Privacy Policy

RTCXpression established Feb 28, 2011
Copyright © 2013-2017 RT Cunningham