I have developed a plugin (despite minor understanding) that replaces pluggable function get_user_by to pull userdata from another source. While this works, it can lead to maintenance whenever there is a new WP version with changes in the pluggable function. I learned with get_avatar that it is best to filter rather than replace.

Is there a means to filter get_user_by rather than replacing it?

For example, using add_filter like the following doesn't appear to work.

Class XenWord_Login {

    public $visitor;
    public $user_id;
    public $current_user;

    public function __construct() {
        add_filter( 'get_user_by', array( &$this, 'getUserData' ) );

    function getUserData() {

        $visitor = XenWord::getVisitor();
        $user_id = $visitor['user_id'];
        $current_user = $visitor;

        $userdata = XenWord_XFUserData::xf_user_data( $visitor, $current_user,
        $user_id );

        if ( ! $userdata ) {
            return false;

        $user = new WP_User;
        $user->init( $userdata );

        return $user;

$login = new XenWord_Login();

The above is simply test code to try to filter rather than replace. Is there a better way?

1 Answer 1


If you look at the source get_user_by() is not too much more than a kind of interface to the WP_User object. You can see that it uses the methods 'get_data_by()`:

$userdata = WP_User::get_data_by( $field, $value );

And init:

$user = new WP_User;
$user->init( $userdata );

Neither offer any filters.

Nor does the cache object used by get_data_by()

I don't see a way to do it without replacing that function. Maybe somebody can spot something I don't.

  • Thank you @s_ha_dum. Since the point of replacing get_user_by is to have $userdata variable point to another source, would it be wiser to use update_user_meta and copy the information from the external source directly into the wp_usermeta table? Would get_user_by then pull that information? Thank you for thinking this through with me.
    – LPH
    Commented Dec 6, 2015 at 21:04
  • These are the fields that can be updated in the _users table: ID, user_pass, user_login, user_nicename, user_url, user_email, display_name, nickname, first_name, last_name, description, rich_editing, user_registered, role, show_admin_bar_front. Next, would be to use user_contactmethods for new meta data ...
    – LPH
    Commented Dec 6, 2015 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.