Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: table creator
- */
- /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create Table */
- function build_table() {
- global $wpdb;
- // name the table and store in var
- $address_table = $wpdb->prefix . "address_table";
- // look for table and build if !=
- if ( $wpdb->get_var ( 'SHOW TABLES LIKE ' . $address_table ) != $address_table ) {
- $sql = 'CREATE TABLE ' . $address_table . '(
- id INTEGER(10) UNSIGNED AUTO_INCREMENT,
- hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- user_agent VARCHAR(255),
- address VARCHAR(100),
- PRIMARY KEY (id) )';
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- add_option('version_num', '3.7');
- }
- }
- register_activation_hook(__FILE__, 'build_table');
- /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Update Table */
- function user_agent_finder() {
- global $wpdb;
- global $address;
- if ( isset( $_POST['s'] ) && $_POST['s'] != '') {
- $address = $_POST['s'];
- }
- // grab the table if exist or build and store in var
- $address_table = $wpdb->prefix . "address_table";
- $wpdb->insert($address_table,array('user_agent'=>$_SERVER['HTTP_USER_AGENT'], 'address' => $address));
- }
- add_action('admin_menu', 'user_agent_finder');
- /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create options page menu item on backend */
- function table_settings_menu() {
- add_options_page( 'Table Management',
- 'Table Manager', 'manage_options',
- 'table-manager',
- 'options_form_page' );
- }
- add_action( 'admin_menu', 'table_settings_menu');
- /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Form on backend options page */
- function options_form_page () {
- global $address;
- ?>
- <form method="post" action="" id="insertForm" name="insertForm">
- <input type="text" value="<?php if(isset($address)) echo $address ?>" placeholder="enter text" id="s" name="s">
- <input type="submit" value="submit">
- </form>
- <?php
- }
- /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create custom hook to connect form to insert function */
- function custom_hook() {
- add_action( 'table_config_page', 'user_agent_finder' );
- }
- add_action( 'admin_init', 'custom_hook');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement