从Wordpress中的functions.php运行插件代码

时间:2014-01-16 10:35:05

标签: wordpress wordpress-plugin wordpress-theming

我有一个允许注册时重复电子邮件的插件,如果我使用插件并且我想在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' ) );

1 个答案:

答案 0 :(得分: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 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 );  
    }
?>