Forum Replies Created
-
AuthorPosts
-
April 9, 2024 at 11:53 am in reply to: WP Users integration / custom coded subscription form #324924herzstueckParticipant
Thank 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.herzstueckParticipantHello 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;
-
AuthorPosts