PHP functions for WordPress

Below you can find a list of source code snippets for very useful PHP functions for wordpress. You can use each one of the following parts of source code in your functions.php file:

How to Increase or Decrease WordPress JPEG Image Compression

WordPress does automatically compress JPEG images at 90% quality. In this article, we will show you how to increase or decrease WordPress JPEG image compression.

All you need to do is paste the following code in your theme’s functions.php file or your site-specific plugin.

add_filter('jpeg_quality',function($arg){return 100;});

Setting it to 100 would mean that WordPress would compress the image at its highest quality. For most users, this is not an issue. Heck, we never noticed it on our site. But if you are a photographer, then this is noticeable (we suppose).

There are definitely performance benefits to leaving the compression quality as is. If you want, you can change the number from 100 to 80 or something lower to squeeze a few more kBs.

Replace Excerpt Ellipse

A lot of people really do not like that WordPress puts in excerpts an ellipse. An ellipse is famously seen as […]. A lot of times this does not have a URL. For those who use excerpts, you can replace the More or ellipse with a filter in your functions.php file of your theme. For example, I use Read More and have the article’s permalink anchored.

function excerpt_ellipse($text) {
return str_replace('[...]', ' Read More →', $text); }
add_filter('the_excerpt', 'excerpt_ellipse');

Add Google Analytics

Simply paste the code below and insert your Google Analytics where it says paste your Google Analytics. You can paste the code once in your functions.php file and never have to worry about it again. We are adding an action to the wp_footer, so it will automatically insert adsense codes wherever on all pages you have the wp_footer string.

add_action('wp_footer', 'add_google_analytics');
function add_google_analytics() { ?>
// Paste your Google Analytics code from here
<?php } ?>

Add a Favicon to your Blog

Every blog deserves to have its own identity. You can add this identity by adding the favicon code in your header.php file, or you can make it easier for yourself by simply adding the following code in your functions.php file.

// add a favicon to your
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'" />';
add_action('wp_head', 'blog_favicon');

Now whenever you are developing the theme, just upload the .ico file in the root folder where the blog is uploaded. You can also modify the file destination if you like. Often theme developers forget to change the URL of the favicon. This can eliminate another worry.

Remove WordPress Version Number

You should always encourage your clients to upgrade to the latest version, so you don’t have this problem. But if you are working with a client that does not want to upgrade, then it is essential that you remove your WordPress version number from your WordPress header, RSS feeds, and all other locations. To do this, add the following code:

function wp_remove_version() {
   return '';
add_filter('the_generator', 'wp_remove_version');

Add a Custom Dashboard Logo

When creating themes for a client, you can use this as one of the perks to the theme. All you have to do is paste the following code below:

//hook the administrative header output
add_action('admin_head', 'my_custom_logo');
function my_logo() {
   echo '<style type="text/css">#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/my-logo.gif) !important; }</style>';

Change the Default Gravatar in WordPress

Have you seen the default Mystery man avatar on blogs? Why waste the branding opportunity for your clients? You can replace the mystery man with a custom branded gravatar for your clients to give their site a unique touch. All you have to do is paste the following codes:

add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
   $myavatar = get_bloginfo('template_directory') .'/images/gravatar.gif';
   $avatar_defaults[$myavatar] = "WPTechjam";
   return $avatar_defaults;

Dynamic Copyright Date in WordPress Footer

Often you will come across sites with outdated copyright dates. Some sites show the current year as their copyright date. Both of these are annoying, and it shows that the site designer was lazy. In order to give your users a little background info about your site, you should display the copyright date as such: © 2006 – 2010. We can do this by simply pasting the following code:

function footer_copyright() {
   global $wpdb;
   $copyright_dates = $wpdb->get_results("
   YEAR(min(post_date_gmt)) AS firstdate,
   YEAR(max(post_date_gmt)) AS lastdate
   post_status = 'publish'
   $output = '';
   if($copyright_dates) {
      $copyright = "&copy; " . $copyright_dates[0]->firstdate;
      if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
         $copyright .= '-' . $copyright_dates[0]->lastdate;
      $output = $copyright;
   return $output;

Once you add this function, then open your footer.php file and add the following code wherever you like to display the dynamic copyright date:

<?php echo footer_copyright(); ?>

This function looks for the date of your first post, and the date of your last post. It then echos the years wherever you call the function.

Don’t forget to upload a custom image to your theme’s image folder. Also change the name of the gravatar to their brand name. Once you upload the image and the functions, then visit: WP-Admin » Settings » Discussion

For more code utilities that can be incorporated inside the functions.php file of your WordPress installation visit the social snippets article

Written By

Panagiotis (pronounced Panayotis) is a passionate G(r)eek with experience in digital analytics projects and website implementation. Fan of clear and effective processes, automation of tasks and problem-solving technical hacks. Hands-on experience with projects ranging from small to enterprise-level companies, starting from the communication with the customers and ending with the transformation of business requirements to the final deliverable.