3

I'm trying to remove a filter on the register_url hook a parent theme added, that seems to have a class nomenclature I can't figure out.

Ordinarily, a filter for this hook would be easily added like so:

    add_filter( 'register_url', 'custom_register_url' );
    function custom_register_url( $register_url )
    {
        $register_url = "YOUR_PAGE_URL";
        return $register_url;
    }

However, the parent theme has added its own filter to this hook, and due to loading sequence, my child theme can't overwrite that filter (this issue is discussed here and demonstrated here and in detail here).

That process is relatively straightforward, but the parent theme I am working from has placed this filter and function within object oriented programming, which has my brain aching. WordPress Codex says

If a filter has been added from within a class, for example by a plugin, removing it will require accessing the class variable.

   global $my_class;
   remove_filter( 'the_content', array($my_class, 'class_filter_function') );

But I can't figure out the correct nomenclature to access this function. Here is a snippet of the parent theme:

    class APP_Registration extends APP_Login_Base {

        private static $_template;

        private $error;

        function get_action() {
            return 'register';
        }

        function __construct( $template ) {
            self::$_template = $template;
            parent::__construct( $template, __( 'Register', APP_TD ) );

            add_action( 'appthemes_after_registration', 'wp_new_user_notification', 10, 2 );
            add_filter( 'register_url', array( $this, '_change_register_url' ), 10, 1 );
        }

        function _change_register_url( $url ) {
            return self::get_url( 'raw' );
        }

    . . .

Trying to remove the filter just listing the function (_change_register_url) doesn't work:

    function custom_register_url( $register_url )
    {
        $register_url = "YOUR_PAGE_URL";
        return $register_url;
    }
    add_filter( 'register_url', 'custom_register_url' );

    function remove_parent_filters() {
        remove_filter( 'register_url', '_change_register_url' );
        add_filter( 'register_url', 'custom_register_url' );
    }
    add_action( 'after_setup_theme', 'remove_parent_filters' );

Because I assume, I need to indicate the function hooked by it's correct class name.

Can anyone help me figure out the proper remove filter syntax for this line?

        remove_filter( 'register_url', '_change_register_url' );
  • How is the APP_Registration class instantiated? – birgire Dec 9 '15 at 15:02
  • @birgire, does this help? list( $templates ) = get_theme_support( 'app-login' ); new APP_Login( $templates['login'] ); new APP_Registration( $templates['register'] ); – Bob Diego Dec 9 '15 at 15:31
2

It looks like the plugin doesn't want you do access the APP_Registration instance, so you can try this:

add_filter( 'register_url', function( $url )
{
    // Adjust this to your needs
    $url = site_url( 'wp-login.php?action=register', 'login' );

    return $url;
}, 11 );

to override the plugin's modifications, where we use a priority > 10.

|improve this answer|||||
3

Filters and actions should always be static methods exactly to make them easy to remove. If they are not static and the object is not a singleton (seems that way in your case) then there are no options but to remove all the hooks for that action/filter and add yours (@birgre's answer is a variation on this theme). While far from being elegant this might work if you do the child theme for yourself and therefor know in advance what plugins there are and what hooks they use.

The reason why the remove_filter do not work in case of non singleton object is that there have to be an exact match beetwen the pair of (object, method) used in add_action and remove_action and if you don't know the value of object you are doomed.

|improve this answer|||||
  • Very informative answer. I don't have the rep to vote your answer up, but I'm grateful. Is this an instance where the theme author neglected to make his function "pluggable," or is that a different concept? – Bob Diego Dec 9 '15 at 15:52
  • 1
    it is bad coding used to "hide" the fact that they use a global object. If the value returned by "new" was assigned to a globally accessible variable, you would have had an ugly implementation of a singleton and you would have been able to use the object as the object part of the array, and since it would have been the same object in add and remove, everything would have worked. – Mark Kaplun Dec 9 '15 at 16:06

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.