• Hi, I’m trying to define a constant in my plugin’s main file so that I can use that constant later on inside a function. Essentially, I’m trying to build a single table prefix constant. I’m defining the constant as follows in my myplugin.php file:

    define('myplugintableprefix',$table_prefix.'myplugin_');

    This works, and when I echo with echo myplugintableprefix;, I get a correct output of wp_myplugin_

    Then, I have an install.php file with a function inside which creates the table for me. The relevant lines of the function are:

    $mytablename=myplugintableprefix."item";
    $sql="create table $mytablename (itemid int not null);";
    dbDelta($sql);

    However the table that is created is simply myplugin_item rather than wp_myplugin_item.

    Having declared the constant, and seen it output as wp_myplugin_, and with my understanding of a constant being constant, I would have expected it to not change.

    Is there a way to define a constant as a fixed string using a variable? My best guess is that because I’m using a variable to define it, it’s possibly trying to re-parse the $table_prefix variable at the time that I’m trying to create my sql statement. But I also thought that this variable was reusable as it’s defined in my wp_config as my database prefix?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Defining constant with variable fixing as string’ is closed to new replies.