PHP - override existing function

29,699

Solution 1

Only if you use something like APD that extends the zend engine to allow for that:

Intro

Override Method Docs

Note: Runkit seems like a better option than APD since it's more specific to this purpose and would allow you to keep the original method intact at a different address.

Solution 2

You could use the WordPress hooks (called filters and actions) and then use the add_filter() function to override the function you are using.

i.e.

function function_name() {
 //code goes here
}

$hook = 'get_options'; // the function name you're filtering
add_filter( $hook, 'function_name' );

The Codex will help a lot with this.

http://codex.wordpress.org/Plugin_API/Filter_Reference

Solution 3

Just comment the old function out and write your new one ;)

In PHP it is not possible to redefine or overload (i.e. define a function with the same name but different parameters) a function natively. There though are extensions like runkit which allow to redefine functions (runkit_function_redefine), but you probably don't want to use these (such extensions are rarely installed and mostly unreliable.)

Solution 4

You can wrap the block defining the original function in a conditional checking if another of the same name is not already defined (I'm assuming you mean Wordpress functions and not core PHP ones)

    <?php 
if(!function_exists('function_name')){ 
    //old definition here
    } ?>

You could then redefine it above while still preserving the original should you need to roll back to it.

Depending on how complex the changes are and how many times you may do this, you may also want to look into Namespaces if you are on PHP 5.

Solution 5

My attempted solution was to do this:

function suffusion_get_image($options = array()) {
    include_once ABSPATH.'wp-content/themes/suffusion-child/functions/media.php';
    return childtheme_overide_suffusion_get_image($options = array());
    ....
}

Obviously there is an overhead at upgrade as you would need to add lines back into the scripts again and I have used this method successfully to date but now trying to do it with get_terms in the wp-includes and hitting a redeclaration issue which I am trying to resolve or workaround at the moment.

My reason to edit core is that the existing core does not provide in a convenient way for a multisite requirement.

Someone has just suggested on another forum however using override_function but the manual is worded such that it appears to be of use only to built-in functions - I took it that means PHP built in functions

Share:
29,699

Related videos on Youtube

Alex
Author by

Alex

I'm still learning so I'm only here to ask questions :P

Updated on July 09, 2022

Comments

  • Alex
    Alex almost 2 years

    Can I redeclare a existing function, with the same name, but different code? Or somehow "disable" the old function?

    I want to redefince a core WordPress function, but since plugins and theme call this function a lot, I need to keep the same function name.

    • Tobias
      Tobias over 13 years
      Keep in mind that this could make trouble with further wordpress updates...
    • Phill Pafford
      Phill Pafford over 13 years
      if you're just writing a plugin or theme you shouldn't be touching the core IMHO. Maybe you could tell us what you're trying to accomplish so we could help you out with that?
    • Gordon
      Gordon over 13 years
      possible duplicate of Redefine Built in PHP Functions and Redefining PHP Function. It took less than a minute to find these, so I am sure you used the search function and found these too. Please point out why they are not solving your question.
  • Alex
    Alex over 13 years
    that might work for my site, but if I'm writing a public plugin or theme I can't do that...
  • 0xC0000022L
    0xC0000022L over 9 years
    Obviously only works for code that uses apply_filter. Anything else simply cannot be manipulated. So this solution depends on the original code making use of filters in the first place. Although more and more WordPress code (incl. plugins) does that, not all of them lend themselves to this method. Unfortunately. Still +1 for pointing it out.
  • Ian Dunn
    Ian Dunn almost 3 years
    WordPress itself does this with a few functions, like wp_mail(). That makes it possible for plugins to override them, but it's much better to use hooks whenever possible.