简体   繁体   English

registration_redirect在插件文件中不起作用

[英]registration_redirect Not working in plugin file

I want to redirect a user to OTP Verificatio n Page after registration. 我想在注册后将用户重定向到OTP验证页面。 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. 我在主题的functions.php中使用了以下代码来实现此功能,但效果很好,但是当我在自定义插件文件中使用此代码时,则无法正常工作。

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 TIA

I think your problem is the plugin file is loaded before wordpress can work with its content. 我认为您的问题是在wordpress可以使用其内容之前加载了插件文件。

If you are using class in your main plugin file like this: 如果您在这样的主插件文件中使用class:

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. 尝试实现上述代码,或者在主插件文件结构不起作用或您的init有所不同的情况下,将其发布在此处。

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' ) );   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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