How to enable Gmail sing in notification for users in WordPress website

  • هذا الموضوع فارغ.
  • Post
    Weekend Wiki
    مدير عام
    To enable Gmail sign-in notifications for users on your WordPress website, you’ll need to set up Google authentication and configure email notifications. Here’s a step-by-step guide to help you achieve this:

    1. Set Up Google OAuth 2.0 for Authentication

    First, you need to enable Google Sign-In for your WordPress website. For this, you’ll use Google’s OAuth 2.0 API.

    • Go to the Google Developer Console: Visit Google Cloud Console.
    • Create a new project: Click on “Select a project” at the top and then click “New Project.”
    • Enable the OAuth 2.0 API:
      • Go to the “APIs & Services” > “Library.”
      • Search for “Google+ API” or “Google Identity Platform” and enable it.
    • Create OAuth Credentials:
      • Go to “APIs & Services” > “Credentials.”
      • Click on “Create Credentials” and select “OAuth 2.0 Client IDs.”
      • Set up the consent screen with required fields.
      • For “Authorized JavaScript origins,” enter your website URL (e.g., https://yourdomain.com).
      • For “Authorized redirect URIs,” use the redirect URI provided by your WordPress plugin (more on that later).
      • Copy the Client ID and Client Secret for later use.

    2. Install and Configure Google Sign-In Plugin on WordPress

    To simplify the process, you can use a plugin to integrate Google Sign-In with your WordPress site. One of the most popular options is Nextend Social Login.

    • Install Nextend Social Login:
      • Go to your WordPress dashboard, navigate to Plugins > Add New, and search for Nextend Social Login.
      • Install and activate the plugin.
    • Configure Google Sign-In:
      • After activating the plugin, go to Settings > Nextend Social Login.
      • Under “Providers,” click on “Google” and enter the Client ID and Client Secret you copied from the Google Developer Console.
      • Save the settings.

    3. Enable Gmail Sign-In Notifications

    To enable sign-in notifications, you need to set up email alerts from Google when users sign in using Gmail. Google doesn’t send login notifications to WordPress directly, so you can achieve this via a plugin or custom code.

    • Enable Gmail Sign-in Notifications via Google:
      • Google automatically sends email alerts to users when there’s a sign-in attempt from a new device or location. To ensure these notifications are active, ask users to check the “Security” section of their Google Account and enable “Sign-in & security alerts”.
    • WordPress Email Notifications for Sign-In (Optional): If you want to send custom email notifications to admins or users each time they log in with Google, you can use a plugin like WP Activity Log or a custom solution.
      • Install WP Activity Log from the plugin directory.
      • Go to WP Activity Log settings, and enable login tracking.
      • Configure email notifications for admin or specific actions (like login) under the plugin settings.

    Alternatively, you can create a custom function using WordPress hooks to trigger email notifications when a user logs in using Google.

    function google_sign_in_notification($user_login, $user) {
        $to = '[email protected]';  // Admin email or user email
        $subject = 'New Login via Google';
        $message = "User " . $user->user_login . " has logged in using Google Sign-In.";
        wp_mail($to, $subject, $message);
    }
    
    add_action('wp_login', 'google_sign_in_notification', 10, 2);
    

    4. Test the Google Sign-In and Notifications

    • Log out of your WordPress site.
    • Use the Google sign-in button on the login page.
    • Ensure that you receive the Gmail sign-in notifications and WordPress notifications.

    This setup will allow users to sign in using their Gmail account, and you’ll be notified via Gmail or WordPress depending on the method you choose.

  • يجب تسجيل الدخول للرد على هذا الموضوع.
arArabic