Description

A simple plugin that displays a form on a page or post and then writes the information in the form to a table in the WordPress database.
To use it just put the short code [ccoc_form] into a page or post. This will insert the form into that page or post.

Details

When the plugin is activated it creates a table in the WordPress database called {prefix}ccocform. With 3 fields id, name and email.
To insert the form into a page or post just put in [ccoc_form] in the page or post source.

Screenshot of the form

When the user fills in the form and clicks submit, the data is then written to the database table and a message displayed to the user.
If the plugin is deleted from the site then it drops the table from the database.

Tested in WordPress 4.7.1

Code

		
	function ccocform_install()
	{
		// Create Database Table
		global $wpdb;
		$table_name = $wpdb->prefix . 'ccocform';
		$charset_collate = $wpdb->get_charset_collate();
		$sql = "CREATE TABLE IF NOT EXISTS $table_name (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			name varchar(50) NOT NULL,
			email varchar(80) NOT NULL,
			PRIMARY KEY  (id)
		) $charset_collate;";
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $sql );
	}
	
	function ccocform_uninstall()
	{
		// Drop Database Table
		global $wpdb;
		$table_name = $wpdb->prefix . 'ccocform';
		$wpdb->query("DROP TABLE IF EXISTS $table_name");
	}

	function ccocform_htmlcode() 
	{
		echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
		echo '<p>';
		echo 'Your Name (required) <br />';
		echo '<input type="text" name="ccocform-name" value="' . ( isset( $_POST["ccocform-name"] ) ? esc_attr( $_POST["ccocform-name"] ) : '' ) . '" size="40"  maxlength="50" required />';
		echo '</p>';
		echo '<p>';
		echo 'Your Email (required) <br />';
		echo '<input type="email" name="ccocform-email" value="' . ( isset( $_POST["ccocform-email"] ) ? esc_attr( $_POST["ccocform-email"] ) : '' ) . '" size="40"  maxlength="80" required />';
		echo '</p>';
		echo '<p><input type="submit" name="ccocform-submitted" value="Send"/></p>';
		echo '</form>';
	}

	function ccocform_writetodb()
	{
		if(isset($_POST['ccocform-submitted']))
		{
			global $wpdb;
			$name = sanitize_text_field( $_POST["ccocform-name"] );
			$email = sanitize_email( $_POST["ccocform-email"] );
			$table_name = $wpdb->prefix . 'ccocform';
			$result = $wpdb->insert( 
				$table_name, 
				array( 
					'name' => $name, 
					'email' => $email, 
				) 
			);
			// If it is written to database, display a success message
			if ( $result != false ) 
			{
				echo '<div>';
				echo '<p>Thanks for filling out the form your information has been added to the database.</p>';
				echo '</div>';			
				$_POST["ccocform-name"] = '';
				$_POST["ccocform-email"] = '';
			} 
			else 
			{
				echo 'An unexpected error occurred ($result)';
			}
		}
	}

	function ccocform_shortcode() 
	{
		ob_start();
		ccocform_writetodb();
		ccocform_htmlcode();
		return ob_get_clean();
	}

	register_activation_hook( __FILE__, 'ccocform_install' );
	register_uninstall_hook(__FILE__, 'ccocform_uninstall' );
	add_shortcode( 'ccoc_form', 'ccocform_shortcode' );
	
		


Download Plugin

References

https://codex.wordpress.org/Writing_a_Plugin
https://developer.wordpress.org/plugins/intro/
https://www.sitepoint.com/build-your-own-wordpress-contact-form-plugin-in-5-minutes/
https://codex.wordpress.org/Creating_Tables_with_Plugins
https://developer.wordpress.org/reference/functions/