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.):
<?php /* Plugin Name: Plugin Name Here Description: Plugin description here. Version: Version # here Author: Author Name here Author URI: Author URI here */ /* Add Your Functions Below This Line */ /* Stop Adding Your Functions 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”.
NOTE: In order to keep things organized, I use beginning “titles” and ending lines for each function via a comment. Comments within PHP are denoted by beginning with a //. So, it would look something like this:
/* Add Your Functions Below This Line */
//Adds User Fields <function goes here> //End Adds User Fields
This allows you to easily see where each function begins and ends, 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 $ad_code, 2, $content) section denotes after which paragraph the ad will display. So, if you want it to display after the 5th paragraph, change the number from 2 to 5.
In the function, insert your Google ad code in between the single quotation marks containing GOOGLE AD 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.
// Add Ads in Post Content - Google Ad Code
function prefix_insert_post_ads( $content ) {
$ad_code = 'GOOGLE AD CODE GOES HERE';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
// Add Ads in Post Content - Function *Do Not Edit*
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
// END Add Ads in Post 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.
// Creates a Read More Button After Excerpt
function new_excerpt_more( $more ) { return ' <a class="read-more-button" href="'. get_permalink( get_the_ID() ) . '"> Read More</a>'; } add_filter( 'excerpt_more', 'new_excerpt_more' );// END Creates a Read More Button After Excerpt
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 new_excerpt_length($length) { return # OF WORDS; } add_filter('excerpt_length', 'new_excerpt_length');// END Edit Excerpt Word Length
Make iFrames Responsive
This function makes iFrames responsive. It works well! The only issue with it is that it only affects the width of the iframe, and not the height. But, it’s not a huge bit of code, like most responsive video plugins, so it’ll keep your site running smoothly.
How do you get it to work? Well, that’s the only part that’s a bit tricky. You need to find out the maximum width of your post content. Basically, how wide do your posts extend out? Once you know that, it’s simple. Just replace the part that says CONTENT WIDTH PX with the #px of your content width and you’re ready to go!
// Sets iFrames Width to Be Responsive
if ( ! isset( $content_width ) ) $content_width = CONTENT WIDTH PX;// END Sets iFrames Width to Be Responsive
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'); ?>
Or you can read up on the following for a regular user. Here’s the function:
//Adds New Fields to User Profile
function modify_contact_methods($profile_fields) { $profile_fields['SLUG'] = 'TITLE THAT DISPLAYS'; return $profile_fields; } add_filter('user_contactmethods', 'modify_contact_methods');// END Adds New Fields to User Profile
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.
// Search One Result Redirect to Post
add_action('template_redirect', 'redirect_single_post'); function redirect_single_post() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) { wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); exit; }}}// END Search One Result Redirec to 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 WordPress Version Number
function wpbeginner_remove_version() { return ''; } add_filter('the_generator', 'wpbeginner_remove_version');// END Remove WordPress Version Number
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 Comment Form
function remove_comment_fields($fields) { unset($fields['url']); return $fields; } add_filter('comment_form_default_fields','remove_comment_fields');// END Remove URL from Comment Form
The End
So, that’s it. Those are some of my favorite/most used functions. What are some of your favorites?