Custom data migration from WordPress to discourse

I have used discourse script and migrated phpbb data to discourse, now i have moved my site to WordPress and I need to migrate some user data, I have made custom user fields in discourse, and used this hook “wpdc_sso_params” to migrate custom fields data.
but problem is it sync data when user logged in, if some user not logged in again or expired users data will not migrate in this way.
is there any hook or script that can migrate all users data at once.

other thing is like we are mapping custom fields, how can I map full name and first and last name fields? like we can set custom avatar using “wpdc_sso_avatar_url” this hook.

If the WP Discourse plugin is activated on your WordPress site, you can write a script that loops through all your site’s users and calls the sync_sso_record function with the SSO params for each user you want to update:

It is a static function, so you need to call it with:

WPDiscourse\Utilities\Utilities::sync_sso_record($sso_params_array, $wp_user_id)

It has been a while since I tried to do this. Test it with one user before trying to loop through all users on the site.

You could use the Discourse external_name field for the full name. The name you set with the external_name field will become the user’s Name on Discourse. If you need separate first and last name fields, you will have to create custom fields for them on Discourse.

Let me know if you run into any trouble with this. I can try to test it out tomorrow.

1 Like
$myusers = get_users( );
	$sso_params_array = [];
	$sso_params_array['custom.user_field_1'] = $user->user_login;
	$sso_params_array['custom.user_field_2'] = get_user_meta( $user->ID, 'first_name', true );
	$sso_params_array['custom.user_field_3'] = get_user_meta( $user->ID, 'last_name', true );
	foreach ( $myusers as $myuser ) {
		WPDiscourse\Utilities\PublicPluginUtilities::sync_sso_record($sso_params_array, $myuser->ID;);
	}

@simon am I doing it right?

You need to set the $sso_params_array inside of the foreach loop. Something like:

$myusers = get_users();
foreach ( $myusers as $myuser ) {
    $sso_params_array = [];
    $sso_params_array['custom.user_field_1'] = $myuser->user_login;
    $sso_params_array['custom.user_field_2'] = get_user_meta( $myuser->ID, 'first_name', true );
    $sso_params_array['custom.user_field_3'] = get_user_meta( $myuser->ID, 'last_name', true );

	WPDiscourse\Utilities\Utilities::sync_sso_record($sso_params_array, $myuser->ID;);
	}

Try it with just one user first though. For example:

$myuser = get_user_by('id', 1);

It might be best to create a test user for this. You can get the user with this function: get_user_by() | Function | WordPress Developer Resources

3 Likes

@simon
this is giving me a fatal error when using
WPDiscourse\Utilities\PublicPluginUtilities::sync_sso_record($sso_params_array, $myuser->ID);

PHP Fatal error: Uncaught Error: Call to undefined method WPDiscourse\Utilities\PublicPluginUtilities::sync_sso_record()

wp discourse plugin is active and sso is working fine.

I tried
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
DiscourseUtilities::sync_sso_record($sso_params, 3992);

in this way it not giving fatal error, but custom fields data is not saving in discourse.

returning this error

WP_Error Object ( [errors] => Array ( [wpdc_response_error] => Array ( [0] => An invalid response was returned from Discourse ) ) [error_data] => Array ( [wpdc_response_error] => Array ( [http_code] => 422 [http_body] => {“failed”:“FAILED”,“message”:“The external_id is required but was blank”} ) ) [additional_data:protected] => Array ( ) )

1 Like

@simon can you please check the problem

Hi muhammad :slight_smile:

Everyone here does their best to help solve problems. Simon’s helping you and is automatically notified of replies unless he disabled the notifications.

So, there is no need to @mention him and bump a topic, especially after waiting less than 3 hours only. Please be patient in this regard :slight_smile:

2 Likes

Sorry about the error in the code I supplied. I was reading the plugin’s code wrong. The way you are calling the function is correct. This should also work:

WPDiscourse\Utilities\Utilities::sync_sso_record($sso_params, 3992);

I will edit that into my previous post.

This is the error:

{“failed”:“FAILED”,“message”:“The external_id is required but was blank”}

You need to set the external_id parameter in your $sso_params_array. The external_id should be set to the user’s WordPress ID.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.