Skip to content

Commit f61ef8c

Browse files
Merge pull request #22 from greatislander/issues/18
Introduce `wp db prefix` for getting the `$wpdb->prefix` for a site
2 parents 4c65adb + 312d161 commit f61ef8c

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This package implements the following commands:
1616
Create a new database.
1717

1818
~~~
19-
wp db create
19+
wp db create
2020
~~~
2121

2222
Runs `CREATE_DATABASE` SQL statement using `DB_HOST`, `DB_NAME`,
@@ -83,7 +83,7 @@ specified in wp-config.php.
8383
Check the current status of the database.
8484

8585
~~~
86-
wp db check
86+
wp db check
8787
~~~
8888

8989
Runs `mysqlcheck` utility with `--check` using `DB_HOST`,
@@ -105,7 +105,7 @@ for more details on the `CHECK TABLE` statement.
105105
Optimize the database.
106106

107107
~~~
108-
wp db optimize
108+
wp db optimize
109109
~~~
110110

111111
Runs `mysqlcheck` utility with `--optimize=true` using `DB_HOST`,
@@ -127,7 +127,7 @@ for more details on the `OPTIMIZE TABLE` statement.
127127
Repair the database.
128128

129129
~~~
130-
wp db repair
130+
wp db repair
131131
~~~
132132

133133
Runs `mysqlcheck` utility with `--repair=true` using `DB_HOST`,
@@ -451,6 +451,21 @@ The size defaults to a human-readable number.
451451
$ wp db size --size_format=mb
452452
6
453453

454+
### wp db prefix
455+
456+
Display the database table prefix.
457+
458+
~~~
459+
wp db prefix
460+
~~~
461+
462+
Display the database table prefix, as defined by the database handler's interpretation of the current site.
463+
464+
**EXAMPLES**
465+
466+
$ wp db prefix
467+
wp_
468+
454469
## Installing
455470

456471
This package is included with WP-CLI itself, no additional installation necessary.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"db reset",
3838
"db check",
3939
"db optimize",
40+
"db prefix",
4041
"db repair",
4142
"db cli",
4243
"db query",

features/db-prefix.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Display database table prefix
2+
3+
Scenario: Display database table prefix for a single WordPress install
4+
Given a WP install
5+
6+
When I run `wp db prefix`
7+
Then STDOUT should be:
8+
"""
9+
wp_
10+
"""

src/DB_Command.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function query( $args, $assoc_args ) {
264264
*
265265
* [--tables=<tables>]
266266
* : The comma separated list of specific tables to export. Excluding this parameter will export all tables in the database.
267-
*
267+
*
268268
* [--exclude_tables=<tables>]
269269
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
270270
*
@@ -288,7 +288,7 @@ public function query( $args, $assoc_args ) {
288288
* # Export all tables matching prefix
289289
* $ wp db export --tables=$(wp db tables --all-tables-with-prefix --format=csv)
290290
* Success: Exported to 'wordpress_dbase.sql'.
291-
*
291+
*
292292
* # Skip certain tables from the exported database
293293
* $ wp db export --exclude_tables=wp_options,wp_users
294294
* Success: Exported to 'wordpress_dbase.sql'.
@@ -650,6 +650,24 @@ public function size( $args, $assoc_args ) {
650650
}
651651
}
652652

653+
/**
654+
* Display the database table prefix.
655+
*
656+
* Display the database table prefix, as defined by the database handler's interpretation of the current site.
657+
*
658+
* ## EXAMPLES
659+
*
660+
* $ wp db prefix
661+
* wp_
662+
*/
663+
public function prefix() {
664+
@WP_CLI::get_runner()->load_wordpress();
665+
666+
global $wpdb;
667+
668+
WP_CLI::log( $wpdb->prefix );
669+
}
670+
653671
private static function get_create_query() {
654672

655673
$create_query = sprintf( 'CREATE DATABASE `%s`', DB_NAME );

0 commit comments

Comments
 (0)