10 Sep

Useful WordPress Code Snippets vol.1

Wordpress functions.php code

Here’s our first collection of WordPress Code Snippets which deserve a place in every WP developer’s toolbox. These are snippets of code that I myself find useful and use frequently.

Alot of the functionality from these code snippets can be done via plugins, but in the interest in keeping it simple and your installed plugin count down, it’s often better to simply paste these into your theme.  Unless stated otherwise, these snippets belong in your theme’s functions.php file.

1. Limit Max Width of Youtube Embeds

When faced with the issue of seeing Youtube videos I had embedded into a WP site were displaying with a width wider than that of the content area, I was surprised to see the maximum width for videos wasn’t in the core settings.

Drop the below snippet into your theme to overcome this problem, and define your desired maximum video width on the 2nd line ($maxw).

2. Replace the Read More link

A simple snippet to put you in control of how the “read more” link is rendered. I made use of this as the default link was linking to a non-existant named anchor within a page, which resulted in Google Webmaster Tools detecting them as broken links.

3. Hide Admin Bar

I find it useful, but some find it annoying – so here’s how you can do away with the WP Admin Bar at the top of the page, with the option to remove from the front-end only.

4. Limit or Disable Post Revisions

This goes into wp-config.php file. Post revisions can be handy to refer to older version of a post, but there’s no default limit and many post revisions for every post can lead to a bulky database.

5. Boost WP PHP Memory Limit

This goes in your wp-config.php file. By default, WordPress limits it’s PHP memory usage to just 40mb. This limit is easily exhausted as some plugins can require a fair amount of memory.

I was asked recently to fix a broken WP site which was only outputting blank pages with no error shown (always a fun one to try and fix!) – and it was a result of the site exceeding this memory limit, and was fixed with the below snippet.

Just be aware your host may have PHP limits in place also (which WP can’t override), particularly if it’s a cheap shared hosting service.

Reference:  WP Codex

6. Auto Link Twitter Usernames

This can save a lot of manual work if your content refers to twitter users. Speaking of twitter, follow us: @codepeach

source:  snipplr.  author: ederwp.

7. wp_head() Spring Cleaning

There’s alot of elements added to the <head> of WordPress generated pages by default, alot of which is often unused, so let’s clean it up.

You can use whichever (or all) of the following lines to remove what you want gone – I recommend at least using the wp_generator line as this shows the WordPress version number – which makes life easier for hackers.

source:  themelab

8. Don’t Link URLs in Comments

WP by default will convert any URLs found in comments to clickable links (anchor tags), this will disable that default functionality and keep them as plain text:

9. Change Excerpt Length

This shows how to limit the excerpt length to 80 characters (change that value to your heart’s desire):

10. Use Widgets Anywhere

You can include a widget in a template file, using the_widget().

Usage: the_widget($widget, $instance, $args );
For full documentation on this function, see the WP Codex.

Examples:

 

 

And for a more advanced example, the below snippet displays a RSS feed using the RSS Widget’s functionality.
source:  shailan.com

 


I hope you find this first collection of WordPress code snippets from CodePeach useful – please leave your feedback below as we’d love to hear from you.

We will be releasing collections of useful snippets regularly so stay tuned for the next volume!

 

  • Simon Rapin

    Hi, if you set the $content_width global variable, the embeds won’t be larger than that.

    if (!isset($content_width)) { $content_width = 1140; }

    More infos : http://codex.wordpress.org/Content_Width

    • Hi Simon, you’re spot on – and that’s definitely the preferable approach to take.

      Unfortunately it had no effect on a particular site I was asked to do some work on, so I came up with the alternative method I’ve posted which worked. On further investigation I’ve found the theme was setting $content_width value elsewhere, after functions.php was executed, overwriting the value I’d set.

      cheers :)