How to auto log-in users that click on links in your email campaigns

Use the PHP code snippet below to auto-login users who click on links in your email campaigns.

This is useful, for example, when sending an abandoned cart email series or custom magic login links.

<?php
add_filter( 'noptin_actions_page_template', 'noptin_custom_code_autologin_user', 0 );
function noptin_custom_code_autologin_user( $template ) {
	$handler = noptin()->actions_page;

	// Check if a guest has clicked a link in a previous email.
	if ( 'email_click' === $handler->get_request_action() && 0 === get_current_user_id() ) {

		// Decode the recipient details.
		$recipient = $handler->get_request_recipient();

		// If we have a user id, log them in.
		if ( ! empty( $recipient['email'] ) ) {
			$user = get_user_by( 'email', $recipient['email'] );
		}

		if ( empty( $user ) && ! empty( $recipient['uid'] ) ) {
			$user = get_user_by( 'id', $recipient['uid'] );
		}

		if ( ! empty( $user ) ) {
			wp_set_current_user( $user->ID );
			wp_set_auth_cookie( $user->ID, true );
			do_action( 'wp_login', $user->user_login, $user );
		}
	}

	return $template;
}

Tip: Add this code snippet to your theme’s functions.php file or use the code snippets plugin.

Code Explanation

  • Line 2: Registers a filter hook that’s called whenever someone opens the Noptin actions page.
  • Line 7: Ensures the current action is a click event and that the user who took the action is not logged in.
  • Line 10: Decodes the current recipient details.
  • Line 21: Logs in the current recipient if their details match an existing user.

Related Guides

  • Visual Email Editor

    The visual email editor is a simple to use and a responsive editor for crafting and sending emails to your customers. The editor is user-friendly as it has inbuilt elements that allow you to drag and drop across your email fields for easier design. Therefore, you do not have to be a programmer to use…

    Read More

  • Tracking revenue generated per email

    Noptin allows you to track revenue generated per email. How to enable Open the Noptin settings page and mark both “Show campaign stats” and “Enable e-Commerce tracking” as shown below. How it works Supported Plugins Revenue is currently tracked for the following plugins:-

    Read More

  • How to fix emails not sending

    This tutorial will show you how to fix emails not sending or only sending to a portion of your subscribers. Fix emails only sending to a few subscribers Check sending limits First, check if your host (or email sender) has email-sending limits. If they do, inquire about their hourly limits then set it via Noptin…

    Read More

  • How to send a welcome email to imported subscribers

    Noptin allows you to send a welcome email to imported subscribers. To do this:- First, click on Noptin > Email Campaigns > Automated Emails to open the automated emails overview page. Next, click on the New Automated Email button and then select “Subscriber > Imported” under the Subscribers tab. Finally, select “Manual Recipients” as the…

    Read More

  • [Paid Memberships Pro] How to filter email recipients by their membership level

    Learn how to attach lead magnets and other attachments to your email newsletters and automated email campaigns.

    Read More

  • How to attach files to your email campaigns

    Learn how to attach lead magnets and other attachments to your email newsletters and automated email campaigns.

    Read More

  • How to send an email to all your newsletter subscribers

    Noptin allows you to send an email to all your newsletter subscribers. To do this:- First, click on Noptin > Email Campaigns to open the email campaigns management page. Next, click on the “New Campaign” button to open the email campaign creation page. Finally, click on the “Noptin Subscribers” button to create a new campaign.…

    Read More

  • How to Send an Email to All your WordPress Users

    This guide shows you how to send an email to all your WordPress users, or only those who have a certain user role.

    Read More

  • How to set up Automated Emails

    What are automated emails and how do you set them up?

    Read More

  • How to send an email to your WooCommerce Customers

    Learn how to send a mass email to all your WooCommerce customers, or only customers who bought specific products.

    Read More

  • New Post Notifications

    Learn how to automatically notify users whenever you publish new content, or set-up a daily, weekly, or monthly digest of your latest content.

    Read More

  • Automatically Send an Email to New Subscribers

    This guide shows you how to automatically send a welcome email to new email subscribers or set up a series of welcome emails to act as an email course.

    Read More

  • Automatically Send an Email to Email Subscribers Whenever they are Tagged or Untagged

    This guide shows you how to send a welcome email to your WordPress subscribers whenever you tag or untag them

    Read More

  • Automatically email your subscribers when the join or leave a list

    This guide shows you how to automatically send an email to your WordPress subscribers whenever they are added or removed from a list

    Read More

  • Automatically Send an Email to New WordPress Users

    This guide shows you how to send a welcome email to new WordPress users or set up a series of welcome emails to act as an email course.

    Read More

  • How to display posts in your email campaigns

    This guide shows you how to display blog posts, pages, products, and other custom content types in your email campaigns

    Read More

  • How to Automatically Send a Welcome Email to New WooCommerce Customers

    This guide shows you how to send a welcome email to new WordPress users or set up a series of welcome emails to act as an email course.

    Read More

  • Sending Expired Card Notification Emails

    This guide shows you how to send a welcome email to new WordPress users or set up a series of welcome emails to act as an email course.

    Read More

  • Email Sending Limits

    By default, when you send a newsletter campaign, Noptin will send out the email to as many subscribers as it can. This can cause a problem for those of us who use a shared hosting provider, since most of them have a limit on how many emails you can send per hour. Since Noptin version…

    Read More

  • Using a WordPress SMTP Plugin to Send your Newsletter Emails

    Noptin uses the same method that WordPress uses to deliver its emails. The default email delivery method is to send the email directly via your web server or hosting provider. The main problem with sending emails via your web server is that most of the emails will be marked as SPAM. In addition, most web…

    Read More

  • Email Templates

    Noptin does not come with a drag-and-drop email builder. Instead, it uses email templates to customize the look of your emails. You can think of an email template as a WordPress theme. It is given the email heading, content, and footer text and then produces the final HTML email. This allows you to do awesome…

    Read More

  • Merge Tags

    You can use email tags (also known as merge tags) to display dynamic and personalized content in your emails and success messages. This guide shows you how to personalize your emails and success messages using merge tags. What is a Merge Tag? A merge tag is a placeholder that allows you to personalize email campaigns…

    Read More

  • Troubleshooting

    Sometimes Noptin won’t be able to send emails to your subscribers. Whenever this happens, the problem will probably be with WordPress (or your SMTP plugin) and not Noptin. This is because Noptin uses WordPress to send its emails instead of sending them manually. This guide shows you how to troubleshoot most of the issues which…

    Read More

  • Troubleshooting Logos Not Showing in Noptin Emails

    If your logos are not displaying in your Noptin emails, it can be frustrating, but there are a few common issues you can check to resolve this problem. This guide will walk you through troubleshooting steps to ensure your logo appears correctly in your emails. 1. Verify the Logo URL The most common reason for…

    Read More

  • How to display past newsletter issues on your site

    Learn how to display past newsletter issues on your website using the [past_noptin_newsletters] shortcode

    Read More

  • How to Set Up an External CRON Job in WordPress and speed up email sending

    WordPress has a built-in scheduling system called WP-Cron, designed to handle time-based tasks. This is what Noptin uses when sending emails in the background. However, it is not often reliable as it is tied to your website traffic. This guide will walk you through the process of setting up an external CRON job for WordPress,…

    Read More

  • How to Bulk Edit Subscribers

    Sometimes, you might want to filter your email subscribers based on similar characteristics such as subscriber status, source or using given tags. This guide will show you how to edit your subscribers in bulk, saving you a lot of time. Login in to your WordPress Admin Panel, then on the right menu select Noptin >…

    Read More

  • How to Include a ‘View in Browser’ Link in Your Noptin Email Campaigns

    Adding a “View in Browser” link to your email campaigns allows recipients to open and view your email in their web browser. This can be especially useful for users who may have difficulty viewing the email in their email client or prefer to see it in a web format. With Noptin, you can easily include…

    Read More