WordPress has these powerful bits of code, called functions, that add a unique function to your WordPress site. There are all sorts of functions for WordPress online, but today, I’m going to share some of my favorites and how to use them. That’s right! There isn’t going to be any, “now here’s some code, you figure out how to use it!” I’m hoping to simplify this process so that even beginners can use this!


Note, some of these functions are not my own, but rather functions that I’ve gathered over time. Some, however, are mine, so I hope you enjoy!

How to Implement Functions

First things first. How do you use functions in your site? Well, many people would say that you log into your site via FTP, go into your theme, find the functions.php file, and add your functions there. I, for one, do not recommend that. Why? It’s simple. If you ever update your theme, then your functions are definitely being overwritten! Congratulations, you’ve lost all of your previous work. That’s no good!

How do I implement functions? I build my own plugin. I’m sure that sounds complicated, but it’s really not!

1. Log into your site via FTP.
2. Navigate to wp-content > plugins and create a new directory. Name it whatever you want. I usually name it something like “sitename-plugin”.
3. Navigate into the directory you just created and create a new file.
4. Name the new file whatever you’d like, but I usually name it like the directory. So, “sitename-plugin.php”. Notice the “.php”. This is important to add!
5. Open this new file and add the following into it and save it (Note: Make sure you fill in the plugin name, description, etc.):

Plugin Name: Plugin Name Here
Description: Plugin description here.
Version: Version # here
Author: Author Name here
Author URI: Author URI here

// Functions start below this line


So, for example, it might look something like this:

Plugin Name: WPDevelopers Plugin
Description: Custom code changes for this site.
Version: 1.0
Author: Tyler Johnson
Author URI: http://tylerjohnsondesign.com

// Functions start below this line


6. Login to WordPress and from the Dashboard go to Plugins. If you did everything correctly, you should now see your plugin listed with the other plugins. Simply activate it so that it’s working.

7. Now, all you have to do is add any of the functions below between the “Add Your Functions Below This Line” line and the “Stop Adding Your Functions Below This Line”.

NOTEIn order to keep things organized, I use beginning “titles” for each function via a comment. Comments within PHP are denoted by beginning with a //. So, it would look something like this:

/** Comment Type 1 - Separates Functions **/
// Comment Type 2 - Notes what elements do particularly

This allows you to easily see where each function begins and ends and allows others to know what each elements is doing at a glance in case you need to edit or remove a function.

The Functions

Google Advertisement Within Post Content

The function below looks into your post content and inputs the advertisement after the specified paragraph. The number in the in the $count > 3 && $i == 2 section denotes in which position position in the content the ad will display.

In the function, insert your Google ad code in between the single quotation marks containing AD 1 CODE GOES HERE and AD 2 CODE GOES HERE. As a note, if your Google ad code contains any single quotation marks, such as ‘, this could break the code. That mark will be seen as the ending ‘ mark and will cause the code to stop short. This is easy to fix. Within the Google ad code, just change all ‘ marks to \’ marks and it will work perfectly.

AdSense in Content
function wpdev_add_adsense_content($content) {
  $ad1 = 'AD 1 CODE GOES HERE';
  $ad2 = 'AD 2 CODE GOES HERE';

  $content = explode('</p>', $content); // Break paragraphs into array
  $count = count($content); // Count paragraphs in array
  for($i = 0; $i < $count; $i++) {
    if($count > 3 && $i == 2) { // If more than 3 paragraphs, output ad at 2nd paragraph spot
      return $ad1;
    if($count > 6 && $i == 5) { // If more than 6 paragraphs, output ad at 5th paragraph spot
      return $ad2;
  return $content[$i] . '</p>';
add_filter('the_content', 'wpdev_add_adsense_content');

Create a Read More Button After Excerpt

Sometimes we want a read more button after excerpts. You can add one into the loop, but that can get messy. Simple solution? Add the function below. With the code below, it will display as a basic link after the excerpt. In order to make it a button, add some CSS for the class read-more-button.

Add Read More Button
function wpdev_new_excerpt_more( $more ) {
    return ' <a class="wpdev-read-more-button" href="'. get_permalink() . '"> Read More</a>';
add_filter( 'excerpt_more', 'wpdev_new_excerpt_more' );

Edit Excerpt Word Length

Once again, we’re editing the excerpt! This time, we’re editing the length, as you may want a little more, or less, than the default 55 word limit. Just use the code below and change # OF WORDS to how every many words you want displayed in your excerpts.

Edit Excerpt Word Length
function wpdev_new_excerpt_length($length) {
    return 20; // 20 denotes number of words, update according to your tastes
add_filter('excerpt_length', 'wpdev_new_excerpt_length');


Add Fields to User Profile Page

WordPress, by default, has only four fields available under “Contact Info” for the user profile. What if I want to add more? That’s easy. Just add the function below. Update the SLUG section with a slug that hasn’t been used, and edit the TITLE THAT DISPLAYS. You can make more than one field by copying the $profile_fields[‘SLUG’] = ‘TITLE THAT DISPLAYS’; line.

How do you display these fields? You can use the following for an author bio:

<?php get_author_meta( 'SLUG' ); // Output custom profile field ?>

Or you can read up on the following for a regular user. Here’s the function:

Add User Fields
function wpdev_modify_contact_methods($profile_fields) {
    $profile_fields['SLUG'] = 'TITLE THAT DISPLAYS'; // Update SLUG and TITLE THAT DISPLAYS
    return $profile_fields;
add_filter('user_contactmethods', 'wpdev_modify_contact_methods');

One Search Result Redirects to Post

If you search for something and only one result is returned in that search, instead of making the user then click the article, this theme automatically redirects them to that one result. Basically, it removes the middle man! And this function needs no set-up.

One Search Result Redirect
function wpdev_redirect_single_post() {
    if(is_search()) {
        global $wp_query;
        if($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {
add_action('template_redirect', 'wpdev_redirect_single_post');

Remove WordPress Version Number

So, this one might puzzle you. Why would I want to remove the version number? Because, it can cause a security risk. You always want to update to the latest version of WP for patches to security, but if your client doesn’t want to upgrade, or if you can’t, it’s important to remove the version number. Hackers can use the version number to hack into your site, so removing it makes it just that much harder. Thankfully, the function is simple.

Remove WP Version Number
function wpdev_remove_version() {
    return ''; // Leave empty to return nothing
add_filter('the_generator', 'wpdev_remove_version');

Remove URL Field From WordPress Comments Box

Why is this useful? Well, who really uses this anyway? I don’t know. I find it kind of annoying. And you know what, removing it can reduce spam. Who woulda thunk it!? Just pop this little, old function in and your URL field will be removed the comment form.

Remove URL from Comments Box
function wpdev_remove_comment_fields($fields) {
    unset($fields['url']); // Unsets the field
    return $fields; // Returns the fields without the URL field

The End

So, that’s it. Those are some of my favorite/most used functions. What are some of your favorites?