I have read many "tutorials" which told users to modify WordPress core files. This is a really, really, bad idea. Why? Simply because you'll have to modify that file again when you'll upgrade your blog. Here's how to overwrite WordPress core function without modifying any core file.

Why overwriting instead of replacing?

At first, modifying a core file seems a lot easier than overwriting it. But what will you do when you’ll have to upgrade your blog? New WordPress versions are released something like every two months, so it might be a long and boring work to reimplement each core file you previously modified.

Happilly, there’s a solution to that problem. It is WordPress specific functions calls filters. For exemple, when you install a WordPress plugin which modify some WordPress core functionnality, you can be sure that the plugin uses a filter.

Using filters

As I said, a filter can be used within a plugin. But that’s not necessary: You can also use it on the functions.php file from your theme. If your theme doesn’t have a functions.php file, you should create it.

The most common WordPress filter function is called add_filter():

add_filter('hook_name', 'your_filter', [priority], [accepted_args]);

Let’s study the required and optionnal parameters in detail:

  • hook_name: (required) the name of the hook provided by WordPress. It defines when your filter will apply.
  • your_filter: (required) The custom function to call instead of hook_name.
  • priority: (optionnal) used to specify the order in which the functions associated with a particular filter are executed.
  • accepted_args: (optionnal) The number of accepted arguments.

Exemple use of the add_filter() function

Let’s see how to use the add_filter() function with a practical exemple. The following code overwrites the bloginfo() function.

add_filter('bloginfo', 'mybloginfo', 1, 2);
add_filter('bloginfo_url', 'mybloginfo', 1, 2);

function mybloginfo($result='', $show='') {
        switch ($show) {
        case 'wpurl':
                $result = SITE_URL;
                break;
        case 'template_directory':
                $result = TEMPL_DIR;
                break;
        default:
        }
        return $result;
}

Removing filters

While it is possible to add new filters to overwrite WordPress core functions, it is also possible to remove existing filters. For exemple, many people who publish code on their blog are tired about the curly code, because it breaks the code. A WordPress function called wptexturize() replaces normal quotes with curly quotes. As this is a filter, it is possible to simply remove it and get rid of theses curly quotes:

<?php remove_filter('the_content', 'wptexturize'); ?>

Nothing hard, isn’t it? You now know how to overwrite WordPress core functionnality without editing core files, as well as removing unwanted filters.

Related Posts

No related posts.
 

36 Comments

  1. Posted November 11, 2008 at 12:38 pm | Permalink

    Well, it is not hard when you, master of Wp, are explaining it :) . Thanks for sharing. Indeed very useful as i didn’t know it was possible to overwrite core wp functions.

  2. Posted November 11, 2008 at 12:46 pm | Permalink

    Thanks Catrin, I appreciate your kind words :)

  3. Posted November 11, 2008 at 5:12 pm | Permalink

    Excellent post. I’m surprised not to see more info like this – everyone seems to focus on the theming and theme related functions rather than the back-end/core issues. Thank you!

  4. Posted November 12, 2008 at 3:03 am | Permalink

    Thanks for sharing the information

  5. Posted November 12, 2008 at 3:04 am | Permalink

    Nice article :)

  6. Posted November 12, 2008 at 3:28 pm | Permalink

    Well, I generally tend to get into the code details,but Wordpress is new to me. These little tutorials help immensely. I am currently redesigning and rebuilding my entire website in Wordpress so these posts are extremely useful to me.

    I use your website as my browser home page so I don’t miss anything.

  7. Posted November 13, 2008 at 6:34 pm | Permalink

    I´ve actually just started blogging and I need to read up on word press and templates etc. I´ve come across a few blogs which give a lot of information, which is always good for beginners like me. I´d ike to think I´ll get the hang of it all soon – fingers crossed!

    Frank

  8. Posted November 13, 2008 at 8:04 pm | Permalink

    Hi, this helpd me out alot, but i wanted to ask if you had a list of all the hook name’s, since theres alot of them.

  9. Posted November 13, 2008 at 8:10 pm | Permalink

    @Omar: You can find some hooks here.

  10. Posted November 13, 2008 at 8:48 pm | Permalink

    Well, that’s a nice article if i understood it! LOL!! I am more than sure that the piece of code you provided is going to be quite useful to…people who know their way through Wordpress. As far as I am concerned i had to read your article twice just to find out that i didnt know what it was talking about. I just know how to upgrade my blog and my plugins plus some other nice tricks to protect it and how to work with http://ftp…and that’s it! ta-da! When something goes wrong i have, as a last resource, pay to get the job done..sad but true!

  11. Posted November 15, 2008 at 5:56 am | Permalink

    The term overwrite in this context is not strictly correct since using hooks only “hooks” your code into the wordpress function that is already being executed. It allows you to additionally manipulate the data after wordpress is done with it. If you really want to overwrite a wordpress function, then I’d suggest copying the code for the desired function into your functions.php file, giving it a different name, changing the code to suit and then calling that function everywhere in your template.

  12. Posted November 15, 2008 at 9:04 am | Permalink

    @Robert: Thanks for the correction!

  13. Posted November 16, 2008 at 1:53 am | Permalink

    very well said post

  14. Posted November 16, 2008 at 8:46 pm | Permalink

    I still overwrite core files because adding filters can be more difficult depending on the scenario ;) Well, right now I am trying to write a couple of plusings to learn how to use filters properly. May be after that…

  15. Charly
    Posted November 22, 2008 at 3:12 am | Permalink

    Hello jrj..
    How about function / filter to hide or replace some word at admin area. For sample, at header i want to replace “Howdy” with “Logged User :” or at footer, I want to replace “Thank you for creating with Wordpress” with “Powered by Wordpress”
    Thx before and after..

  16. Posted November 24, 2008 at 7:21 pm | Permalink

    Thank you, this is a big help to a problem I’ve been having.

  17. Posted November 25, 2008 at 12:40 am | Permalink

    I am currently redesigning and rebuilding my entire website in Wordpress so these posts are extremely useful to me.

  18. Posted December 3, 2008 at 3:50 am | Permalink

    I learn new lesson here, some themes have function php file, some not … thank you

  19. Posted February 10, 2009 at 11:16 am | Permalink

    NIce article, came in really handy for me. Thank you. I used to modify my core files. Reading this made things really easy for me.

  20. Posted February 11, 2009 at 6:36 pm | Permalink

    Really neat article… generally you wouldnt want to overwrite core functions but sometimes you need to to make your site even slicker.

  21. Posted February 14, 2009 at 12:00 pm | Permalink

    Oh nice idea, I need to check on this one because I have replaced many of the wordpress functions in the core files. I’ll try this now because the new wordpress upgrade it out!

  22. Posted February 26, 2009 at 8:32 pm | Permalink

    That’s a really cool idea. I’ve always wanted to mes around and check out some core function changes. But didn’t want to mess things up at the same time. This will help me just to twist things around and see. Thanks a bunch

  23. Posted March 4, 2009 at 12:57 pm | Permalink

    Why I can’t overwrite it, when I called bloginfo(’siteurl’) it not point to mybloginfo() function or what, so I still get old site url value.

    Help me! Thanks!

  24. Posted March 5, 2009 at 4:00 pm | Permalink

    Thanks for the info! Very helpful!

  25. Posted March 13, 2009 at 6:39 pm | Permalink

    Thanks Jung, it helps my blog a lot.

  26. Posted March 17, 2009 at 10:30 am | Permalink

    Owh, good. my friends still in re-coding the site with WP engine, maybe this articles will help him to know deeply about Wordpress. Thanks for sharing with us

    Regard’s from Bali Island

  27. Posted April 9, 2009 at 5:24 pm | Permalink

    Superb. Another awesome article on your blog. You simply rock. This would really help me. I’m still new to wordpress.Thanks a lot.

  28. Posted April 11, 2009 at 4:28 am | Permalink

    Awesome. just the solution I was looking for. I’ve been having this problem for few days now. Replacing the functions certainly did not work. I’ll try overwriting them. Cheers

  29. Posted May 1, 2009 at 6:00 am | Permalink

    Really cool article. I’m kind of new to blogging and this certainly is a treat. would love to learn more and more. Appreciate it. Cheers

  30. Posted May 24, 2009 at 2:17 pm | Permalink

    Hey thanks for this article!

    I am trying to edit “the_editor” function – but it is not fully replacing the function when I use:

    add_filter(’the_editor’, ‘custom_the_editor’);

    It replaces SOME of the code – but other parts are not removed. OR say if I make the function completely empty, the original function continues working fully. Am I missing something?

    I have a feeling it has something to do with Roberts comment (#11) – so is there a way to make certain parts of a function NOT work? Or can you only add to functions that already exist.

  31. Posted June 14, 2009 at 9:59 am | Permalink

    did anyone find out if it is possible to hack the core and get rid of “thank you for creating with wordpress.”…it just takes up screen real estate….everyone who needs it will just do a direct search for wordpress….makes no sense really to me….anyway, if anyone found out how to do it…please let me know. I looked in all the files and can’t find it.

  32. Posted October 26, 2009 at 10:05 pm | Permalink

    Great post! Not so easy to find this info.

    And this is dumb… but… you spelled “Example” wrong “Exemple” (if ya cared to know)

    Thanks again!

  33. Posted October 26, 2009 at 10:20 pm | Permalink

    Hmmm… couldn’t seem to get this to work for wp_generate_password. Perhaps this is because it’s not a filter?

  34. Posted October 26, 2009 at 10:28 pm | Permalink

    Sorry to spam up your comments…. but I answered my own question.

    Interesting to note that the functions in pluggable.php look to see if the function already exists. With that little tidbit, I just created a plugin with the same name as the function I wanted to replace.

    Poof! It’s magic! :-)

  35. Posted November 24, 2009 at 6:27 pm | Permalink

    Very nice to find such a succinct post!

    Just what I was looking for too…wooohoo!

    thanks for posting.

  36. Posted December 27, 2009 at 3:09 pm | Permalink

    Hi, that’s a very helpful article, but i have one serious question. What about functions that are inside a specific class ? How can i reference them by string ?

    For example: I want to overwrite the output of the “Recent posts” Widget.
    The function is named :

    function widget($args, $instance) { …. }

    Ok.. but, this function is inside of this class:

    class WP_Widget_Recent_Posts extends WP_Widget { … }

    There are also other classes with this function name, so the function name itself won’t help me. I need something to tell the add_filter – Function that he has to look in a specific class. I did not found something in the api or google, thats why my last hope is this article :)

    Thx for any help,
    Aeox

3 Trackbacks

  1. [...] How to: Overwrite WordPress core functions [...]

  2. By links for 2008-12-14 | girlie geek on December 15, 2008 at 9:01 am

    [...] How to: Overwrite WordPress core functions (tags: wordpress functions hacks core) [...]

  3. By WordPress Functions.php Magic on January 5, 2009 at 7:08 pm

    [...] and kinda breaks your code. This little code in your functions.php takes care of that. Hattip to CatsWhoCode. They also have another example of using functions.php [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Smashing Network
WordPress Appliance - Powered by TurnKey Linux