108 – The Dangers of the functions.php File
Announcements
- WordPress Webinar on Custom Post Types on January 4th at 10am EST (http://yourwebsiteengineer.com/webinar)
Advanced WordPress Developers Course
– Last week in January
– Take your developer skills to a whole new level
Tools / Plugins
oEmbeds http://codex.wordpress.org/Embeds
One of the great features in WordPress that is rarely mentioned is oEmbeds. This give the ability for WordPress users to simply paste a link (the main URL) to their YouTube video (and other services) directly into their visual editor. This saves a blog / website owner from learning how to find the embed code, format the application to be the correct size, then switch to the HTML editor to paste in the code.
Main Topic
Dave Clements (http://doitwithwp.com/)
Some of the questions that were covered on today’s episode:
– Tell me a little about yourself. Who is Dave Clements?
– How long have you been using WordPress?
– Why do you cringe when I mention adding code to your functions.php file?
– Where should I be putting this code?
– Is it hard to create my own plugins?
– Can I put several snippets of code in one plugin?
– How can I make sure that some the website owner doesn’t accidentally turn off my plugin or remove it
Call To Action
- Sign up for webinar at http://yourwebsiteengineer.com/webinar
Interesting conversation; so interesting I listened to it twice back-to-back! I was wondering though, this concept of creating custom plugins instead of adding to functions.php only applies if youre modifying someone elses theme though; is that right? If youre making a custom theme from scratch or making a child theme, then I wouldnt think this applies because youre never updating the theme using the WP update mechanism.
Dec 28, 2012@forresttanaka Not really: when it comes time to change your theme one day, you’ll want all that custom functionality in one place, that isn’t going to die with your theme. It’s also good practice to do it “the correct way” than to perpetuate bad habits. It’s no more difficult doing it the right way, so that should be your default 🙂
Dec 28, 2012@daclements Thanks for the replies, guys! I see what you mean, and probably should have seen that myself since Im in the process of making a new theme for my own site right now, and will toss my old one when its done. Cool stuff!
Dec 28, 2012@forresttanaka What Dave said 🙂 Basically, if you put your custom code in the functions.php file and you changed your theme, then you would have to copy all the custom code to your next theme’s functions.php file. By creating your own plugin, then you can change themes with less hassle.
Dec 28, 2012Not really: when it comes time to change your theme one day, you’ll want all that custom functionality in one place, that isn’t going to die with your theme. It’s also good practice to do it “the correct way” than to perpetuate bad habits. It’s no more difficult doing it the right way, so that should be your default 🙂
Dec 28, 2012Would you recommend this method over a Child’s Theme?
Jan 1, 2013I have part of a Theme function replaced by some custom code, but included this as part of a Child’s Theme, which also contains some further changes to other .php files.
Would adding all of these changes into 1 new plugin give the same result? (i.e. putting all the changes from the various .php files into 1 plugin file)
Also, will code in a plugin always have precedence (higher priority / loaded last) over code in the original Theme files?
@Geocaching Studio It’s a step better to put the code in a child theme’s functions.php file, because you won’t have to worry about losing your changes if the main theme is updated.
Jan 2, 2013But, if you ever change main themes or create a new child theme, then you would have to duplicate your code to the new theme.
And you can bundle all of the extra code into one custom plugin and get the same result as many small different plugins. Official plugins have higher precedence over theme files.
One thing to note about the MU-Plugins folder is you can not have your plugin inside a sub folder say mu-plugins/myplugin/plugin.php like you can with regular plugins. and there are some things that a MU plugin can not do. i would check out the codex when creating a MU-Plugin
Jan 8, 2013@LehmanCreations great point Andy! Your plugin must only be one file. @daclements right?
Jan 10, 2013@dhartzler10 @LehmanCreations @daclements Yes that’s right. There’s some good information about the differences between mu-plugins and regular plugins in the Codex: http://codex.wordpress.org/Must_Use_Plugins
Jan 11, 2013