在 WordPress 中运行“functions.php”中的插件代码

问题描述 投票:0回答:1

我有一个允许注册时重复电子邮件的插件,如果我使用了该插件,则效果很好。我想在

functions.php
中使用插件代码(意思是说使用主题运行插件代码功能)。

下面是我的插件代码:

/**
 * Plugin Name: Allow duplicate emails on registration
 * Plugin URI: http://wordpress.stackexchange.com/a/125129/26350
 */
add_action( 'plugins_loaded', array( 'Allow_Duplicate_Emails_Registration', 'get_instance' ) );

class Allow_Duplicate_Emails_Registratn
{
    private $rand = '';

    static private $instance = NULL;

    static public function get_instance()
    {
        if ( NULL === self::$instance )
            self::$instance = new self;

        return self::$instance;
    }

    public function __construct()
    {
        // Generate some random string:
        $this->rand = '_remove_' . rand( 0, 99999);

        add_filter( 'user_registration_email', array( $this, 'user_registration_email' ) );
        add_action( 'user_register', array( $this, 'user_register' ) );

    }

    public function user_registration_email( $user_email )
    {
        // inject random string into the email
        $user_email = str_replace( '@', $this->rand . '@', $user_email );

        return $user_email;
    }

    public function user_register( $user_id )
    {
        // retrieve the newly registered user object
        $user = get_user_by( 'id', $user_id );

        // remove the random string
        $user_email = str_replace( $this->rand . '@', '@', $user->user_email );

        // update the email of the newly registered user
        $args = array(
            'ID'            => $user_id,
            'user_email'    => $user_email,
        );

        $result = wp_update_user( $args );
    }

}

我已经更改了上面的代码并放入了

functions.php
,但它不起作用:

add_action( 'init', array( 'Allow_Duplicate_Emails_Registration', 'get_instance' ) );
php wordpress wordpress-theming
1个回答
0
投票

我已经解决了这个问题。在 wp-content/themes/my-themes/functions.php 文件中添加以下代码:

<?php
    add_filter( 'user_registration_email', array( $this, 'user_registration_email' ) );
    add_action( 'user_register', array( $this, 'user_register' ) );

    function user_registration_email( $user_email )
    {
        // Inject a random string into the email
        $user_email = str_replace( '@', $this->rand . '@', $user_email );

        return $user_email;
    }

    function user_register( $user_id )
    {
        // Retrieve the newly registered user object
        $user = get_user_by( 'id', $user_id );

        // Remove the random string
        $user_email = str_replace( $this->rand . '@', '@', $user->user_email );

        // Update the email of the newly registered user
        $args = array(
            'ID'            => $user_id,
            'user_email'    => $user_email,
        );

        $result = wp_update_user( $args );
    }
?>
© www.soinside.com 2019 - 2024. All rights reserved.