wpdb::set_prefix PHP Method

set_prefix() public method

Sets the table prefix for the WordPress tables.
Since: 2.5.0
public set_prefix ( string $prefix, boolean $set_table_names = true ) : string | WP_Error
$prefix string Alphanumeric name for the new prefix.
$set_table_names boolean Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not.
return string | WP_Error Old prefix or WP_Error on error
    public function set_prefix($prefix, $set_table_names = true)
    {
        if (preg_match('|[^a-z0-9_]|i', $prefix)) {
            return new WP_Error('invalid_db_prefix', 'Invalid database prefix');
        }
        $old_prefix = is_multisite() ? '' : $prefix;
        if (isset($this->base_prefix)) {
            $old_prefix = $this->base_prefix;
        }
        $this->base_prefix = $prefix;
        if ($set_table_names) {
            foreach ($this->tables('global') as $table => $prefixed_table) {
                $this->{$table} = $prefixed_table;
            }
            if (is_multisite() && empty($this->blogid)) {
                return $old_prefix;
            }
            $this->prefix = $this->get_blog_prefix();
            foreach ($this->tables('blog') as $table => $prefixed_table) {
                $this->{$table} = $prefixed_table;
            }
            foreach ($this->tables('old') as $table => $prefixed_table) {
                $this->{$table} = $prefixed_table;
            }
        }
        return $old_prefix;
    }

Usage Example

コード例 #1
0
 private static function staticInitialization()
 {
     self::$testConfig = TestConfig::createDefaultConfig();
     self::$wpAutomation = new WpAutomation(self::$testConfig->testSite, self::$testConfig->wpCliVersion);
     $yamlDir = self::$wpAutomation->getPluginsDir() . '/versionpress/.versionpress';
     $schemaFile = $yamlDir . '/schema.yml';
     $shortcodeFile = $yamlDir . '/shortcodes.yml';
     /** @var $wp_db_version */
     require self::$wpAutomation->getAbspath() . '/wp-includes/version.php';
     if (!function_exists('get_shortcode_regex')) {
         require_once self::$wpAutomation->getAbspath() . '/wp-includes/shortcodes.php';
     }
     self::$schemaInfo = new DbSchemaInfo([$schemaFile], self::$testConfig->testSite->dbTablePrefix, $wp_db_version);
     $rawTaxonomies = self::$wpAutomation->runWpCliCommand('taxonomy', 'list', ['format' => 'json', 'fields' => 'name']);
     $taxonomies = array_column(json_decode($rawTaxonomies, true), 'name');
     $dbHost = self::$testConfig->testSite->dbHost;
     $dbUser = self::$testConfig->testSite->dbUser;
     $dbPassword = self::$testConfig->testSite->dbPassword;
     $dbName = self::$testConfig->testSite->dbName;
     $dbPrefix = self::$testConfig->testSite->dbTablePrefix;
     self::$database = new \mysqli($dbHost, $dbUser, $dbPassword, $dbName);
     self::$wpdb = new \wpdb($dbUser, $dbPassword, $dbName, $dbHost);
     self::$wpdb->set_prefix($dbPrefix);
     self::$vp_database = new Database(self::$wpdb);
     $shortcodesInfo = new ShortcodesInfo([$shortcodeFile]);
     self::$vpidRepository = new VpidRepository(self::$vp_database, self::$schemaInfo);
     self::$shortcodesReplacer = new ShortcodesReplacer($shortcodesInfo, self::$vpidRepository);
     $vpdbPath = self::$wpAutomation->getVpdbDir();
     $tableSchemaRepository = new TableSchemaStorage(self::$vp_database, $vpdbPath . '/.schema');
     self::$storageFactory = new StorageFactory($vpdbPath, self::$schemaInfo, self::$vp_database, $taxonomies, null, $tableSchemaRepository);
     require self::$wpAutomation->getPluginsDir() . '/versionpress/.versionpress/hooks.php';
     self::defineGlobalVariables();
 }
All Usage Examples Of wpdb::set_prefix