Tag Archives: functions.php

How to Add Meta Descriptions to WordPress Themes Using functions.php

While working on an SEO for WordPress course recently, I was looking for a way to manually add meta descriptions to a theme from the functions.php file.

After playing around a bit, I came up with this solution:

This of course raises the bigger question of why would you want to do this instead of using a plugins like SEO by Yoast or All in One SEO.  I don’t think that you actually would :p I just wanted to demonstrate how to do it.

If you have a more elegant solution, please suggest away!

How to Deregister, Unqueue and Unhook a Parent Theme CSS Stylesheet

There comes a time when working with a WordPress project where you want to not include certain CSS from a parent theme or plugin.  For example, a parent theme may include extra stylesheets that you don’t want or a plugin may be adding styles that conflict with your site.

On a recent project, I wanted to remove the parent styles just on a certain section of the site.  Here’s how to do it.

First what you have to do is find the handle for the parent stylesheet you no longer want.  Let’s say for example that we had a twentyfifteen child theme and wanted to remove the parent styling.

You would go to the parent theme’s functions.php file and find the hook.  It can help to search for “wp_enqueue_style” to find the CSS includes faster.  Here is what it looks like in twentyfifteen:

TwentyFifteen Theme Functions.php Including Stylesheet

We can see the handle is “twentyfifteen-style” and this is what we will now use to dehook it using the code below:

function unhook_parent_style() {

  wp_dequeue_style( 'twentyfifteen-style' );
  wp_deregister_style( 'twentyfifteen-style' );

}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );

It’s important that we add this code into a function and then hook the function into wp_enqueue_scripts.

If you only want to unhook the styles on certain pages, you can also add conditional code like this:

function unhook_parent_style() {

  is_page_template( 'custom-template.php' ) {
    wp_dequeue_style( 'twentyfifteen-style' );
    wp_deregister_style( 'twentyfifteen-style' );
  }

}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );

This will only unhook the parent styles when WordPress is loading pages that use the custom-template.php file. You can also use any of these conditional statements as well.

Shout out to Brad Dalton over at WP Sites for the original code snippet on How To Deregister & Dequeue Style Sheets!

How to Send Emails in WordPress from the Functions.php

I have been working on a project that needs to occasionally send off emails. If you need to do so, here’s how with wp_mail():

add_filter(‘wp_mail_content_type’,create_function(”, ‘return “text/html”; ‘));
wp_mail(‘whoever@whatever.com’, ‘Subject’, ‘Message’);

Thanks to WordPress Cheat Sheets for the code!

The Difference Between WordPress Filters and Actions

Thanks to Otto for making the difference between actions and filters clear:

Filters filter things. Actions do not. And this is critically important when you’re writing a filter. A filter function should never, ever, have unexpected side effects.

There, you go, pretty straightforward!  He actually makes some really good, concise points about the difference between these terms and as he says: please learn this lesson well.