I am trying to write a WordPress plug-in. In my project I have three PHP files.

  1. My plug-in file
  2. Widget for the plug-in
  3. Data installation

I need to install data table on plug-in activation. For that sake I wrote code to create table for my plug-in in data installation file. But I am not able to create data table using this file.

My code for include data installation file and widget file in my plug-in file is

define ( 'OTHER_FILES_PATH', plugin_dir_path( __FILE__ ) );
require_once( OTHER_FILES_PATH . '/data_installation.php' );
require_once( OTHER_FILES_PATH . '/plugin_widget.php' );

My problem is when I put my data installation code in my plug-in file it’s working fine but when I put it in separate file (data installation) I am unable to create tables for my plug-in.

Please let me know if I am making any mistake.

1 Answer
1

When using plugin_dir_path() like you do:

plugin_dir_path( __FILE__ );

It does return something like:

/var/www//wordpress/wp-content/plugins/your-plugin/

So it adds a trailing slash, because as the documentation states:

It is a wrapper for trailingslashit( dirname( $file ) );.

So remove the slash before the file in your calls:

require_once plugin_dir_path( __FILE__ ) . 'file.php';

Leave a Reply

Your email address will not be published. Required fields are marked *