Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including plugin files into main plugin file properly

Tags:

php

wordpress

I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.

I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:

define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));

include(SBT_PLUGIN_URL . 'competition_table.php');

inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:

function add_table() {
  //Run code here
}
add_shortcode('competition_table', 'add_table');

When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:

Call to undefined function add_shortcode()

However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.

So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?

Thanks in advance

like image 752
ThallerThanYall Avatar asked Dec 02 '25 11:12

ThallerThanYall


1 Answers

You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".

Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:

include_once __DIR__ . '/competition_table.php';
like image 200
brasofilo Avatar answered Dec 05 '25 01:12

brasofilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!