My problem is that my constants aren’t being globally available to files included deeper within my plugin. Let me explain.
In my main wordpress plugin file, the very first line right after the necessary /* Plugin Name: blah blah etc… */ I include my constants like so
require_once( plugin_dir_path(__FILE__) . 'constants.php' );
Next, within my __construct() function I include another file which builds the menu items in the admin section of wordpress like so
require_once( TRADESHOW_DIR . 'structure/build_menu.php' );
The build_menu.php file works as a router with mor require_once calls to pages corresponding to each menu page, and submenu. Here’s where I don’t get it. within the build_menu.php I still have access to my constants, which I use to do wordpress database queries using them, but I don’t have access to the constants within the files that are included right after. Here’s the code:
//// build_menu.php
function tradeshow_all_forms() {// callback from a wordpress add_submenu_page() function
global $wpdb;
$TS = new Bio_Tradeshow_Request_Plugin();// store plugin class in variable
if( isset( $_GET['form_id'] ) && is_integer( intval( $_GET['form_id'] ) ) ) {
$form = $wpdb->get_results(
"
SELECT *
FROM " . TRADESHOW_FORMS . "
WHERE id = '" . $_GET['form_id'] . "'
"
);
$form = $form[0];
echo TRADESHOW_SUBMITTED . ' build_menu.php<br />';// echo's out the correct value
require_once( TRADESHOW_DIR . 'structure/fill_form.php' );
}
}
Here’s the top part of the fill_form.php file
$user_id = $TS->user();
$user_id = $user_id['id'];
$form_aswers = $wpdb->get_row(// returns nothing because TRADESHOW_SUBMITTED doesn't work
"
SELECT answers
FROM " . TRADESHOW_SUBMITTED . "
WHERE user_id = '$user_id' AND form_id = '$form->id'
",
ARRAY_A
);
echo TRADSHOW_SUBMITTED;// echoes out TRADESHOW_SUBMITTED as a string and not a variable
So as you can see it’s an include within an include within the main plugin.
Additionally if anyone could explain to me why the $this variable of the plugin isn’t available within files included within the plugin. I have this at the top of my plugin
static $_o = null;
static public function init() {
if (self::$_o === null)
self::$_o = new self;
return self::$_o;
}
so that I can do $variable = new plugin_class; but I would like to understand if something gets lost when you include a file using require or require_once.
When I’m writing a plugin, its always 100% inside class, so this always works for me. I define constants in __construct() and call them in child classes with self..