70 Responses

  1. Chip Bennett
    Chip Bennett Published |

    Great writeup, Justin!

    Out of curiosity: why no if ( ! function_exists( 'mytheme_setup_theme' ) ) {} wrapper around the Parent Theme’s mytheme_setup_theme() function, for direct override by Child Themes?

    Reply
  2. Mark McWilliams
    Mark McWilliams Published |

    Another GREAT quality article Justin! :) Got to say it was well worth the read, and thanks for that new little bit of knowledge. Maybe I’ll get round to creating a (Child) Theme or that in 2011, we’ll see what happens?

    Reply
  3. Chuck Reynolds
    Chuck Reynolds Published |

    Good write-up… I tend to just add stuff at the bottom quite often when I’m rebuilding something… Just cleaned up my personal site’s functions file and I like that setup… reads better and overall is much better.

    Reply
  4. lishijie
    lishijie Published |

    I really like your theme, my station is to use your theme, thank you very much to share

    Reply
  5. Brian Krogsgard
    Brian Krogsgard Published |

    Thank you. Thank you. Thank you. and Thank you. 1 for each of the 4 tips.

    I’ve been realizing some of my past errors recently, but the prefixes and priorities really make more sense now. Your tutorials are priceless… well, very valuable at least ; )

    Thanks as always.

    Reply
  6. rzepak
    rzepak Published |

    Show me your functions.php and I’ll tell you who you are :)

    Thanks Justin for great article! I’ll start to clean my themes today.

    Reply
  7. Adam W. Warner
    Adam W. Warner Published |

    Even though I’m not building themes anymore, I’m always mucking around in the functions.php files of various themes to do add custimizations or one sort or he other.

    Your write has just helped me understand where and how I should be adding my own functions:)

    Thanks Justin, glad to see your still writing!

    Reply
    1. Adam W. Warner
      Adam W. Warner Published |

      Geez, I guess I need more coffee…sorry for the spelling above :/

      Reply
  8. Jake
    Jake Published |

    Thanks Justin for this great tip. I’m always excited to read new post from your blog. I know something useful will come out from you! Thanks again!

    Reply
  9. Jamal Mohamed
    Jamal Mohamed Published |

    Great write-up, Justin.

    I’ve been recently looking into the code of a couple of the so-called premium themes. I felt shocked when I saw how many familiar snippets from the internet were pasted awkwardly into the functions.php file.

    Post thumbnails function was around for more than a year and many leading theme developers still use timthumb.php to resize images and, ironically, they still tell users to insert image URLs in the custom fields when WordPress has post feature image functionality. Maybe these theme developers are only interested to gain fame and fortune without giving much care the quality of their codes.

    …you created a [parent] theme setup function and hooked it to after_setup_theme with a priority of 10 in the previous section (10 is the default, so it’s actually shown in the code).

    Erm… do you mean “10 is the default, so it is NOT actually shown in the code”?

    Reply
  10. Manish Goyal
    Manish Goyal Published |

    Thanks Justin. I have never go through the function files in detailed. But through this post it’s really very easy to understand about them. Great piece of work here and hope you will recommend some more good stuff. Especially the code’s gives an idea how to handle such a situation.

    Reply
  11. Rami
    Rami Published |

    I think the problem is that we don’t have enough tutorial/guides like this one. When theme developers have more guides like this one, they will code their themes properly.

    Reply
    1. Zialingua
      Zialingua Published |

      Rami, i cannot agree more with you.
      Seriously WP has to give proper guidelines on how to use WP for theme and plug-in developers to produce correct code.
      At the moment it is a jungle where one has to spend hours or days to find things.
      Seriously, WP core developers cannot claim that people are able to read their minds to understand how to code themes and plug-ins without telling how to use this WP application.

      Reply
  12. dronix
    dronix Published |

    Would you recommend to “always” specify the priority number when using hooks? I’ve never run into a case where I’ve had to specify the priority and would like to know what would be the best practice.

    Reply
  13. Ralf Hortt
    Ralf Hortt Published |

    I prefer to write the hook below the function i.e:

    function myhook(){
    	// Does something
    }
    add_action( 'ahook','myhook' );

    Which way is better ? I do it because I fear that someday the add_action function will check if the callback is a function.

    Reply
  14. Kaiser
    Kaiser Published |

    Other things every theme/plugin author should know/do:
    http://wordpress.stackexchange.com/questions/715/

    And yes: (Imo) Everything should be hooked or filtered in wp.

    Reply
  15. Aws
    Aws Published |

    that is great thanks Justin :)

    Reply
  16. Nick Plekhanov
    Nick Plekhanov Published |

    Another GREAT quality article Justin!

    My BIG request: can you publish some tuts on:
    1. Creating very first real-life plugin from scratch.
    2. Creating a simple template from very scratch.

    ..both using high-top coding standards and practices with all theme /plugin files.

    HUGE THANKS!

    Reply
  17. Kishore Mylavarapu
    Kishore Mylavarapu Published |

    Great tutorial.Really simple to understand.

    Reply
  18. Robert Popovic
    Robert Popovic Published |

    When developing some more complex custom themes, I like to split out groups of custom functions and classes used in functions.php to several include files located in a theme subfolder. Just like the hybrid and your other themes do it.

    For example, I often have a bunch of utility functions, then the theme options and admin stuff, etc… splitting them out allows me to reuse them easily in the next project/theme.

    Reply
  19. WordPress theme function files
    WordPress theme function files at |
  20. Marlon
    Marlon Published |

    Awesome! Tanks a lot

    Reply
  21. How to Load WordPress Javascript and Stylesheets Using wp_enqeue_script and wp_enqueue_style | Mark Wilkinson on WordPress
  22. Hybrid theme version 0.9
    Hybrid theme version 0.9 at |
  23. Michael Hall
    Michael Hall Published |

    first time reader and i’ve already gobbled up about 4 articles, gonna grab a copy of your book in a few days too.

    thanks for the great info and clarity.

    i especially love reading about cleaning up code, and only very recently discovered wp hooks after reading sitepoints wordpress book

    Reply
  24. Sarah
    Sarah Published |

    Thank you for this. Exactly what I needed to sort my code :)

    Question: would adding

    load_theme_textdomain(‘my-theme’, TEMPLATEPATH . ‘/languages’);

    be appropriate in the same function?

    Reply
  25. Jason
    Jason Published |

    This post is seriously so valuable. I can’t thank you enough for taking the time to go into so munch detail about including functions in our themes.

    Reply
  26. 7up
    7up Published |

    Understanding how child themes are related to the parent themes is the most important info I got today.
    Thanks.

    Reply
  27. Janine
    Janine Published |

    Just to echo all the previous comments and thank you for taking the time to put articles like this together.
    This sort of info should really be in the official docs. I look up individual functions all the time in the codex and in the example code given, I don’t recall seeing the example wrapped in a function and wired up with a hook, or they don’t mention which functionality should be matched with which hook.
    This encourages bad development practices without developers realising that they are not doing things the right way.

    Reply
  28. Javier
    Javier Published |

    Hey! First of all, thanks for an awesome post. Truly moving.

    I’m trying to get my comments styling function inside an add action line. What hook do you think it would be appropriate to put it into? Should it be on the same ‘after_setup_theme’ or is there a better suit?

    Reply
  29. Denis
    Denis Published |

    Thank for a nice tutorial on functions.php. I am trying to clean my header via remove_action( ‘wp_head’, ‘parent_post_rel_link’, 10, 0 ); Should I use After_setup_theme hook for that? Thank you in advance.

    Reply
  30. Using remove_action in a Wordpress child theme | Marcus Nyberg
  31. Dear theme developers, this is how you add scripts in WordPress themes | WPCandy
  32. Create a child theme functions file | Twelve NMS509.com
  33. How to Add target=”blank” to all links
  34. Create a child theme functions file - Laura Collins
  35. Create a child theme functions file (+2) | TEN50nineTEN50nine
  36. Creating a child theme functions file (2 points) | NMS 509 Fall 2012NMS 509 Fall 2012
  37. Create a child theme functions file | Run 2012Run 2012
  38. Create a child theme functions file (2 points) | 13 Thirteen NMS509.com13 Thirteen NMS509.com
  39. Add a custom field and display it on single post pages (+5) and style (+2) | TEN50nineTEN50nine
  40. Create a child theme functions file (2 Points) | The "Oh So Customized" BlogThe "Oh So Customized" Blog
  41. WordPress: Necessary Reading | Tom J Nowell
  42. BDW @ Night: WordPress Intro - session 1 | hank is me
  43. Using Custom Fields and Custom Post Types in Wordpress
  44. My Journey into Web Design - aXtran
  45. VIDEO'EX » WebTekNeq's » WebTekNeq's
  46. Ted
    Ted Published |

    As a newbie to Wordpress, I learned a lot just from reading this post. I recently developed a small site that relies a lot on Gravity Forms and their user registration add on. Your members plugin is their go-to solution for solid content management. Gravity Forms is great and your knowledge of Wordpress is clear from your plugins and writing.

    I only wish that you had made the login widget that comes with “members” so that it would enable “register or login” for the unregistered or un-logged in user and change to “logout” when a registered user is logged in. In fact the whole user registration and login UX of wordpress could benefit from your attention. Thanks for your good work either way. It’s much appreciated.

    Reply
  47. 10 Tricks For Your WordPress Functions.php File | Pixelita Designs
  48. 10 Tricks For Your WordPress Functions.php File | Pixelita Designs (Sandbox)
  49. Proper wordpress coding | The Taylors
    Proper wordpress coding | The Taylors at |
  50. Desain Rumah
    Desain Rumah Published |

    In WordPress, themes can have a file called functions.php, which loads PHP functions for the theme. It behaves in much the same way as a plugin. Plus, WordPress automatically loads this file when a theme is active. Both the parent theme and child theme functions.php files are loaded when using a parent/child theme setup.

    Reply
  51. Integrate Google Analytics Dashboard into your WordPress Powered Website »
  52. Explode Your Aweber List with Aweber Comment Optin

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.

css.php