registration_redirect在插件文件中不起作用

时间:2017-02-21 14:15:44

标签: php wordpress

我想在注册后将用户重定向到 OTP Verificatio n Page。我在我的主题的functions.php中使用了以下代码来实现此功能,它工作正常,但是当我在自定义插件文件中使用此代码时,它无法正常工作。

add_filter( 'registration_redirect', 'wpesov_registration_redirect' );

function wpesov_registration_redirect() {

        return home_url( '/otp-verification');

    }

我需要在插件中更改什么,或者我错过了哪些内容?

TIA

1 个答案:

答案 0 :(得分:0)

我认为您的问题是在wordpress可以使用其内容之前加载了插件文件。

如果您在主插件文件中使用类,请执行以下操作:

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

并正确加载,然后你应该添加该函数作为插件类的方法并在构造函数中注册过滤器。您可以通过不同的方式初始化插件,因为我不知道您正在使用哪个插件,因此我无法提供更多帮助。如果它不起作用或你的init不同,请尝试实现上面的代码或在此发布你的主要插件文件结构。

编辑:或者在你的插件类中添加静态方法并在外面注册过滤器:

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