Podcast Episode

522 – Stop Using functions.php for Custom Code


New Version of Sensei
People of WordPress
Sandbox testing for WooCommerce Extensions

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 from the repository.

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

Sensei is a Learning Management System built by Automattic that integrates nicely with WooCommerce.

Stop Using functions.php for Custom Code

Sometimes it makes sense to use the functions.php file for theme related code snippets.

But if your theme depends on the functionality, I recommend one of three options:

  • [Code Snippets plugin](https://wordpress.org/plugins/code-snippets)
  • Custom functionality plugin
  • Custom plugin

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.

[00:00:00] On today's episode. We are going to talk about the problems with, in the reasons why not to use functions.php for custom code snippets on your WordPress website right here on your website, engineer.

Right here on Your Website Engineer podcast, episode number 520.

Hello, and welcome to another episode of your website, engineer podcast. My name is Dustin Hartzler, and I'm excited to be here with you today because of two reasons. One is today. I am traveling to WordCamp US my first trip since 2019 to see anyone in the WordPress space. And I'm. Extremely excited to be heading to San Diego.

And the second reason is we are talking about WordPress again, and I've learned a ton over the last couple weeks, and I'm not gonna dive into all of that, but there's been some really cool things that I have learned.

And before I go any further, I just wanna clear up, I've talked about Crowdsignal, the last couple podcasts, and I am still at Automattic, Crowdsignal is a part of [00:01:00] Automattic. It used to be called PollDaddy and we've rebranded to Crowdsignal. And so I'm still at Automattic. I'm still in the developer apprenticeship and I'm still learning a ton.

And so we'll talk about that as upcoming shows I come along. But this past week, I've learned how to create a block, basically from scratch and then get it working and then getting all of the data, just not on the front end, but then being able to submit the data to the database and be able to pull that information in.

It's been really cool. And I think I'm still wrapping my head around it. And I don't know if I could recopy, like if I could duplicate all of that again, but I'm learning a lot and I'm excited to share that in upcoming weeks.

I have three announcements that I wanna share with you today and a plugin. So the first announcement is all about Sensei.

There's a new version of Sensei. And so Sensei is a e-learning platform or an E learning management tool. I think they call them sometimes. And the cool part about Sensei is they are introducing interactive videos for WordPress. And this was something that my wife had been asking me for a long time.

Hey, can we stop a video like in the middle and [00:02:00] ask questions? She's in a continuing education area and they do a lot of, okay. Make sure they've understood the concept and make sure they're just not like watching the video. Like, it's gonna pause. It's gonna ask 'em a question. They gotta get the question right before they can move on and continue watching.

And that is now a feature inside of Sensei. So of course I wanna move everything from LearnDash where the currently the eLearning platform is over to Sensei. Another project is a for my books, but basically how this works is you have the ability to, it works with all different video providers.

So either YouTube, Vimeo, or VideoPress, and you paste the link to your video in the WordPress editor, and then you can create a break point and you can say at what specific time you want a break point to happen and a modal will open up and you can create any available block inside.

So whether it be a course block, or maybe you just want to put some text up on the screen or whatever, it's really, really cool. There is a blog post that I've shared here in the show notes here for episode number 522. So you can see a little bit more [00:03:00] about that. If you head on over there. The other thing that I wanna share with you today is there is a new section on the wordpress.org news page.

And so if you go to wordpress.org/news, there's actually a section that's called people of WordPress and you can go in and you can read about different people that are in the WordPress space.

On August 31st, there was an interview with Bud Kraus and it talks about how Bud has made his career helping other people learn software. And the interesting thing is if you go back into the show archives of Your Website Engineer, I had some conversations with Bud. I've done a webinar with him before, and it's just really interesting to see like how he has taken the free software of WordPress and created an entire living out of it and, and whatnot.

So there's all kinds of things that you can see there. You can search for community or can search for interviews. And all of those will pop up on the wordpress.org site. And then lastly, the, the last piece of news that I wanna share today is about WooCommerce using sandboxes now.

And this is a way to test [00:04:00] out a plugin to see if it will work before you actually make the purchase. When I was a WooCommerce Happiness Engineer, a lot of times we'd say, oh, well, we have a 30 day, no questions asked money back guarantee, like just purchase it, see if it works. And then get the refund. Well, it's kind of a hassle to do that.

So now WooCommerce is now bringing back this feature, it's called a sandbox feature and basically it will spin up a, a new WordPress site with that of plugin installed. It looks like it's only gonna do one plugin at a time. So it's gonna be very hard to compare if like, say bookings works with subscriptions and things like that.

And you also can't like import and build your entire site on this sandbox. And so it. For all extensions yet. It's not for all extensions yet. It looks like the ones that are working now are follow up emails, product add-ons, product CSV import suites, product vendors, table rate, shipping bookings, checkout, field editor points, and resort rewards.

And pre-orders. Oh, I take this back, actually. Any of the available extensions that I just mentioned can be simultaneously activated on an [00:05:00] existing test site.

This is made for testing these extensions only you cannot install any other plugins or switch themes or anything like that. And so that is the first rollout. I'm sure there'll be more features and advancements coming, but that's the news coming from a woocommerce.com.

And today in the plugin that I wanna share with you is just Sensei.

You can find a free version over at sensei, lms.com/download, and you can get started there. They do have different pricing options. You can get the pro version for $149 per year. For one site, you can start as. Easy with just getting Sensei blocks for $49 a year. And then if you wanna do up to 20 sites, it's $349.

They do have that free plan, like I said, and it Sensei is really cool. It has the ability to create paid courses. You can drip lessons, you can have people have access for certain amounts of time. You can do quizzes. You can do time quizzes. You can use video blocks to, to add video and whatnot.

The cool part is when I started testing this the other day is they have a whole section on like, basically you [00:06:00] would set it up with WooCommerce. People would purchase with WooCommerce. And then it will go into like this whole eCommerce learning area from the, my account page. And then you've got all the lessons down the left hand side, and then the content is on the right hand side.

And that's way cleaner than the way learn dash. Learn dash puts all the content on the, the initial page on your, my contact page. And then on the next page, once you click on one, then it shows you all the content.

But then to get to the next one, you have to either click the back button or know that there's navigation down at the bottom. And I just like the visual, like it basically takes over the entire webpage. There's not stuff on the sides you get into learning mode. And I think that's really cool. And like I said, this is gonna be a project of me switching from LearnDash to Sensei.

And I will explain all the details of that as the time comes.

All right. It is time to jump on the, the topic that I like to bring up every once in a while. And I don't know when the last time was probably a couple years ago, but the question is, um, where should you put custom code for your WordPress site?

A lot of times you will [00:07:00] Google like how to, how to add author, how to add a byline to my articles or how to. I don't know how to make all my titles uppercase or how to make all my titles lower case, or, you know, you're gonna Google something in WordPress, and then it's gonna come back with a snippet.

And a lot of times the snippet just says, use your functions.php file. Put that in your functions.php file and go on your merry way.

Now why this might be a good solution. Like it's gonna get it there. You know, it's gonna work and you can give it a test to see if it's working, but it's not the best solution in, from a long term point of view. And I'll talk about it in a minute.

Let's talk about WordPress in general. Just real briefly. There's I think three broad camps of people that are WordPress users. So we've got the plug and play users. These are ones that basically aren't web designers and they're just looking for a turnkey solution. They can install WordPress or maybe get WordPress set up on a managed host or on a BlueHost or something.

They can find a theme, they can install it, they can customize it and then they can start adding their content. There's another camp of people that are tweakers. These folks will take the theme [00:08:00] that can be probably used straight outta the box, but then they start making their own tweaks, maybe some custom CSS or maybe some custom functions.

And they're just basically taking the off the shelf version and they're gonna mess around with it to make it what they want. And then there's frameworkers. And these are usually people who will start off with a theme framework and then basically spend hundreds of hours just setting it up and get it crafted to exactly what they want.

And I would say a lot of people listening to this podcast are in the tweakers category. They're learning. They wanna know how to make things work the right way. They wanna make things work the way that they want it. Not the way that a theme is designed to work or not the way that a plugin's designed to work.

And let's face it. WordPress is a tweakers paradise. If you start off with just a Google search and some functions dot PHP file, you can do all kinds of things to your WordPress site. Now I wanna talk about the functions. Do PHP file.

The functions that PHP file is a file inside of your theme that has to control and add [00:09:00] functionality pieces to your theme themes that are specific for your themes. For example if you wanted to create the length of your excerpt, say you want to have 200 words that might look fantastic with one theme, but terrible with another.

Let's think about a reason why you shouldn't use or what shouldn't go into functions, do PHP file. Then I think it makes a little bit more sense.

You shouldn't use the functions dot PHP file. If it is a core piece of functionality for your WordPress. That you want to continue to work after the theme has been changed. Let's say that again. So if it is a part of your website that is very dependent on always working, no matter what theme that you're using, then it should not go in the functions.

PHP file. An example is maybe a testimonial custom post type. Like if you have it in your functions, PHP file to set up that custom post type, you can put all of your testimonials in there and then you switch to a different theme and then you can't access those testimonials anymore.

That would be a good example.

Another good example would be [00:10:00] to the script to put in the inject the Google analytics tracking code on every page on your website. If you put that into a theme functions, PHP file, and then someday you change to a different theme and you don't remember that it was in the functions that PHP file.

Then you will have a big break in your data and it won't track your website anymore. Have Facebook pixels or, you know, whatever you're using there, you don't want that to be in your functions dot PHP file.

Basically the question that you should ask when you are finding a code or your writing code, and you want to put it in your theme and you want that functionality say, is this dependent on the theme?

If your theme changes and you can lose that code, you can put it in your functions. That PHP file. If by the chance that you want this code to continue with a different theme, then we're gonna have to put it somewhere else.

And where is that somewhere else that you may ask? Well, there's a couple different places. You can put it in your custom functions plugin. And I'll talk about that in a second, or you can create your own plugin or you can use the, [00:11:00] probably the simplest matter is a plugin called Code Snippets.

Code snippets is probably my favorite way to use and to test things out because I know that it is very easy to paste code in there. I know that I can turn it on. I can turn it off. I can turn the whole plugin on and off, and it I'll put a link in the show notes. It's called code snippets.

You can find out on the WordPress repository. You can actually use this for PHP and CSS and even JavaScript. I believe you can put all of that in there. And this is a self-contained area. It's a plugin. So it's a standalone thing. You can change themes, things will work, but it gives you the ability to putting code and you can turn it on.

You can turn it off. And this is really helpful if you're trying to figure out if there's a conflict with your code, with something else on your site, so you can basically say, oh, this code snippet that I have to make sure that all of my titles are all capitalized. Then I can turn that off and then I can check my site and I can turn it on.

I can check my site. And so you can toggle those on and off, which is really, really nice. The other thing that you can do is you can create your own plugin. So I have a couple of these that are across some of my sites. You know, one of 'em is for custom post types. I [00:12:00] basically just call to plug in, I call it my custom post types.

And then I put in all of the code that you need to define and set up a custom post type. And then I know that that's in a plugin and I can turn that off. If I turn it off, then I lose those custom post types. I can't see that information anymore. If I turn 'em back on, then they are exactly there and where I expect them to be.

So I do that, that would be considered a creating your own plugin. So that is pretty easy. If you have custom post types for say, uh, testimonials, that's a good one. I'll just create a testimonial plugin, and then I can move that from site to site. I could just duplicate it across sites and then every code or every theme.

And then every site would have that exact same layout and then every site would have that exact same code and they have that exact same testimonials area on the site. The other thing that you can do is you can just do this custom functionalities plugin. I, I use this a lot for a lot of small snippets, maybe it's something that's very simple.

Like on one of my sites for my wife, I have her have a sign off that just says Dr. Hartzler and it's in a [00:13:00] custom font and it's basically a short code. Like I have that and a handful of other things all in one custom functionality plugin, mainly because it doesn't need its own plugin. It needs a place to live and I don't need to put them 10 different plugins.

I can just put them all in one spot and that would be what's called a custom functionalities plugin. And so those are some different ways that you can handle these things.

The thing that I like to do in the way that I build sites is I use the Code Snippets plugin, and I will use that to put in the code snippets that I need the down and dirty way to get things into the site that I need. And then later. Which later never happens. Let's be honest.

But later the intention is to go back through and to take those blocks and put them in a custom functionality plugin, or put them in their own plugin and then make sure that they are in a safe, secure area and that they are working well and as expected. And I kind of do the same thing with the custom CSS area in the WordPress site.

So if you go into appearance and then go to custom CSS, like you can make tweaks and changes and modifications to a [00:14:00] theme right there, and it's instant. And then I don't have to go and, you know, try to get the CSS file and then compile it and do all that kind of stuff. So then it's a, okay. Every once in a while, I take all the stuff from the custom CSS area and the WordPress dashboard or the Customizer, and then add that into my real CSS.

So it will ultimately work in, you can keep it in the, the code snippets plugins forever. It's just for me, I like to clean things up a little bit and put them in a quote unquote, proper place. And so this is just kind of the discussion that I like to have every once in a while, just to make sure that you're understand in your familiarized with the functions, that PHP file in what it's used for what it should be used for those, those theme related things and where we should be putting the things that are not theme dependent, the things that we always wanna have on our WordPress site.

That's what we wanna do. We'll put it in our own plugin. We'll put it in Code Snippets, or we will do something with it so that it cannot be affected when we change our theme. So that's what I wanna share with you this week. The next week, I'll give you a recap of what happened at WordCamp US, and be back with you with more WordPress information [00:15:00] as the month of September rolls on.

That's what I wanted to share with you today. Take care and we'll talk again soon. Bye bye. For more great WordPress information, head on over to your website, engineer.com.