Home › Forums › Newsletter Plugin Support › WP Users integration / custom coded subscription form
- This topic has 5 replies, 2 voices, and was last updated 7 months, 1 week ago by Michael.
-
AuthorPosts
-
April 2, 2024 at 12:40 pm #324096herzstueckParticipant
Hello,
I’m using the plugin with the WP Users integration to subscribe new users when they register.I have a locked area which only registered and logged-in users can access. What I want to do is offering a newsletter subscription form inside the locked area and connecting the currently logged-in user to the newsletter subscriber that is created by the form.
I have created a custom subscription form as it is shown in your documentation. The only thing I changed is that I pre-defined the form input values based on the user’s profile information (name, email address). Now I don’t know how to make my form connect the user_id to the newly created subscriber. Can you guys help?
Is there maybe a hidden field that I can use with my custom coded form?Thanks in advance!
April 3, 2024 at 9:22 am #324157MichaelKeymasterHello,
I am sorry but I didn’t fully understand what you’re trying to achieve here. You said you have Wp User addon active, and a private area that only registered users can access, with a custom coded form. So at that point, registered users aren’t subscribers yet? Can you please elaborate further?
Thanks,
MichaelApril 4, 2024 at 7:53 am #324230herzstueckParticipantHello Michael,
thanks for checking back and sorry for the confusion.
I am using the WP Users integration in a way where I offer the option to subscribe during user registration. I want to offer the option to subscribe in the private area because there might be people who don’t tick the checkbox when they register, but maybe change their mind later on. All of this happens on the front-end of the website.
I managed to create a shortcode to check if the currently logged-in user is already a subscriber like:
// Get Newsletter instance $newsletter = Newsletter::instance(); // Get current user ID $user_id = get_current_user_id(); // Check if user is a subscriber $user_is_subscriber = $newsletter->get_user_by_wp_user_id($user_id); // If user is a subscriber, return enclosed content if ($user_is_subscriber) { return do_shortcode($content); }
If the user is not a subscriber yet, I want to show the subscription form. This basically works but I’m missing the part where I assign the user_id to the subscriber generated by the form.
This is what my code looks like generating the form and pre-populating the fields with information from the user’s profile:
$newsletter = Newsletter::instance();
// Get user_id of the currently logged-in user
$user_id = get_current_user_id();// Get user data to pre-populate the form
if ($user_id > 0) {
$user_info = get_userdata($user_id);
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
$email = $user_info->user_email;
} else {
$first_name = '';
$last_name = '';
$email = '';
}// Create an HTML form and pre-populate data if available
$form_html = '<div class="tnp-subscription">
<form method="post" action="https://www.my-website.com/?na=s">
<input type="hidden" name="nl[]" value="1">
<input type="hidden" name="optin" value="single">
<input type="hidden" name="nlang" value="">
<div class="tnp-field tnp-field-firstname"><label for="tnp-1">First name</label>
<input class="tnp-name" type="text" name="nn" id="tnp-1" value="' . esc_attr($first_name) . '" placeholder=""></div>
<div class="tnp-field tnp-field-surname"><label for="tnp-2">Last name</label>
<input class="tnp-surname" type="text" name="ns" id="tnp-2" value="' . esc_attr($last_name) . '" placeholder=""></div>
<div class="tnp-field tnp-field-email"><label for="tnp-3">Email</label>
<input class="tnp-email" type="email" name="ne" id="tnp-3" value="' . esc_attr($email) . '" placeholder="" required=""></div>
<div class="tnp-field tnp-field-button" style="text-align: left"><input class="tnp-submit" type="submit" value="Subscribe">
</div>
</form>
</div>';return $form_html;
April 8, 2024 at 5:27 pm #324619MichaelKeymasterHello,
unfortunately we do not offer support for custom code. Anyway, in your case I’d take a look at this hook: https://www.thenewsletterplugin.com/documentation/developers/dev-newsletter-hooks/#filter-newsletter-user-subscribe
Thanks,
MichaelApril 9, 2024 at 11:53 am #324924herzstueckParticipantThank you, Michael.
I have solved the issue using your suggested filter like this:
add_filter('newsletter_user_subscribe', 'nda_filter_newsletter_user_subscribe', 10, 1);
function nda_filter_newsletter_user_subscribe($user) {
if (!class_exists('Newsletter') || !method_exists('Newsletter', 'instance')) {
return '';
}if( is_user_logged_in() && !empty($_REQUEST['wp-user-id']) ) :
$user->wp_user_id = get_current_user_id();
endif;return $user;
}and adding
<input type="hidden" name="wp-user-id" value="'.$user_id.'">
to my custom subscription form.April 10, 2024 at 9:39 am #324976MichaelKeymasterHello,
glad to have pointed you in the right direction : )
Have a nice day,
Michael -
AuthorPosts
- You must be logged in to reply to this topic.