19

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.

T0xicCode
  • 4,583
  • 2
  • 37
  • 50
Alex
  • 66,732
  • 177
  • 439
  • 641
  • Keep in mind that this could make trouble with further wordpress updates... – Tobias Jan 18 '11 at 14:33
  • 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? – Phill Pafford Jan 18 '11 at 14:36
  • possible duplicate of [Redefine Built in PHP Functions](http://stackoverflow.com/questions/2326835/redefine-built-in-php-functions) and [Redefining PHP Function](http://stackoverflow.com/questions/2640958/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. – Gordon Jan 18 '11 at 15:23

5 Answers5

9

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

Morgan Estes
  • 223
  • 3
  • 16
rachael
  • 99
  • 1
  • 1
  • 2
    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. – 0xC0000022L Aug 11 '14 at 17:37
6

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.

atiquratik
  • 1,296
  • 3
  • 27
  • 34
coreyward
  • 77,547
  • 20
  • 137
  • 166
6

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.

DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
  • WordPress itself does this with [a few functions](https://github.com/WordPress/WordPress/blob/cbfa060/wp-includes/pluggable.php), like `wp_mail()`. That makes it possible for plugins to override them, but it's much better to [use hooks](https://stackoverflow.com/a/8125517/450127) whenever possible. – Ian Dunn May 06 '21 at 21:14
5

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.)

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • 1
    that might work for my site, but if I'm writing a public plugin or theme I can't do that... – Alex Jan 18 '11 at 14:32
3

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

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Andrew Seabrook
  • 397
  • 2
  • 17