How to load function with a hook
-
I’m trying to add code for handling the database, using this page – https://codex.www.ads-software.com/Creating_Tables_with_Plugins – for reference. But that page doesn’t say where to add the function file. I’ve tried various ways but none work. The one below gives a critical error. Would someone please explain my mistake.
<?php /** Plugin Name: My Script **/ add_action( 'admin_menu' , 'My_script'); function My_script(){ $page_title = 'My_script'; $menu_title = 'My_script'; $capability = 'manage_options'; $menu_slug = 'myscript_viewer'; $function = 'myscript_functions'; add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function ); } function myscript_functions(){ require_once plugin_dir_path( __FILE__ ) . 'includes/my-functions.php'; register_activation_hook( plugin_dir_path( __FILE__ ) . 'includes/my-functions.php' , 'my_db_install' ); } //In my-functions <?php global $my_db_version; $my_db_version = '1.0'; function my_db_install() { global $wpdb; global $my_db_version; $table_name = $wpdb->prefix . 'mytable'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, name tinytext NOT NULL, text text NOT NULL, url varchar(55) DEFAULT '' NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); add_option( 'my_db_version', $my_db_version ); }
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘How to load function with a hook’ is closed to new replies.