-
-
Save sumonst21/265d431ab2049a559a07b949ca886f11 to your computer and use it in GitHub Desktop.
Simple wordpress plugin for inserting form details into database using ajax and usual form submit.
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 | |
//It’s a standard plugin header. | |
//display in plugin list. | |
/* | |
Plugin Name: Name Plugin | |
Plugin URI: http://localhost/wordpress/ | |
Description: A Name register plugin. | |
Version: 1.0 | |
Author: Swapnil Patil | |
Author URI: | |
*/ | |
include_once 'registeration_form.php'; | |
$mob_table_prefix=$wpdb->prefix; | |
define('MOB_TABLE_PREFIX', $mob_table_prefix); | |
//****If plugin is active************************************** | |
register_activation_hook(__FILE__,'name_plugin_install'); | |
register_deactivation_hook(__FILE__ ,'name_plugin_uninstall'); | |
//function will call when plugin will active | |
function name_plugin_install() | |
{ | |
global $wpdb; | |
$table_name = $wpdb->prefix."name_list"; | |
$structure = "CREATE TABLE $table_name ( | |
id INT(9) NOT NULL AUTO_INCREMENT, | |
user_name VARCHAR(20) NOT NULL, | |
PRIMARY KEY id (id) | |
);"; | |
$wpdb->query($structure); | |
} | |
//function will call when plugin will deactive | |
function name_plugin_uninstall() | |
{ | |
global $wpdb; | |
$table = MOB_TABLE_PREFIX."name_list"; | |
$structure = "drop table if exists $table"; | |
$wpdb->query($structure); | |
} | |
//create menu at admin side | |
add_action('admin_menu','name_plugin_admin_menu'); | |
function name_plugin_admin_menu() { | |
add_menu_page( | |
"Name plugin", | |
"Name plugin", | |
administrator, | |
__FILE__, | |
"name_plugin_setting" | |
); | |
} | |
?> |
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 | |
$success_msg = ''; | |
global $wpdb; | |
if(isset($_GET['txtUsername'])){ | |
require('./../../../' .'wp-blog-header.php'); | |
$table_name = $wpdb->prefix."name_list"; | |
if($_GET['txtUsername']==""){ | |
$full_text_status="show"; | |
} | |
$insert_query = " insert into ".$table_name."(user_name) values ('".$_GET['txtUsername']."') "; | |
$insertResult=$wpdb->query($insert_query); | |
if($insertResult){ | |
$success_msg = "Insert successfully."; | |
//get_permalink(); | |
header("Location:registeration_form.php"); | |
} | |
} | |
if(isset($_POST['submit'])){ | |
$table_name = $wpdb->prefix."name_list"; | |
if($_POST['username']==""){ | |
$full_text_status="show"; | |
} | |
$insert_query = " insert into ".$table_name."(user_name) values ('".$_POST['username']."') "; | |
$insertResult=$wpdb->query($insert_query); | |
if($insertResult){ | |
$success_msg = "Insert successfully."; | |
get_permalink(); | |
} | |
} | |
add_action('init','ava_test_init'); | |
function ava_test_init() { | |
wp_enqueue_script( 'ava-test-js',plugin_dir_url(__FILE__). '/jquery-1.8.3.js'); | |
} | |
function name_plugin_setting(){ | |
global $wpdb; | |
?> | |
<script type="text/javascript"> | |
function fun() | |
{ | |
var txtUsername=$('#txtUsername').val(); | |
var query='txtUsername='+txtUsername; | |
alert(query); | |
$.ajax({ | |
url:'../wp-content/plugins/name_plugin/registeration_form.php', | |
data:query, | |
dataType:'text', | |
success:function(jArray) | |
{ | |
alert('Insert Successfully'); | |
}, | |
error:function() | |
{ | |
alert('ERROR:Problem in user registeration'); | |
} | |
}); | |
} | |
</script> | |
<form name="registration_form" method="post" action="<?php get_permalink();?>"> | |
<table border="0" cellspacing="4" cellspadding="4" width="700px"> | |
<tr> | |
<td colspan="2"><b><h3>User Registeration</h3></b></td> | |
</tr> | |
<tr> | |
<td>Username :</td> | |
<td><input style="width: 300px;" type="text" id="txtUsername" name="username" /></td> | |
</tr> | |
<tr> | |
<td><input type="button" onclick="fun()" value="click me"></td> | |
</tr> | |
<tr> | |
<td></td> | |
<td align="left"><?php submit_button();?></td> | |
</tr> | |
</table> | |
</form> | |
<?php | |
} | |
?> |
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 | |
global $wpdb; | |
$table_name = $wpdb->prefix."name_list"; | |
echo $table_name; | |
$insert_query = " insert into ".$table_name."(user_name) values ('".$_GET['txtUsername']."') "; | |
$insertResult=$wpdb->query($insert_query); | |
?> |
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
Download and copy jquery library in directory. | |
In this plugin jquery-1.8.3.js is used. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
When I run this plugin and try to submit a name to the database the alert box gives me an error "ERROR:Problem in user registeration". Where am I going wrong?
In the line "wp_enqueue_script( 'ava-test-js',plugin_dir_url(FILE). '/jquery-1.8.3.js');" what is ava-test.js?
Sorry if these are newbish questions!