I trying to develop a plugin .the plugin was already developed but the guy who developed left us no support has been given.When i am trying activate the following error comes up and its not getting activated.
Fatal error: Using $this when not in object context in C:\xampp\htdocs..\wp-content\plugins\sjr-product-import\functions.php on line 238
line number and the associated function is given bellow. I do not know what could be the problem.
/**
* Installation. Runs on activation.
* @since 1.0.0
* @return void
*/
function install(){
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$this->db_table} (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`row_id` varchar(22) DEFAULT NULL,
`title` varchar(255) DEFAULT '',
`description` text,
`google_product_category` text,
`product_type` text,
`link` text,
`image_link` text,
`additional_image_link` text,
`condition` text,
`availability` text,
`price` text,
`sale_price` text,
`sale_price_effective_date` text,
`brand` text,
`gtin` text,
`mpn` varchar(22) DEFAULT NULL,
`item_group_id` int(22) DEFAULT NULL,
`color` text,
`material` text,
`pattern` text,
`size` text,
`gender` text,
`age_group` text,
`adwords_labels` text,
`custom_label_0` text,
`custom_label_1` text,
`custom_label_2` text,
`custom_label_3` text,
`custom_label_4` text,
`feed_file` varchar(255) DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `row_id` (`row_id`),
KEY `title` (`title`),
KEY `item_group_id` (`item_group_id`)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
} // End install ()
Please help.thanks.
You have an error at line:
$sql = "CREATE TABLE {$this->db_table} (
Here, $this
is undefined.
Take it as a global variable.
For that, change global $wpdb;
to global $wpdb, $this;
OR, check from where you should get the value of db_table
, replace the appropriate variable/object in place of $this->db_table
.
if (!is_object($wpdb)) {
require_once ABSPATH . 'wp-admin/includes/FILE NAME CLASS OF YOUR FUNCTION get_charset_collate.php';
$wpdb = new 'CLASS_NAME OF YOUR FUNCTION get_charset_collate';
}
and try to replace {$this->db_table} with {$wpdb->db_table} Note: this is just idea to check the object variable.
You should be having something like below code in functions.php to use $this
:
class SomeClass {
var $db_table = 'my_table';
...
function install() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$this->db_table} (
But you might not have similar code to get $this->db_table
from var $db_table
So replace line 238 with
$sql = "CREATE TABLE my_table (
to test whether it works