· 9 years ago · Nov 23, 2016, 08:12 AM
1function elh_insert_into_db() {
2global $wpdb;
3$table = $wpdb->prefix . "my_table";
4$charset_collate = $wpdb->get_charset_collate();
5$sql = "CREATE TABLE IF NOT EXISTS $table (
6 `id` mediumint(9) NOT NULL AUTO_INCREMENT,
7 `name` text NOT NULL,
8UNIQUE (`id`)
9) $charset_collate;";
10require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
11dbDelta( $sql );
12ob_start();
13?>
14<form action="#v_form" method="post" id="v_form">
15 <label for="visitor_name"><h3>Hello there! What is your name?</h3></label>
16 <input type="text" name="visitor_name" id="visitor_name" />
17 <input type="submit" name="submit_form" value="submit" />
18</form>
19<?php
20$html = ob_get_clean();
21if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" ) {
22 $table = $wpdb->prefix."my_table";
23 $name = strip_tags($_POST["visitor_name"], "");
24 $wpdb->insert(
25 $table,
26 array(
27 'name' => $name));
28 $html = "<p>Your name <strong>$name</strong> was successfully recorded. Thanks!!</p>";
29}
30if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
31 $html .= "<p>You need to fill the required fields.</p>";
32return $html; }add_shortcode('elh-db-insert', 'elh_insert_into_db');