Podcast Episode

277 – 10 Handy Snippets to Add to Your WordPress Site

Announcements

WPForms.com
WordPress 4.5 Beta 4

Is there a plugin for that?

With more than 50,000 plugins in the WordPress repository, it’s hard to find the perfect one. Each week, I will highlight an interesting plugin form the repository.

For more great plugins, download my 50 Most Useful Plugins eBook.

WPBackItUp Backup & Restore is a simple & complete backups of your WordPress website with this easy to use plugin.

10 Handy Snippets to Add to Your WordPress Site

Add Google Analytics

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

Change Footer in WP-Admin

function remove_footer_admin () {
echo 'Fueled by WordPress | Designed by Uzzz Productions | WordPress Tutorials: WPBeginner

'; } add_filter('admin_footer_text', 'remove_footer_admin');

Create a dynamic copyright

function current_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

Then you will need to add this code to your footer:

function donate_shortcode( $atts ) {
extract(shortcode_atts(array(
'text' => 'Make a donation',
'account' => 'REPLACE ME',
'for' => '',
), $atts));

global $post;

if (!$for) $for = str_replace(" "," ",$post->post_title);

return ''.$text.'';

}
add_shortcode('donate', 'donate_shortcode');

Delay publishing to RSS

function publish_later_on_feed($where) {
global $wpdb;

if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);

// value for wait; + device
$wait = ‘10′; // integer

// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

// add SQL-sytax to default $where
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
}
return $where;
}

add_filter(‘posts_where’, ‘publish_later_on_feed’);

Customize Excerpt More […]

// custom excerpt ellipses for 2.9
function custom_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'custom_excerpt_more');

add_filter('wp_trim_excerpt', 'custom_excerpt_more');

Custom Length Excerpt

function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

Require a featured image before you can publish post

add_action('save_post', 'wpds_check_thumbnail');
add_action('admin_notices', 'wpds_thumbnail_error');

function wpds_check_thumbnail($post_id) {

    // change to any custom post type 
    if(get_post_type($post_id) != 'post')
        return;
    
    if ( !has_post_thumbnail( $post_id ) ) {
        // set a transient to show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'wpds_check_thumbnail');
        // update the post set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));

        add_action('save_post', 'wpds_check_thumbnail');
    } else {
        delete_transient( "has_post_thumbnail" );
    }
}

function wpds_thumbnail_error()
{
    // check if the transient is set, and display the error message
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "

You must select Featured Image. Your Post is saved but it can not be published.

"; delete_transient( "has_post_thumbnail" ); } }

Block Dashboard for all Non-Administrators


add_action( 'init', 'blockusers_wps_init' );
function blockusers_wps_init() {
   if ( is_admin() && ! current_user_can( 'administrator' ) ) {
                    wp_redirect( home_url() );
                    exit;
            }
    }

Change admin post/page color by status – draft, pending, published, future, private

add_action('admin_footer','posts_status_color');
function posts_status_color(){
?>
<style>
.status-draft{background: #FCE3F2 !important;}
.status-pending{background: #87C5D6 !important;}
.status-publish{/* no background keep wp alternating colors */}
.status-future{background: #C6EBF5 !important;}
.status-private{background:#F2D46F;}
</style>
<?php
}

Thank You!

Thank you to those who use my affiliate links. As you know I make a small commission when someone uses my link and I want to say thank you to the following people. For all my recommended resources, go to my Resources Page

Full Transcript

Business Transcription is provided by GMR Transcription.

For today's episode we're going to talk about ten handy snippets to add to your WordPress site right here on Your Website Engineer Podcast Episode No. 277.

Hello, everybody. Welcome back to another episode of Your Website Engineer Podcast. My name is Dustin Hartzler, and today we are going to talk a little bit about customization. We're going to talk about some snippets just to give you a brief idea of some of the cool things that you can do to kind of automate the process behind the scenes when it comes to WordPress. But first, of course, as always, we're going to start off with the news and what's happening in the WordPress space this week. I do want to let you know that WordPress 4.5 Beta 4 is now available. Obviously this software is still in development.

We're shooting for an April release of this software, and so we're getting real close to the release candidate versions. We're fixing bugs. They're doing all kinds of things. There's been more than 100 changes since I reported last week. Now one of the biggest things that has been added is moments and timelines from Twitter are now imbeddable on your WordPress website, which is really cool. Then another really cool part that I'm excited for is the ability to add a horizontal rule, which is normally a HTML – it's an [HR]. You can now add that in the editor by just adding dash, dash, dash, which is really cool. So that's just helping to speed up the process when you're creating your content on your WordPress website.

There's a bunch of other stuff that has been updated, and things have been changed in this version. If you're interested, head on over to the show notes for Episode No. 277, and you can read everything over there. I'm not going to go through everything here at the top of this episode. Another thing that I saw that came out this week was a new plugin that is for form builders, and it's called WP Forms. It is a drag and drop WordPress form builder. So if this is something that you're not a big fan of Gravity Forms or Contact Form 7 isn't quite what you're looking for when it comes to building forms, then WP Forms might be the perfect thing for you. You can find out more at wpforms.com.

One site's going to cost you $39.00. The Plus version is a growing business. It does up to three sites, is $100.00. And the Pro version is unlimited sites, with one year worth of updates for $199.00 per year. So if you're in the market for a different forms plugin, definitely check out wpforms.com. Alright, moving right along, I want to highlight a plugin this week because there's more than 43,000 plugins. There's a lot of duplicates in there, and I just want to highlight a new plugin every single week to just give you information that there's new plugins out there. And this one is called WP Back It Up – backup and restore – and it's another backup plugin.

So much like WP Forms is, is an alternative way to build forms, this is an alternative way to back up your website. Maybe there's certain functionality pieces that aren't in Backup Buddy, or doesn't work well with Back [inaudible] [00:02:50] Up or whatever. You might want to try WP Back It Up. This is a simple and complete backup solution for your WordPress website, and it's easy to use, and you don't have to configure anything within your C panel or your hosting platform. It all gets done within your WordPress configuration. So if you're interested in that, there'll be a link to that in the show notes for Episode No. 277 as well.

So today when we're talking adding custom snippets or handy snippets to add, you know there's – I could have had, instead of being ten, there could have been 100 million. This show could go on forever, for some of the cool little snippets that people have added to their WordPress website. And I kind of got thinking about this topic earlier today when I was troubleshooting a ticket that came in from Wu Commerce, and somebody was asking, "Is there a way that we can set a product to always be virtual and always be downloadable?" Because it always takes that extra time. Every time you create a new product, you have to check those two boxes. If you can eliminate that step, you're going to save 2 seconds each time you add a product.

But if you're creating ten products a day, you know, eventually you'll save a lot of time by just adding some customizations like that in. And this was something that, it was a short little function that was written and it could be put in a functions.php file. You also know, if you've been listening to this show for a long time, that I highly recommend using a custom functionality plugin, which basically is a plugin that goes into a folder called MU-Plugins, or you can just create your own custom plugin, and put that right in your plugins directory. Then that gives you the ability to have all your code working on your website, and if you ever change themes sometime, you're never going to have to copy that code out of your theme.

When you put code in a functions.php file, it leaves it within your theme, and then if you're trying to troubleshoot things and you have to turn off your theme, then you lose that functionality. It's always just best to keep the functionality of your website and what your website looks like completely separate. So anything that has to deal with the way that your website looks, you can put that inside your theme folder, and then anything that has to do with how your website functions, then you want to put that in some sort of plugin, if you will.

And I've talked about this in the past. I'm pretty sure there's a webinar replay if you head on over to yourwebsiteengineer.com/webinar-replays, I'm pretty sure there's one there about how to create a plugin. It's pretty simple. But basically, I wanted to highlight some neat little examples. Again, there's only ten because I didn't want this show to go on forever, and there's all code in the show notes. I'm not going to try to repeat the code and say bracket, question mark, PHP, underscore, action, all that stuff. I'm not going to try to do that on a show because it will be impossible for me to say, and it will be impossible for you to comprehend, and we would just be wasting each other's time.

So let's just go ahead and talk about some of these little snippets that you can add to either your functions.php file or your custom plugin that you may have created. So the first one is Google Analytics, how to add Google Analytics. And there's a short little four-line piece of code that you can add, that basically will allow you to add the code that Google wants you to add for your Google Analytics plugin. Basically it's a – I forget how long it is off the top of my head – it's maybe five or six lines of HTML that is supposed to be at the bottom of every single page.

What you can do is you can just run this little function that's going to load every time the footer loads of your website, and virtually that will put that tracking code on every single page on your website. You can put it there and then you can automatically forget about it because it's there and it's going to work perfectly every single time. Now there's some plugins or some themes that have this functionality built right in, and maybe all you have to add is like the UA code. Then someday down the road, if you change your theme, then there's a good chance that you may forget to change – you may forget to add that tracking code back in until the next time that you look at your Google Analytics account.

So that's the first handy snippet you can add, is a little code to automatically add Google Analytics to each and every page on your website. Another thing that you can do, and this is perfect if you are running a WordPress agency or WordPress shop and you have clients that regularly log in to their own dashboards, you can actually change the footer in WP Admin. So you can add this little function, and you can put whatever you want in there. So maybe you want it to say "Fueled by WordPress, designed by Hartzler Digital Media." Maybe you want to put a link to some of your WordPress tutorials or link to a handy guide where they can learn how to use their dashboard, or whatever that looks like.

Or you know, a button to get support, and it automatically takes them to the support page. Whatever that looks like, you can do that. It kind of takes away the footer text that's there. I believe right now it says, "Thanks for using WordPress," and it may say the version number that you're using, something along those lines. You can actually change that, and you can customize that, and you can make it very personal, and you can do some branding things with that. I've also seen people replace the WordPress logo in the upper left part of the admin toolbar, and put your own company logo or the company logo of the person's website that it is. So I've seen that done as well. I don't have any code to do that, but you can Google around and find that as well.

I probably should say that most of this code can be found by, what are you trying to do? You type in Google with the word WordPress, like what you're trying to accomplish. Say for this first one, change footer in WP-Admin. You type that in to Google, maybe add the word WordPress, your snippet, code, something like that, and it will come up with a filter or an action or something like that, that can be done to customize the look and feel of WordPress. The next one is pretty cool, and this gives you the ability to create a dynamic copyright. And at first when I saw this code, I was like, "Well, why would you ever want to do this? What benefit could this be?"

I know that in all of my themes, I always say, copyright, and then I just use a little bit of PHP, and it automatically kicks out the date, the current date that it is. Well this little bit of code will actually find you the starting date, or the date of the very first post that's been published in the year, and then use the current date or the last published post. So that would be pretty cool. For example, if I added this chunk of code on to my website, it would say, copyright 2010 through 2016, and that's because those are the years for – my first post was in 2010, and my last one, you know, the most recent one, has been in 2016.

So I think that's kind of cool. If that's something that you need that range, that dynamic range, then this may be something that you always add to all of your themes, and then it just goes ahead and gets that information for you. If you or the client wants to backdate a few posts, then it's going to give that copyright range of that full year, which I think is pretty cool. That's a pretty neat plugin. So I thought that – or pretty neat little snippet of code, so I thought that was worth mentioning today. Another one is create a PayPal donate short code.

And basically what you can do is you can get all the information from PayPal, when it asks you for – when it gives you all the information that you can post to create your button or whatever that may look like, and then you can go ahead and create a short code, which is basically like donates, or donate_short code, or whatever that is. And so if you have a business or you want to say at the bottom of a blog post, or if you wanted to put in random places to donate now, you can go ahead and just use that short code to donate, which I think is pretty cool. This code on my page, basically you just go ahead and change a few little things in there. There's a few little places to change the text, and then it automatically will just start working.

Another thing you can do, and I don't know if you'd use this that often, but this is something that could work, is something called delay publishing to RSS. So this basically would make sure that a set amount of time has passed before your post goes out to your RSS feed. You know, sometimes you make a typo. Once you hit Publish and you want to fix it real quick, and you want to make sure that the most corrected version goes out. You can set this for 10 minutes, 20 minutes, something like that, so you have enough time to look over it. You can see if you have any typos or things like that, and that will kind of delay things before sending out to an RSS feed.

You can do something like this maybe for a podcast, if you wanted to make sure that all the pieces of your content has published correctly before it goes out to iTunes or your RSS feed or whatever. You could use this little functionality as well. Again, these are just – I'm just kind of throwing out – this whole episode is just kind of throwing out different things that you can do that kind of aren't really standard WordPress things, but can all be done with just a little bit of code, which is really, really cool. The last two are ones that I've struggled with before in the past, before I found these little snippets of code. But basically the first one is you can customize the excerpt more button.

So I think right now by default, when you either use an excerpt on your home page instead of using the full page post, it will say "read more" and then it's got (…). And it's kind of annoying, if you will, like you can't really change that anywhere. Except if you add this little custom function, and you can basically – you can put anything in there. You know, I know that for my website, I might, on ones that are in the podcast category, I've programmed it to say, "Read show notes." If it's in a blog post category, then it's going to say, "Read full blog post," or something along those lines so I can customize that text based on the category that's being pulled.

So that's not in this snippet per se, but that's something you could do to kind of customize that text. And the last one is you can customize the length. Out of the box by default, I think it's 55 words, is what WordPress uses. So it takes the first 55 words and kind of truncates it or just chops it off at the end, and then that's going to be your excerpt, and then it's going to be followed up with the read more thing that we just fixed. So if you want it to be more, this is like a two-line function basically, or two lines of code. You basically put in the number of words that you'd like it to display, and then you add this to your functions.php or your custom functionality plugin, and it will just automatically do that.

I guess I do have a few more. I thought that was my last one. I do have a few more, and these are also pretty unique ones that are kind of cool actually. The first one is you can require a featured image before you can publish a post. This might be helpful if you have multiple authors that are trying to publish posts on your website. Maybe you're running a magazine, and you want to make sure that everyone has a featured image. You can add this custom functionality. You can add – this is maybe 20, 25 lines of code. Then it will make sure that something cannot be published unless it has a featured image, which I think is kind of neat.

It's kind of a neat way to kind of make sure that all the checks are in balance, and everybody has submitted an image along with the text for their blog post, which is kind of cool. Another one that you can do, and this may be another one. I would see using this, may be perfect if you were updating something on your website and you don't want people in the dashboard until you're all done. You could have these six lines of code or so, and it will basically block everybody from the dashboard except administrators.

So if you have subscribers that are trying to get into the dashboard to look at stuff, or maybe you have authors or any of the other content, any other levels that are beneath admin, like they have the ability to go in and it will just redirect them right back to their home page, which could be something, if you've got a little bit of downtime, you know, maybe you're down for 10 or 15 minutes, you could do this to make sure everybody stays out, so they're not messing with things while you're doing some sort of update. And the last one is kind of another neat one, and this one is really good if you do a lot of scheduled posts, if you have a lot of people on your team that you just want to easily be able to see things.

This gives you the ability to change the – on the post or the page, like the main – let's say if you go to all posts, and you see all of your posts listed there, you can change the color based on whether it's a draft, if it's pending, if it's published, if it's scheduled for the future, or if it's set as private, you can change those colors, and you can set that up. So I think that's kind of cool. So you can see at a glance, like, oh, these four that are red, they're pending. I should probably go in and check those and make sure that I can improve them so they can go out as published when they're scheduled to be published.

All right, so that's the ten that I wanted to share today. Again, that's not going to be – I'm missing hundreds and thousands of these little snippets that are living all over the Internet somewhere. But I just wanted to kind of give you an idea of some of the cool things you can do to really, I want to say automate, but automate's not really the best word. It's basically, it's doing stuff for you in the back end so you don't have to do it. So I guess that's kind of like automating. It just gives you the ability to not have to check those checkboxes every single time. You don't have to add this every single time you do a new blog post or whatever. It's just trying to make your life easier.

WordPress out of the box, it would be awesome if we could add all of these really cool functionality pieces in, that we think we need for our website, but we don't want to build that into everyone's website because then there's a lot of people that wouldn't use those custom functionality pieces, and so it would make WordPress bloated for those individuals. So we want to try to keep things as lean and mean as possible, and then we can add all the stuff that we need to within our own functions.php file, or start adding them in our own custom functionality [inaudible] [00:16:13]. So there's so many things. I'm going to add a couple of these that I suggested today to my website, and there's a few more that I'm like, I wonder if I can automate that.

I wonder if I can make this a little bit easier. It does take a little coding knowledge. It does take a little bit of skill to figure out how we can hook into WordPress and make changes and stuff like that, but there are so many tutorials online from people that are smarter than me, and that's where I go to look for me. Because I'm still learning how to code WordPress as well. So that's all I wanted to share with you this week. Take care. Bye-bye.

    • Micke K Reply

      Hi Dustin! Thanks for a great show. Just wanted to know what happened to the 10th snippet? The one I really wanted, the one about WooCommerce.

      Mar 24, 2016
    • Prescott Perez-Fox Reply

      Dustin, I hate to give you more homework, but can you indicate for each of these where it should go in your child theme? eg, functions.php, footer.php “before “, etc. I was able to recognise that most of these go in functions, but this is an ongoing pet peeve of mine with programming tutorials and I always appreciate when an author or community member thinks ahead to consider someone else who is less proficient.

      Mar 25, 2016
      • Dustin Hartzler Reply

        They can all go in functions.php or in a custom functionality plugin (which just means a plugin that you created).

        Mar 25, 2016

Leave a Reply