Can't create table if including the file in main script

I want to create a table by including the script that creates it in the main plugin file.

I have this:

register_activation_hook( __FILE__, 'vkrwps_agt' );
function vkrwps_agt() {
   global $wpdb;
   global $jal_db_version;

   $table_name = $wpdb->prefix . "attribute_groups";

   $sql = "CREATE TABLE $table_name (
  attribute_name VARCHAR NOT NULL,
  attribute_group VARCHAR NOT NULL,
  PRIMARY KEY id (id)

   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );

} // end vkrwps_agt()

If I have that in my main plugin file, it works (it creates the table), but I put that code in another script called tables.php inside inc folder in my plugin root directory, because I want to include it within the main plugin file.

I tried the following but it does not work:

require 'inc/tables.php';

I get no error (e.g. file not found) but the table does not get created.


This is because in register_activation_hook( __FILE__, 'vkrwps_agt' ); you point to the current file (i.e. the plugin's main file, notice the __FILE__).

The function register_activation_hook() expects the path to the main plugin file inside the wp-content/plugins directory. A full path will work also.

For example, when the plugin is located in wp-content/plugin/sampleplugin/sample.php, then the name of this hook will become activate_sampleplugin/sample.php.

When the plugin consists of only one file and is (as by default) located at wp-content/plugin/sample.php the name of this hook will be activate_sample.php.

This function is a wrapper for the activate_PLUGINNAME action, and is easier to use.

Read the documentation here.

Tags: PHP / Database / Wordpress

Similar questions

External file upload including source url as file name?
Currently I am using wp_upload_bits to upload images externally to my website and its working now, however the images are maintaining our source URL as the file name. The above code returns something which looks similar to this within our WordPress site. How can I solve this, so that I can set my file name via a string?
How to add another parameter to use in the php file while including a php file using shortcode in wordpress
I use shortcode to include a PHP-file into a WordPress page. That Works fine. But I need to add a parameter, not only the filename. I can add another parameter in the shortcode, but I don't see, how I can give the other parameter (i.e. "title") to use it in the PHP-file. Actual shortcode looks like [include file=PATH.phpfile]. I would add another p...
MySQL Query: Getting COUNT of one table where the ID of table 1 has a value of X in a different table
I am working with Wordpress creating a filter and am struggling to figure out this raw query. This is what I have now: But I also need to check the wp_postmeta table to see if a row that has a matching post_id has the meta_key of wpcf-active and the meta_value of 1.
MYSQL: I want to update all rows in a table with values from another table where values from the first table are equal to the second
I am trying to update a damaged WordPress terms relation (in tables wp_terms and wp_term_relationships). Some tags were entered in Wordpress with their "term_id" number instead of their "name", so instead of creating a relation with the original correct tag, a new tag was created with this number as its name. (For instance, if t...
I'm including a html file in my plugin, but it can't find its css or js files
I'm writing this little plugin and my php file at one point includes a small html file, like this: But the problem is, that the html in turn needs some css and js files. And no matter what I try, the console shows me 404's. My folder structure looks like this: myPlugin/includes/css The .php is in myPlugin, the html in myPlugin/includes and the css ...
Including a specific Javascript Script in a template. Is my code correct?
I have a contact form that I would to place within my WordPress theme that pulls in specific Javascript AND CSS Files. To pull in the Javascript (called x.js) is this correct? I tested it and it seems to work fine, I just want to see whether I've done this correctly! Also - would I do exactly the same for a .css file? Here's the php code I placed i...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.