WordPress.org

Ready to get started?Download WordPress

Forums

Import Users from CSV
[resolved] Add existing user to blog instead of throw error? (8 posts)

  1. plugins_lexblog
    Member
    Posted 1 year ago #

    Hey,

    Awesome plugin. Works great and is very simple. Great work.

    One question: I notice that if the plugin encounters an existing username/email address among the spreadsheet data, it just adds that user to the error log and moves on. We would like to see this instead:

    The plugin should still log this, but instead of just skipping the user, it should add the existing user to the current blog (this is only relevant in a WPMU setup).

    Any thoughts on adding this as a checkbox option? Any advice on hacking the plugin code myself in pursuit of this?

    http://wordpress.org/extend/plugins/import-users-from-csv/

  2. Ulrich Sossou
    Member
    Plugin Author

    Posted 1 year ago #

  3. plugins_lexblog
    Member
    Posted 1 year ago #

    Thank you for the response, Ulrich.

    I guess I was thinking about something more like,

    http://codex.wordpress.org/Function_Reference/username_exists

    http://codex.wordpress.org/Function_Reference/email_exists

    if(username_exists() || email_exists()) {
        just add the guy to this blog and log this in the log.
    }

    Does that make sense? I saw the function you pasted in the other thread, but I didn't see how that would apply here.

  4. Ulrich Sossou
    Member
    Plugin Author

    Posted 1 year ago #

    Something like this should work (not tested).

    function example_add_user_to_blog( $userdata ) {
      global $wpdb;
    
      if ( username_exists( $userdata['user_login'] ) || email_exists( $userdata['user_email'] ) ) {
        if ( $userdata['user_login'] )
          $user = get_user_by( 'login', $userdata['user_login'] );
        else
          $user = get_user_by( 'email', $userdata['user_email'] );
    
        add_user_to_blog( $wpdb->blog_id, $user_id, $userdata['role'] );
    
        $userdata['ID'] = $user->ID;
      }
    
      return $userdata;
    }
    add_filter( 'is_iu_import_userdata', 'example_filter_import_userdata' );
  5. plugins_lexblog
    Member
    Posted 1 year ago #

    Awesome, thank you. I ended up adding a modified part of that snippet to line 280 of your plugin:

    if ( username_exists( $userdata['user_login'] ) || email_exists( $userdata['user_email'] ) ) {
            		if ( $userdata['user_login'] ) {
              			$user = get_user_by( 'login', $userdata['user_login'] );
            		} else {
              			$user = get_user_by( 'email', $userdata['user_email'] );
    				}
            		$user_id = add_user_to_blog( get_current_blog_id(), $user->ID, $userdata['role'] );
    
    		        $userdata['ID'] = $user->ID;
    
          		}

    Obviously, that's going to bite the dust whenever we update your plugin. Would you consider integrating this, or something similar, into your plugin?

  6. Ulrich Sossou
    Member
    Plugin Author

    Posted 1 year ago #

    I will consider it.

    Why didn't you add the snippet in your theme functions.php file or somewhere else it won't be affected by an update?

  7. plugins_lexblog
    Member
    Posted 1 year ago #

    Very good question.

    1) We use the same parent theme across hundreds of blogs, so we try to avoid adding code that will only apply to any small subset of them (which this would, in our case).

    2) We try to avoid making any changes at all to the parent theme because we are in a very demanding vertical and it's not feasible to test code changes to the parent theme across all our blogs.

    3) It would be tedious to remember to add this in the child theme of every blog that needs it.

    4) We could just write another plugin to include this as necessary, but it will be difficult to remember to activate our plugin every time we activate your plugin.

    5) We're already running a ton of plugins that are network activated. We're trying to cut back as it is, so network activating this AND our patch is not an option.

    6) It's a good update! Maybe include it as a conditional based on an option in the management page?

  8. Ulrich Sossou
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks for explaining. I will add it.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.