• 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)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hey there,

    What does the error message say?

    Thread Starter wpusrpt

    (@wpusrpt)

    Thank you for responding. The error displayed in admin is

    There has been a critical error on this website. Please check your site admin email inbox for instructions.

    That is for the whole admin so I guess the WP code is doing some sanity checks. I never receive an email. If I comment out the register_activation_hook line, everything load but the database function is not loaded.

    Moderator bcworkz

    (@bcworkz)

    When you see a critical error message like that, look at your error log to see what the actual error is.

    Apparently you’ve narrowed down the problem to register_activation_hook(). I think the problem is in the first passed parameter. AFAIK it should be the main plugin file of the plugin. Since this function is normally called from the main plugin file, just __FILE__ is what is normally passed. You do not pass the file where the callback is actually declared if it’s not the main plugin file. That file needs to be included or required elsewhere.

    The purpose of the parameter is to define an action hook using the plugin’s slug name. If you pass a file in a subfolder like you did, the wrong plugin slug is created.

    Thread Starter wpusrpt

    (@wpusrpt)

    Thanks again for the help. It allowed me to see the problem. I changed the call to

    register_activation_hook( __FILE__ ,'test_install' );

    And then added this to the main file:

    function test_install() {
     return 'yes';
    }

    And when I call that function it works. So it appears that any function I want to use needs to be in the main file. There will be other functions I need to add so I thought it would make the code easier to follow if they were all grouped in an external file. That’s why I was trying to do it as I did. Is that not possible?

    Would it be better to add the functions as a class and extend the one I am using? I’m not new to coding but this is my first try at WP so I want to learn the correct way to do things.

    Moderator bcworkz

    (@bcworkz)

    AFAIK you can use functions in external files as activation callbacks as long as the file is included or required from the main file. You could even register the hook from an external file, as long as you pass the main file’s path instead of __FILE__. But it’s much less confusing to keep activation/deactivation code in the main file.

    The callback can reside in a different class, but you then need to correctly pass its object to the registration function. It’s easier to refer to a method within the same class with [$this,'callback_name'].

    You don’t really need to declare classes for your own use. You can do everything procedurally. But programming trends are towards OOP and classes. Whether resources are within one class or separate classes is up to you and how you think your code would be best organized.

    Thread Starter wpusrpt

    (@wpusrpt)

    Thanks once again. I have added it to a class and that is working fine. I would still like to get the function to work just so I understand the concept. I will work on that at some point and use your instructions as a guide.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to load function with a hook’ is closed to new replies.