简体   繁体   中英

registration_redirect Not working in plugin file

I want to redirect a user to OTP Verificatio n Page after registration. I used following code for this functionality in functions.php of my theme, it worked fine but when i used this code in my custom plugin file, its not working.

add_filter( 'registration_redirect', 'wpesov_registration_redirect' );

function wpesov_registration_redirect() {

        return home_url( '/otp-verification');

    }

What should i need to change in my plugin or am i missing something?

TIA

I think your problem is the plugin file is loaded before wordpress can work with its content.

If you are using class in your main plugin file like this:

class my_plugin
{

    public static function init() {
        $class = __CLASS__;
        new $class;
    }

    function __construct() {
        add_filter( 'registration_redirect', array( $this, 'wpesov_registration_redirect' ) );
    }

    public function wpesov_registration_redirect() {
        return home_url( '/otp-verification' );
    }
}

add_action( 'plugins_loaded', array( 'my_plugin', 'init' ) );

and loading it properly, then you should add that function as method of your plugin class and register filter in constructor. There are different ways you can initialize your plugin and since I don't know which one you are using I can't help further. Try to implement above code or post me your main plugin file structure here if it will not work or your init differs.

EDIT: or add static method into your plugin class and register filter outside:

class my_plugin
{

    public static function init() {
        $class = __CLASS__;
        new $class;
    }

    public static function wpesov_registration_redirect() {
        return home_url( '/otp-verification' );
    }
}
// init plugin
add_action( 'plugins_loaded', array( 'my_plugin', 'init' ) );

// init registration_redirect hook
add_filter( 'registration_redirect', array( 'my_plugin', 'wpesov_registration_redirect' ) );   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM