Advertisement
demetriusPop

insert

Dec 7th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Plugin Name: table creator
  6.  
  7. */
  8.  
  9.  
  10. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create Table */
  11. function build_table() {
  12.  
  13. global $wpdb;
  14.  
  15. // name the table and store in var
  16. $address_table = $wpdb->prefix . "address_table";
  17.  
  18. // look for table and build if !=
  19. if ( $wpdb->get_var ( 'SHOW TABLES LIKE ' . $address_table ) != $address_table ) {
  20.  
  21. $sql = 'CREATE TABLE ' . $address_table . '(
  22. id INTEGER(10) UNSIGNED AUTO_INCREMENT,
  23. hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  24. user_agent VARCHAR(255),
  25. address VARCHAR(100),
  26. PRIMARY KEY (id) )';
  27.  
  28. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  29.  
  30. dbDelta($sql);
  31.  
  32. add_option('version_num', '3.7');
  33.  
  34. }
  35.  
  36. }
  37. register_activation_hook(__FILE__, 'build_table');
  38.  
  39.  
  40. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Update Table */
  41. function user_agent_finder() {
  42.  
  43. global $wpdb;
  44. global $address;
  45. if ( isset( $_POST['s'] ) && $_POST['s'] != '') {
  46. $address = $_POST['s'];
  47. }
  48.  
  49. // grab the table if exist or build and store in var
  50. $address_table = $wpdb->prefix . "address_table";
  51.  
  52. $wpdb->insert($address_table,array('user_agent'=>$_SERVER['HTTP_USER_AGENT'], 'address' => $address));
  53.  
  54.  
  55. }
  56. add_action('admin_menu', 'user_agent_finder');
  57.  
  58.  
  59.  
  60. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create options page menu item on backend */
  61. function table_settings_menu() {
  62. add_options_page( 'Table Management',
  63. 'Table Manager', 'manage_options',
  64. 'table-manager',
  65. 'options_form_page' );
  66. }
  67. add_action( 'admin_menu', 'table_settings_menu');
  68.  
  69.  
  70.  
  71. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Form on backend options page */
  72. function options_form_page () {
  73.  
  74. global $address;
  75.  
  76. ?>
  77.  
  78. <form method="post" action="" id="insertForm" name="insertForm">
  79. <input type="text" value="<?php if(isset($address)) echo $address ?>" placeholder="enter text" id="s" name="s">
  80. <input type="submit" value="submit">
  81. </form>
  82.  
  83. <?php
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create custom hook to connect form to insert function */
  93. function custom_hook() {
  94. add_action( 'table_config_page', 'user_agent_finder' );
  95. }
  96.  
  97. add_action( 'admin_init', 'custom_hook');
  98.  
  99.  
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement