-
-
Save vicsimental/83bf06277a6a73cdc6dbc503380d3b6e to your computer and use it in GitHub Desktop.
Ajax in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Quick Contact | |
* @version 0.1 | |
*/ | |
/* | |
Plugin Name: Quick Contact | |
Plugin URI: http://techslides.com/ | |
Description: Quick Contact WordPress Plugin to make an Ajax form submission, store it in the database, and show it in a Admin backend page. | |
Version: 0.1 | |
Author URI: http://techslides.com/ | |
*/ | |
register_activation_hook(__FILE__,'qc_install'); | |
register_deactivation_hook(__FILE__, 'qc_uninstall' ); | |
global $jal_db_version; | |
$jal_db_version = "1.0"; | |
function qc_install() { | |
global $wpdb; | |
global $jal_db_version; | |
$table_name = $wpdb->prefix . "quickcontact"; | |
//http://codex.wordpress.org/Creating_Tables_with_Plugins | |
$sql = "CREATE TABLE $table_name ( | |
id mediumint(9) NOT NULL AUTO_INCREMENT, | |
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, | |
name VARCHAR(120) NOT NULL, | |
email VARCHAR(120) DEFAULT '' NOT NULL, | |
message text NOT NULL, | |
UNIQUE KEY id (id) | |
);"; | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
add_option("jal_db_version", $jal_db_version); | |
} | |
function qc_uninstall() { | |
global $wpdb; | |
global $jal_db_version; | |
$table_name = $wpdb->prefix . "quickcontact"; | |
$wpdb->query("DROP TABLE IF EXISTS $table_name"); | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
} | |
//http://codex.wordpress.org/Shortcode_API | |
function function_qc_form(){ | |
$html = '<div id="qc_form"> | |
<form id="ContactForm" action="" onsubmit="qc_process(this);return false;"> | |
<p> | |
<label>Name</label> | |
<input id="name" name="name" maxlength="120" type="text" autocomplete="off"/> | |
</p> | |
<p> | |
<label>Email</label> | |
<input id="email" name="email" maxlength="120" type="text" autocomplete="off"/> | |
</p> | |
<p> | |
<label>Message</label> | |
<textarea id="message" name="message" cols="6" rows="5" autocomplete="off"></textarea> | |
</p> | |
<p class="submit"> | |
<input id="send" type="submit" value="Submit"/> | |
</p> | |
</form> | |
</div> | |
<script> | |
function qc_process(e){ | |
var data = { | |
action: "my_qc_form", | |
name: e["name"].value, | |
email:e["email"].value, | |
message:e["message"].value | |
}; | |
jQuery.post("'.admin_url("admin-ajax.php").'", data, function(response) { | |
jQuery("#qc_form").html(response); | |
}); | |
} | |
</script> | |
'; | |
return $html; | |
} | |
add_shortcode( 'qc_form', 'function_qc_form' ); | |
//http://codex.wordpress.org/AJAX_in_Plugins | |
add_action('wp_ajax_nopriv_my_qc_form', 'my_qc_form_callback'); | |
add_action('wp_ajax_my_qc_form','my_qc_form_callback'); | |
function my_qc_form_callback() { | |
global $wpdb; // this is how you get access to the database | |
$table_name = $wpdb->prefix . "quickcontact"; | |
$name = $_POST['name']; | |
$email = $_POST['email']; | |
$message = $_POST['message']; | |
$rows_affected = $wpdb->insert( $table_name, array( | |
'id' => null, | |
'time' => current_time('mysql'), | |
'name' => $name, | |
'email' => $email, | |
'message' => $message | |
)); | |
if($rows_affected==1){ | |
echo "Your message was sent."; | |
} else { | |
echo "Error, try again later."; | |
} | |
die(); // this is required to return a proper result | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment