رفتن به مطلب

ریدایرکت کاربر بعد از ورود و نمایش نام نمایشی


پست های پیشنهاد شده

سلام

قالب من برای ورود با حساب گوگل از این فایل استفاده می‌کنه

من میخوام کاربر بعد از ورود به my account ریدایرکت نشه و همان صفحه فعلی ریفریش بشه

فکر کنم مربوط به خط زیر باشه

 wp_redirect( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );

 

و اینکه کاربر بعد از ورود، نام جیمیل او به همراه عدد تصادفی نمایش داده میشه،

اما من میخوام نام و نام خانوادگی کاربر از حساب گوگلش دریافت کنه و در سایت نمایش بده

فکر کنم مربوط به  pre_user_nickname باشه

کسی هست کمکم کنه
 

<?php
add_action( 'init', 'mf_register_auth_urls' );
function mf_register_auth_urls() {
	add_rewrite_rule( '^auth/([\w-]+)', 'index.php?social_driver=$matches[1]', 'top' );
	flush_rewrite_rules();
}

add_filter( 'query_vars', 'mf_add_auth_query_vars' );
function mf_add_auth_query_vars( $vars ) {
	$vars[] = 'social_driver';

	return $vars;
}

add_action( 'parse_request', 'mf_handle_social_urls' );
function mf_handle_social_urls( $query ) {
	if (
		isset( $query->query_vars['social_driver'] )
		&& mf_get_option( 'auth_google' )
		&& mf_get_option( 'google_client_id' )
		&& mf_get_option( 'google_client_secret' )
	) {
		$driver    = $query->query_vars['social_driver'];
		$whitelist = [ 'google' ];
		if ( in_array( $driver, $whitelist ) ) {
			$method = 'mf_auth_by_' . $driver;
			if ( function_exists( $method ) ) {
				$method();
			}
		}
		exit;
	}
}

function mf_auth_by_google() {
	$client_id     = mf_get_option( 'google_client_id' );
	$client_secret = mf_get_option( 'google_client_secret' );

	include MF_THEME_PATH . 'library/hybridauth/autoload.php';

	$config = [
		'callback' => esc_url( home_url( 'auth/google' ) ),
		'keys'     => [ 'key' => $client_id, 'secret' => $client_secret ]
	];

	try {
		$google = new Hybridauth\Provider\Google( $config );
		$google->authenticate();
		$userProfile = $google->getUserProfile();

		if ( ! empty( $userProfile ) ) {
			$email = $userProfile->email;

			if ( email_exists( $email ) ) {
				$user_data = get_user_by( "email", $email );
				mf_login_user( $user_data );
				wp_redirect( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );
			} else {
				list( $user_login, $no_matter_data ) = explode( '@', $email );
				$u_login            = $user_login . rand( 1000, 9999999 );
				$password           = bin2hex( random_bytes( 15 ) );
				$user_register_data = [
					'user_login' => apply_filters( 'pre_user_login', $u_login ),
					'user_email' => apply_filters( 'pre_user_email', $email ),
					'user_pass'  => apply_filters( 'pre_user_pass', $password ),
				];

				$user_id = wp_insert_user( $user_register_data );
				if ( ! is_wp_error( $user_id ) ) {
					$user_data = get_user_by( "id", $user_id );
					mf_login_user( $user_data );

					do_action( 'user_register', $user_id );

					wp_redirect( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );
				}
			}
		}
	} catch ( \Exception $e ) {
		echo 'Oops, we ran into an issue! ' . $e->getMessage();
	}
}

function mf_login_user( $user ) {
	wp_set_current_user( $user->ID, $user->user_login );
	wp_set_auth_cookie( $user->ID );
	do_action( 'wp_login', $user->user_login, $user );
}

 

socials-auth.php

لینک به ارسال

لطفا برای ارسال دیدگاه وارد شوید

شما بعد از اینکه وارد حساب کاربری خود شدید می توانید دیدگاهی ارسال کنید



ورود به حساب کاربری
×
×
  • اضافه کردن...