Simple Subscribe Form WordPress Without Using Plugin |
X

Congrats, You are Subscribed to Receive Updates.

Simple Subscribe Form WordPress Without Using Plugin


Simple Subscribe Form WordPress  without using any plugin. You can create your own features without using third party plugin for it. Let’s make a plugin for our functionality of simple email subscription. email subscription is a simple form submission with email. So let’s make the form first.

email-subscription

Subscription Form : 

The following code helps you to create your form for email news letter subscription.

<form id="newsletter-footer" action="" method="POST">
									
	<h5><strong>Sign up</strong> for email updates</h5>
	<div class="newsletter-form">
		<div class="newsletter-email" style="margin-bottom:10px; " >
			<input type="email" name="subscriber_email" placeholder="Email address">
		</div>
		<div class="newsletter-submit">
			<input type="hidden" name="kv_submit_subscription" value="Submit">
			<input type="submit" name="submit_form" value="Submit">							
		</div>
	</div>
</form>

We have to handle the form submission by using the below code.

if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['kv_submit_subscription'])) {

	if( filter_var($_POST['subscriber_email'], FILTER_VALIDATE_EMAIL) ){
	
		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
					 
		$subject = sprintf(__('New Subscription on %s','kvc'), $blogname);
					 
		$to = get_option('admin_email'); 
					 
		$headers = 'From: '. sprintf(__('%s Admin', 'kvc'), $blogname) .' <No-repy@'.$_SERVER['SERVER_NAME'] .'>' . PHP_EOL;
					 
		$message  = sprintf(__('Hi ,', 'kvc')) . PHP_EOL . PHP_EOL;
		$message .= sprintf(__('You have a new subscription on your %s website.', 'kvc'), $blogname) . PHP_EOL . PHP_EOL;
		$message .= __('Email Details', 'kvc') . PHP_EOL;
		$message .= __('-----------------') . PHP_EOL;
		$message .= __('User E-mail: ', 'kvc') . stripslashes($_POST['subscriber_email']) . PHP_EOL;
		$message .= __('Regards,', 'kvc') . PHP_EOL . PHP_EOL;
		$message .= sprintf(__('Your %s Team', 'kvc'), $blogname) . PHP_EOL;
		$message .= trailingslashit(get_option('home')) . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL;
				
		if (wp_mail($to, $subject, $message, $headers)){					
			echo 'Your e-mail (' . $_POST['subscriber_email'] . ') has been added to our mailing list!';
		}else{
			echo 'There was a problem with your e-mail (' . $_POST['subscriber_email'] . ')';   
		}
				
	}else{
		echo 'There was a problem with your e-mail (' . $_POST['subscriber_email'] . ')';   
	}
}

The above form and php code helps us to handle the form and its submission activities. Let’s put a wrapper function for both codes.

if(!function_exists('kv_email_subscription_fn')) {
	add_action('kv_email_subscription' , 'kv_email_subscription_fn' );

	function kv_email_subscription_fn() {
		// Here your php code and its form components..
	}
}

But, we may need this feature as on widget or sidebar or in the footer.

Screenshot_2016-2-7 11.34.42

we can make it as widget and short-code to use it anywhere in your theme and plugin.

Widget:

Making widget is simple, we will create the wrapper functions of widget and just use the `do_action` to work in side out widget. We will extend the class of `WP_Widget` and create our class for it. Here is our code.

class Kv_Subscription_widget extends WP_Widget {

	public function __construct() {
		$widget_ops = array( 
			'classname' => 'kv_email_subscription',
			'description' => 'A Simple Email Subscription Widget to get subscribers info',
		);
		parent::__construct( 'my_widget', 'Kv Subscriptions', $widget_ops );
	}

	public function widget( $args, $instance ) {
		echo '<aside>'; 
		do_action('kv_email_subscription'); // just adding this function will helps you to call the form and its actions with in it.
		echo '</aside>';
	}
}

And, now we have to register this widget within your present existing widgets.  So your  Code will be

add_action( 'widgets_init', function(){
	register_widget( 'Kv_Subscription_widget' );
});

Screenshot_2016-2-7 11.37.3

Short-Code:

Also you have to make it as short-code. So we can use it inside any page or post. and even you can extend it on your active theme. So the code will be

add_action( 'init', 'kv_register_shortcode_for_newsletter');

function kv_register_shortcode_for_newsletter(){
	
	add_shortcode('kv_email_subscriptions', 'kv_email_subscription_fn' );
}	

So we created a custom short-code `kv_email_subscriptions` . you can use it widely.  Here is my  complete code.

<?php 
/*
Plugin Name: Kv Simple Subscription
Plugin URI: http://wordpress.org/plugins/kv-simple-subscription/
Description: This plugin helps you to get subscriptions through email and you can get email for every user signup for news letters.
Author: Kvvaradha
Version: 1.0
Author URI: https://www.kvcodes.com/
*/

add_action( 'init', 'kv_register_shortcode_for_newsletter');

function kv_register_shortcode_for_newsletter(){
	
	add_shortcode('kv_email_subscriptions', 'kv_email_subscription_fn' );
}

class Kv_Subscription_widget extends WP_Widget {

	public function __construct() {
		$widget_ops = array( 
			'classname' => 'kv_email_subscription',
			'description' => 'A Simple Email Subscription Widget to get subscribers info',
		);
		parent::__construct( 'my_widget', 'Kv Subscriptions', $widget_ops );
	}

	public function widget( $args, $instance ) {
		echo '<aside>'; 
		do_action('kv_email_subscription');
		echo '</aside>';
	}
}

add_action( 'widgets_init', function(){
	register_widget( 'Kv_Subscription_widget' );
});
	

if(!function_exists('kv_email_subscription_fn')) {
	add_action('kv_email_subscription' , 'kv_email_subscription_fn' );

	function kv_email_subscription_fn() {

		if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['kv_submit_subscription'])) {

			if( filter_var($_POST['subscriber_email'], FILTER_VALIDATE_EMAIL) ){
				
				 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
				 
				 $subject = sprintf(__('New Subscription on %s','kvc'), $blogname);
				 
				 $to = get_option('admin_email'); 
				 
				 $headers = 'From: '. sprintf(__('%s Admin', 'kvc'), $blogname) .' <No-repy@'.$_SERVER['SERVER_NAME'] .'>' . PHP_EOL;
				 
				$message  = sprintf(__('Hi ,', 'kvc')) . PHP_EOL . PHP_EOL;
				$message .= sprintf(__('You have a new subscription on your %s website.', 'kvc'), $blogname) . PHP_EOL . PHP_EOL;
				$message .= __('Email Details', 'kvc') . PHP_EOL;
				$message .= __('-----------------') . PHP_EOL;
				$message .= __('User E-mail: ', 'kvc') . stripslashes($_POST['subscriber_email']) . PHP_EOL;
				$message .= __('Regards,', 'kvc') . PHP_EOL . PHP_EOL;
				$message .= sprintf(__('Your %s Team', 'kvc'), $blogname) . PHP_EOL;
				$message .= trailingslashit(get_option('home')) . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL;
			
				if (wp_mail($to, $subject, $message, $headers)){
				
					echo 'Your e-mail (' . $_POST['subscriber_email'] . ') has been added to our mailing list!';
				}	else	{
				   echo 'There was a problem with your e-mail (' . $_POST['subscriber_email'] . ')';   
				}
			}else{
			   echo 'There was a problem with your e-mail (' . $_POST['subscriber_email'] . ')';   
			}
		}?>

		<div class="col-lg-4 col-md-4 col-sm-4 twitter-widget-area animate-onscroll">
								
			<form id="newsletter-footer" action="" method="POST">
									
				<h5><strong>Sign up</strong> for email updates</h5>
				<div class="newsletter-form">
									
					<div class="newsletter-email" style="margin-bottom:10px; " >
						<input type="email" name="subscriber_email" placeholder="Email address">
					</div>
					<div class="newsletter-submit">
							<input type="hidden" name="kv_submit_subscription" value="Submit">
							<input type="submit" name="submit_form" value="Submit">							
					</div>
				</div>
			</form>
		</div>							
	<?php }

} ?>

Just save it on a separate file and put it inside wp plugins directory. Than activate it.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

3 comments

  1. commenter

    hi there
    i have q
    where can i find email list how subscription to my website

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar