Contact: Web / Voice / Email / Tips
Simple Thoughts Blog - Java and Web Technologies

Simple solutions for complex problems.

 

How to add custom functions to a WordPress 1.5 theme?

April 19th, 2005 by Angsuman Chakraborty

Recently the question (How to add add custom functions to a theme) was posed in WordPress Support Forum. Personally I faced the same question when re-designing this site. The following is a discussion of the option with the pros and cons.

There are two solutions.
The first is to create a plugin which goes with the theme. However the theme, when invoking functions defined in the plugin should check first for the availability of the function. It should also provide a fallback option if the plugin is not available or has not been activated.

if(function_exists('your_function_name')) {
    // Invoke your function: your_function_name()...
else {
    // Execute fallback option
}

The advantages are:

  • The plugin can be independently managed.
  • The plugin can be reused for other purposes.

The disadvantages are:

  • It requires another additional step for the theme user to remember.
  • It slightly complicates development and testing.

The second solution to this problem would be to incorporate the functionaility in a php file (as usual) which resides in the theme directory. This file is included in header.php like:
include (’your_php_file.php’);

Yes you may also require it for simplicity like:
require (’your_php_file.php’);

The advantage to this approach is simplicity of usage and deployment by end-users. It also simplifies development.

If the user later decides to switch to a different theme and yet wants to retain the functionality, he would have to re-purpose the custom code into a plugin.

In essence the reusability of the custom functionality determines the ideal location of the custom code.


Tags: CMS Software, Headline News, How To, Pro Blogging, Technology, Web, WordPress | | RSS 2.0 | Email this Article

You may also like to read

»Files Changed from WordPress 1.5.1.3 to WordPress 1.5.2
»Anaconda: 3-Column WordPress Theme Sneak Preview
»New Forum for 3 Column widgetized Anaconda Theme ( WordPress )
»New Site Layout & Design: Feedback Please
»Neat Three Column WordPress Theme - Alexified
»List of Files Changed in WordPress 2.03 wrt. WordPress 2.02
»Anaconda 3 Column Theme Release Schedule
»Next Generation WordPress Theme Design - Giraffe
»Impact2 WordPress Theme - Review
»WordPress 3 Column Anaconda Theme Tested on 9 Display Settings
»Next Generation CSS Based 3 Column Anaconda Theme for WordPress Released
»One Reason Why WordPress Plugin Activation Causes Fatal Error
»How To Display Sidebar in Single Post in WordPress 1.5 / 2.0 Default Kubrick Theme
»Added Forum for Anaconda Theme
»Introducing Cool WordPress Theme - Frost

3 Responses to “How to add custom functions to a WordPress 1.5 theme?”

  1. Andy Staines Says:

    Thanks for this. ~I am new to both WordPress and PHP but also wanted a few home cooked functions. In my case I wanted to change a couple of WP functions to return differently so I copied these, renamed them and made my changes so as not to disturb distributable files. Not knowing where to put them, I just created a my-hacks.php file and turned that one. All works fine. But is this NOT a good place to put my code?
    Appreciate any advice. Thanks

  2. Angsuman Says:

    As I mentioned above, I think you should just create a plugin and put them there.

    Ideally in your template you can check if the function is available (or whether the plugin is active) and then use it, if not use the default WordPress function.

    I too use several such customized functions on my site all packed away in a generic plugin named Impact (named after my custom theme).

  3. Andy Staines Says:

    Thanks for the advice. I’ll collect them all together if a my-hacks file until I;m complete then set about converting to a custom plugin as you suggest,Thanks again

Looking forward to hear your thoughts.



Please enter the code shown below ( to verify that you are human ) before you click Submit Comment.

No. 1 method to ethically increase your blog traffic and reach.

Translate

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanPřeložit do Čech/CzechOversætte hen til Dansk/DanishKääntää jotta Finnish/FinnishLefordít -hoz Magyar/HungarianÞýða til Íslenska/IcelandicTraducir a Latinoamericano Español/Latin American Spanishtagapagsalin sa Filipino/FilipinoTłumaczyć wobec Polski/PolishA traduce la spre Român/RomanianPrevesti za Srpski/Serbiantolmačiti v slovenski/SlovenianÖversätta till Svensk/SwedishChyfieitha at Cymraeg/Welshtercüme etmek -e doğru Türk/TurkishPrevesti to Hrvatski/CroatianПревеждам към Българин/BulgarianTraduzca al Español/SpanishTraduisez au Français/FrenchTraduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplifiedترجمة الى العربية/ArabicVertaal aan het Nederlands/DutchΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/Norwegian中文翻译/Chinese TraditionalTraduzir a Língua portuguesa brasileira/Brazilian PortugueseReddo ut Latin/Latin

Taragana Network

»Ctrl-S
»Enterprise Blog
»Free Book on Eye Care by Natural Therapy
»Health Care Blog
»Hot Computer Jobs Blog
»Pet Care & Grooming News and Tips
»Phil Law Blog
»Taragana - Software Outsourcing
»The Angsuman Chakraborty Blog
»The Diabetes Cure Blog
»The Eye Treatment Blog
»The Stem Cell Blog
»Weblog Hosting Blog
"I am convinced that He (God) does not play dice." - Albert Einstein