Support » Plugin: WordPress Importer » Importing Posts Check if user exists already

  • Hello,

    After using this plugin for a while it got to the point when importing several .xml backups from a site required a lot of time assigning the posts to a author when the author was identical from both sites.

    I made a change to the plugin itself to ensure that this checked if the user existed within the current pool and then selecting that automatically without requiring any human input unless they don’t exist. Saved me a fair bit of time and taught it would be useful for you guys to have this change if your importing large number of posts from many authors.

    The code is found from line 298 in wordpress-importer.php

    function author_select( $n, $author ) {
    		_e( 'Import author:', 'wordpress-importer' );
    		echo ' <strong>' . esc_html( $author['author_display_name'] );
    		if ( $this->version != '1.0' ) echo ' (' . esc_html( $author['author_login'] ) . ')';
    		echo '</strong><br />';
    
    		if ( $this->version != '1.0' )
    			echo '<div style="margin-left:18px">';
    
    		$create_users = $this->allow_create_users();
    		if ( $create_users ) {
    			if ( $this->version != '1.0' ) {
    				_e( 'or create new user with login name:', 'wordpress-importer' );
    				$value = '';
    			} else {
    				_e( 'as a new user:', 'wordpress-importer' );
    				$value = esc_attr( sanitize_user( $author['author_login'], true ) );
    			}
    
    			echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />';
    		}
    
    		if ( ! $create_users && $this->version == '1.0' )
    			_e( 'assign posts to an existing user:', 'wordpress-importer' );
    		else
    			_e( 'or assign posts to an existing user:', 'wordpress-importer' );
    			if ( username_exists( esc_attr( $author['author_login'] ) ) ){
    				wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ), 'selected'=> get_user_by( 'login', esc_attr($author['author_login']) )->ID ) );
    			}else{
    				wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) );
    			}
    		echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />';
    
    		if ( $this->version != '1.0' )
    			echo '</div>';
    	}

    Hope this helps,

    **– Note, this adds more server overhead for importing posts –**

    https://wordpress.org/plugins/wordpress-importer/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi NeekTheNook, that’s funny. I have been doing a similar mod and came here to post it. My approach is slightly different in that it uses the user ID’s straight from the $this->authors array. If no user ID’s are there, it uses your code to retrieve them.

    First, apply this mod to the get_authors_from_import():

    function get_authors_from_import( $import_data ) {
    
    	...
    
    			if ( ! isset($this->authors[$login]) )
    				$this->authors[$login] = array(
    					'author_id' => username_exists( esc_attr( $author['author_login'] ) ) ? get_user_by( 'login', esc_attr($author['author_login']) )->ID : '',
    					'author_login' => $login,
    					'author_display_name' => $post['post_author']
    				);
    		}
    	}
    }

    Note: only one line 'author_id' =>... is added.

    Next, only this mod to author_select() is needed:

    wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'selected' => $author['author_id'], 'show' => 'user_login', 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) );

    Notes:
    1. The parameter 'selected' => $author['author_id'] takes care of the dropdown selection
    2. I chose to append the 'show' => 'user_login' parameter because I found that sometimes when different authors use the same display name, it’s impossible to distinguish between them in the dropdown list.

    Thread Starter NeekTheNook

    (@neekthenook)

    Hello RavanH, I thaught about your approach first, however my concern was if the ID of the poster in the imported .xml was a completely different user in the destination WordPress, then it would asign the wrong posts to the wrong person causing more issues later on. however your approach does seem to be cleaner from what I can see

    -Nick

    You are right: I was importing from one site to another within the same multisite installation. In that case, the IDs match but when moving from one single site to another that will likely not be the case.

    So I think the best solution to fit all cases would be a mix of these two approaches where is there is no user ID or if it does not match the user_login on the new site, it will fall back to a search by user_login 🙂

    Amazing! thanks NeekTheNook.

    Too bad the plugin devs do not seem to be monitoring this forum 🙁

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Importing Posts Check if user exists already’ is closed to new replies.