• Hi All,

    For those who are using Email Users and need to provide their subscribers with an option to unsubscribe, here is a code for a shortcode that can be used to create an unsubscribe link anywhere on the website. First, copy and paste the code below in your functions.php or a blank plugin, then place the shortcode [subscription] anywhere on the site (such as a new page and preferably a password protected section) you want the check boxes to appear. Remember to provide a link to the page in the footer of your mass emails and newsletters!

    function email_users_opt_in_out(){
    	
    	save_post_if_submitted();
    	$id = get_current_user_id();
    	$notif = get_user_meta( $id, 'email_users_accept_notifications', true );
    	$mass = get_user_meta( $id, 'email_users_accept_mass_emails', true );
    	
    	/*
    	//This section for testin purposes
    	echo '<p>'.$notif.'</p>';
    	echo '<p>'.$mass.'</p>';
    	*/
    	
    	if($notif == "true"){
    		$notifications = "checked";	
    	}else{
    		$notifications = "";
    	}
    	
    	/*
    	//This section for testing purposes
    	echo "<p>Notifications:</p>";
    	echo "<p>".$notifications."</p>";
    	*/
    	
    	if($mass == "true"){
    		$mass_emails = "checked";
    	}else{
    		$mass_emails = "";
    	}
    	
    	/*
    	//This section for testing purposes
    	echo "<p>Mass Emails:</p>";
    	echo "<p>".$mass_emails."</p>";
    	*/
    	
    	?>
    <div class="opt-form-container">
    <form id="new_post" name="new_post" method="post">
    </table>
    
    			<h3>Email Preferences</h3>
    
    	<table class="form-table">
    	<tbody>
    		<tr>
    			<th></th>
    			<td>
                	<input type="hidden" id="user_id_number" name="user_id_number" value=<?php echo $id; ?>></input>
    				<input type="checkbox"
    						name="email_users_accept_notifications"
    						id="email_users_accept_notifications"
    						value="notifications"
    						<?php echo $notifications;?> ></input>
    				Accept to receive post or page notification emails<br/>
    				<input type="checkbox"
    						name="email_users_accept_mass_emails"
    						id="email_users_accept_mass_emails"
    						value="mass_emails"
    						<?php echo $mass_emails;?> ></input>
    
    				Accept to receive emails sent to multiple recipients (but still accept emails sent only to me)			</td>
    		</tr>
    	</tbody>
    	</table>
        
        <?php //wp_nonce_field( 'wps-frontend-post' ); ?>
    
        <p align="right"><input type="submit" value="Save Changes" tabindex="6" id="submit" name="submit" /></p>
        
    </form>
    </div><!-- end of .opt-form-container -->
    <?php
    
    	}
    add_shortcode( 'subscription', 'email_users_opt_in_out' );
    
    function save_post_if_submitted() {
        if ( isset($_POST['email_users_accept_notifications']) ) {
    		update_user_meta( $_POST['user_id_number'], 'email_users_accept_notifications', 'true' );
        }else{
    		update_user_meta( $_POST['user_id_number'], 'email_users_accept_notifications', 'false' );
    	}
    
        if ( isset($_POST['email_users_accept_mass_emails']) ) {
    		update_user_meta( $_POST['user_id_number'], 'email_users_accept_mass_emails', 'true' );
        }else{
    		update_user_meta( $_POST['user_id_number'], 'email_users_accept_mass_emails', 'false' );
    	}
    
    }
  • The topic ‘Shortcode For Unsubscribe Check Boxes’ is closed to new replies.