• Save
Don't "Just paste this code in your functions.php"
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Don't "Just paste this code in your functions.php"

on

  • 4,400 views

Presentation slides from WordCamp Toronto 2011 talk. ...

Presentation slides from WordCamp Toronto 2011 talk.

Many snippets of code are available on the web to add functionality to your WordPress site. “Just paste this code in your functions.php”, used to be the refrain for adding those functions to your site. Now many themes are using the update system, which will overwrite the functions.php when updating and you will lose your changes!

A lot of functions that are key to your site operation, but not related to the theme, are also now being added to the functions.php. These functions should really be saved somewhere else, so your site won’t break when you change or update themes.

This talk will show anyone who uses WordPress how to easily create a very simple child theme, and a very simple personal plugin for saving theme changes and code snippets. It’s really very easy!

Statistics

Views

Total Views
4,400
Views on SlideShare
4,400
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Don't "Just paste this code in your functions.php" Presentation Transcript

  • 1. “Just paste this code in your functions.php”Rick Radko WordCamp Torontor3df.com November 5th, 2011
  • 2. SlidesSlides will be posted:  http://www.slideshare.net/r3df© 2011 Rick Radko, r3df.com 1
  • 3. Who am I?I’m  Rick Radko  Developer, coder and web designer  WordPress enthusiast© 2011 Rick Radko, r3df.com 2
  • 4. Who are you?© 2011 Rick Radko, r3df.com 3
  • 5. “Just paste this code in your functions.php”© 2011 Rick Radko, r3df.com 4
  • 6. Automatic theme updates© 2011 Rick Radko, r3df.com 5
  • 7. Oh-oh!© 2011 Rick Radko, r3df.com 6
  • 8. Does it change your theme?Non-Theme related changes  Like buttons, thickbox effects, fancy jQuery effects and addons  Custom taxonomies, custom post types, at least partially.  These changes should be theme independent.  Changes get lost in functions.php theme items.© 2011 Rick Radko, r3df.com 7
  • 9. Don’t do it! “Just paste this code in your functions.php”© 2011 Rick Radko, r3df.com 8
  • 10. Then where?Child theme  All theme related changes.  Changes that should go away if theme changes.Personal “Site” plugin  All other additions/changes.  Changes that should stay even if theme changes.*“Site” from Otto: http://ottopress.com/2011/creating-a-site-specific-snippets-plugin© 2011 Rick Radko, r3df.com 9
  • 11. Child themesHow do they work?  Child themes inherit all the features of their parent theme.  Child themes override their parent theme by providing alternate code/files.  If it’s not present in child, WordPress looks in the parent theme.© 2011 Rick Radko, r3df.com 10
  • 12. Minimal child themeWhat do you need for a minimal child theme?  A theme folder.  A file called style.css, with a specific header, and usually one line of code.  Not required for a child theme, but a functions.php will be needed to add php snippets.© 2011 Rick Radko, r3df.com 11
  • 13. Creating a child themeLots of ways to create the theme content, here area few:  Text editor and ftp the files into place  Local development environment and ftp  Tomorrow’s workshops  Site hosting’s filemanager and editor  Common and simple© 2011 Rick Radko, r3df.com 12
  • 14. cPanel© 2011 Rick Radko, r3df.com 13
  • 15. File manager© 2011 Rick Radko, r3df.com 14
  • 16. Create the child theme folderCreate a new folder in /wp-content/themes.© 2011 Rick Radko, r3df.com 15
  • 17. New child-theme folder© 2011 Rick Radko, r3df.com 16
  • 18. Creating a style.cssCreate style.css in the theme folder.© 2011 Rick Radko, r3df.com 17
  • 19. The style.css file© 2011 Rick Radko, r3df.com 18
  • 20. Absolute minimum style.css© 2011 Rick Radko, r3df.com 19
  • 21. The new child themeNow you have a new theme!© 2011 Rick Radko, r3df.com 20
  • 22. The new theme, not quite rightChild Theme Twenty Eleven© 2011 Rick Radko, r3df.com 21
  • 23. Practical style.cssA style.css that will work a little better:© 2011 Rick Radko, r3df.com 22
  • 24. Child theme activated© 2011 Rick Radko, r3df.com 23
  • 25. Now, it’s the same as the parent© 2011 Rick Radko, r3df.com 24
  • 26. Creating a functions.phpCreate functions.php in the theme folder.© 2011 Rick Radko, r3df.com 25
  • 27. Edit the functions.php© 2011 Rick Radko, r3df.com 26
  • 28. Minimum functions.php© 2011 Rick Radko, r3df.com 27
  • 29. It’s done!We now have a child theme that:  Looks exactly the same as it’s parent.  Will maintain updates/changes to it’s:  style.css  functions.php  any other theme file that you want to add without fear of having them overwritten!© 2011 Rick Radko, r3df.com 28
  • 30. “Grandchild” issuesYou cannot make a child theme of a child theme!  Use a site plugin.© 2011 Rick Radko, r3df.com 29
  • 31. Site pluginsWhat do you need for a minimal plugin?  Only a single php file, of any name.  Plugin defining header in the file.  Not required for a plugin, but a folder keeps things neater, especially if you want to include css and js files.© 2011 Rick Radko, r3df.com 30
  • 32. Creating a site pluginGo to file manager again© 2011 Rick Radko, r3df.com 31
  • 33. Creating the plugin folder© 2011 Rick Radko, r3df.com 32
  • 34. The new site-plugin folder© 2011 Rick Radko, r3df.com 33
  • 35. Create a site-plugin.php file© 2011 Rick Radko, r3df.com 34
  • 36. The site-plugin.php file© 2011 Rick Radko, r3df.com 35
  • 37. Minimum plugin file© 2011 Rick Radko, r3df.com 36
  • 38. The new plugin© 2011 Rick Radko, r3df.com 37
  • 39. Practical plugin file© 2011 Rick Radko, r3df.com 38
  • 40. Activated final pluginactivate© 2011 Rick Radko, r3df.com 39
  • 41. It’s done!We now have a complete plugin that:  Stores code without direct impact from updates.  Can be extended  Inject css and javascript files  Add options pages in admin  Is portable  Easily move from one site to another carrying all your favorite changes.© 2011 Rick Radko, r3df.com 40
  • 42. Timing issues  Plugins load before themes.  For most snippets that are hooked to an action this is not an issue, as the action dictates when the code runs.  Occasionally there are issues with code that is not hooked to an action, so the code snippet in the plugin may run too early and not work.  Hook the code to an appropriate action with add_action(). codex.wordpress.org/Plugin_API/Action_Reference© 2011 Rick Radko, r3df.com 41
  • 43. Extras – Theme thumbnailImage for theme thumbnail:  Create an image in png format.  About 300x225px.  Save as “screenshot.png” in the theme folder.  Will be loaded automatically.© 2011 Rick Radko, r3df.com 42
  • 44. Extras – Split loadingSplit loading  admin/front/both  is_admin() /* Code needed by both admin and front here*/ if ( is_admin() ) { /* code for admin only here*/ } else { /* code for front only here*/ }© 2011 Rick Radko, r3df.com 43
  • 45. Extras – Loading css and javascript Loading css and scripts Use actions  wp_print_styles for css  wp_enqueue_scripts for scripts See the codex for more detail© 2011 Rick Radko, r3df.com 44
  • 46. Key PointsMain theme functions.php is:  Not the best place to put code snippets.  Overwritten by automatic updates.  Not used if theme changes.Some snippets are not theme related.  Should have a place that is theme independent to store them.© 2011 Rick Radko, r3df.com 45
  • 47. Key Points: Child ThemesChild themes for theme related changes  They are easy to create:  Can be really simple, only what you need.  Minimum is a folder and style.css.  Probably will need a functions.php.  Create once per theme.  Once it’s set-up, just paste in your snippets.© 2011 Rick Radko, r3df.com 46
  • 48. Key Points: Personal PluginsPersonal plugin for all other additions/changes:  Also easy to create:  Can be really simple or quite complex  Minimum is a plugin php file  Folder helps keep things organized  Set-up once per site  copy across sites!  Once it’s set-up, just paste in your snippets.© 2011 Rick Radko, r3df.com 47
  • 49. ContactRick Radko  email: wpinfo@r3df.com  twitter: @r3designforgeSlides at:  www.slideshare.net/r3dfCode at:  r3df.com/downloads© 2011 Rick Radko, r3df.com 48