Reputation: 1210
I am new in WordPress plugin development. This is my core PHP and HTML code
create-table.html
<form method="post" action="function.php">
<input type="text" name="table_name">
<input type="submit" name="create">
</form>
function.php
if(isset($_POST['create'])
{
$table-name=$_POST['table_name'];
//create table query here
header("location: add_table_attribute.php");
}
I want to use this same process in my WordPress plugin development. Please any one help me.
Thanks in advance.
Upvotes: 0
Views: 3397
Reputation: 1
global $wpdb;
global $table_db_version;
$table_add_one = $wpdb->prefix."store_rating";
$store_create_ddl="
CREATE TABLE IF NOT EXISTS `".$table_add_one."` (
`rid` INT(11) UNSIGNED AUTO_INCREMENT,
`ip` varchar(255) NOT NULL,
`device` varchar(255) NOT NULL,
`user_agent` varchar(255) NOT NULL,
`storeid` int(11) NOT NULL,
`rating` int(1) NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(rid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
$all_tables = array();
$mytables=$wpdb->get_results("SHOW TABLES");
foreach ($mytables as $mytable){
foreach ($mytable as $t){
$all_tables[]=$t;
}
}
$sql_one='';
if(!in_array($table_add_one,$all_tables)){
$sql_one.=$store_create_ddl;
if(!empty($sql_one)){
if(!function_exists('wp_should_upgrade_global_tables')){
require(ABSPATH . 'wp-admin/includes/upgrade.php');
}
dbDelta($store_create_ddl);
}
}
add this code in "after_setup_theme"
Upvotes: 0
Reputation: 3113
You have very many options for that.
Here is one of them as a little plugin. I've commented it for you: https://hostr.co/pRBSmTkZ2LlJ
<?php
/*
Plugin Name: stackoverlow - My own Table
Version: 1.0.0
*/
class My_Table {
/*
Add an menu entry in the Administration. Fore more information see: http://codex.wordpress.org/Administration_Menus
*/
public function __construct() {
add_action('admin_menu', array($this, 'admin_menu'));
}
public function admin_menu() {
$menu_title = 'Table: Title of your Menu'; // The title of your menu entry
$menu_slug = 'my_table'; // The slug, for example: wp-admin/admin.php?page=my_table
add_menu_page($menu_title, $menu_title, 'manage_options', $menu_slug, array($this, 'admin_page'));
}
/*
Here is the Output of your Page
*/
public function admin_page() {
global $wpdb;
// Handle here your Data
if(isset($_POST['create'])) {
$table_name = $_POST['table_name'];
// WARNING: SQL Injections - Data not prepared!
$wpdb->query("CREATE TABLE IF NOT EXISTS `" . $table_name . "`;");
}
if(!empty($wpdb->last_error)) {
printf('<pre>ERROR: %s</pre>', $wpdb->last_error);
}
if(!empty($wpdb->last_query)) {
printf('<pre>Query: %s</pre>', $wpdb->last_query);
}
?>
<form method="post" action="<?php print admin_url('admin.php?page=my_table'); ?>">
<input type="text" name="table_name" />
<input type="submit" name="create" />
</form>
<?php
}
}
new My_Table();
?>
Upvotes: 2
Reputation: 1196
Here is a sample code to create a table using a plugin
// Make custom table
function add_custom_table_on_install()
{
global $wpdb;
$table_name = $wpdb->prefix . "my_table";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id BIGINT( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 255 ) NOT NULL
)";
$wpdb->query($wpdb->prepare($sql));
}
}
// Custom table hook
register_activation_hook(__FILE__, 'add_custom_table_on_install');
Upvotes: -1