0

My parent theme has some bugs in a function I then want to override in my child theme. The thing is it is neither pluggable and hookable. It is just defined like so :

// no if(!function_exists())
function parentThemeFunction() {
   //some bad coding
}
// no add_action

And that is all. It is not defined in functions.php either, but in "parentTheme/directory/file.php"

So i heard about "runkit_function_redefine" and "runkit_function_rename", but it means i have to implement "runkit" library on my server.

So far, my only option is to edit the original function in the parent theme. With the risk to see my code overrided at the very next theme update.

Here are the different calls :

// parentTheme/includes/profiles.php
class Fre_ProfileAction extends AE_PostAction {
    function __construct( $post_type = 'fre_profile' ) {
        // some unrelevant code

        $this->add_ajax( 'ae-profile-sync', 'sync_post' );
    }
    function sync_post() {
        // bad coding that i have to override
    }
}

// parentTheme/functions.php
class ET_FreelanceEngine extends AE_Base {
    function __construct() {

        // some unrelevant code
        $this->profile_action = new Fre_ProfileAction();
    }
}

global $et_freelance;
add_action( 'after_setup_theme', 'et_setup_theme' );
function et_setup_theme() {
    global $et_freelance;
    $et_freelance = new ET_FreelanceEngine();
    // some unrelevant code
}

Fre_ProfileAction extends AE_PostAction, which extends itself AE_Base class.

class AE_Base {
    public function add_ajax($hook, $callback, $priv = true, $no_priv = true, $priority = 10, $accepted_args = 1) {
        $this->add_action(self::AJAX_PREFIX . $hook, $callback, $priority, $accepted_args);
    }

    public function add_action($hook, $callback, $priority = 10, $accepted_args = 1) {
        add_action($hook, array(
            $this,
            $callback
        ) , $priority, $accepted_args);
    }
}

Any idea?

  • Well hopefully any future update to the theme would fix the issue. I'm not very familiar with it, but runkit_function_redefine probably wouldn't even work because the child theme is loaded first. What exactly is the function and the issue you're dealing with. A function in a theme ultimately has to be hooked somewhere or used in a template (or is used in a function that is hooked or in a template), otherwise it wouldn't do anything. – Jacob Peattie Feb 7 '18 at 13:20
  • Where is the function used? The options available here are very, very limited, and you've already defined most of them in your question, context might provide one or two more – Tom J Nowell Feb 7 '18 at 13:26
  • I just edited so that you could see how the code is implemented – user2183282 Feb 7 '18 at 14:22
  • if you want only change the AJAX call, show us what makes the method add_ajax. – mmm Feb 7 '18 at 15:14
  • I just edited my question again to answer you @mmm – user2183282 Feb 7 '18 at 15:28
1

to redefine the AJAX action, you can try that

add_action(AE_Base::AJAX_PREFIX . "ae-profile-sync", function () {

    $profile_action = $GLOBALS["et_freelance"]->profile_action;


    // remove base action

    remove_action(
        AE_Base::AJAX_PREFIX . "ae-profile-sync"
        ,
        [
            $profile_action,
            "sync_post",
        ]
        , 10
    );


    // add new AJAX action

    add_action(
        AE_Base::AJAX_PREFIX . "ae-profile-sync"
        , function () {

            // new code here


        }
    );


}, 1); // priority 1 to launch this before the method sync_post
  • God damnit thanks! You solved a terribly upsetting issue :) – user2183282 Feb 7 '18 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.