multisite - WP-CLI methods for get_current_blog_id, get_blog_details or $wpdb->prefix

admin2025-01-07  4

I'm looking for some extra information about an mu site (--url) when attempting to export from a remote server to my local testing environment.

One requirement for migration is the existing blog_id and database prefix. Using those I can do a search/replace on the exported.sql for <wp-prefix>_<blog_id>_ to slot it into the correct db table on import.

Search and Replace

printf "%s" "$(sed "s/$blog_id_old/$blog_id_new/g" <<< "$(gunzip < $input)")" > $output;

Is there a better way to get the blog_id here? And can I get more information like the table prefix without running eval?

Blog ID

blog_id=$(wp eval --url=/ 'echo get_current_blog_id();');

DB Prefix

prefix=$(wp eval --url=/ 'global $wpdb; echo $wpdb->prefix;')

Thanks.

I'm looking for some extra information about an mu site (--url) when attempting to export from a remote server to my local testing environment.

One requirement for migration is the existing blog_id and database prefix. Using those I can do a search/replace on the exported.sql for <wp-prefix>_<blog_id>_ to slot it into the correct db table on import.

Search and Replace

printf "%s" "$(sed "s/$blog_id_old/$blog_id_new/g" <<< "$(gunzip < $input)")" > $output;

Is there a better way to get the blog_id here? And can I get more information like the table prefix without running eval?

Blog ID

blog_id=$(wp eval --url=http://domain.com/site-name/ 'echo get_current_blog_id();');

DB Prefix

prefix=$(wp eval --url=http://domain.com/site-name/ 'global $wpdb; echo $wpdb->prefix;')

Thanks.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Aug 16, 2016 at 16:36 jgraupjgraup 9,8543 gold badges32 silver badges69 bronze badges 5
  • From a quick look there doesn't seem to be. Both of these depend on global vars, which have no dedicated command to access (I think) like options do. – Rarst Commented Aug 16, 2016 at 18:15
  • Do you think there should be a command for site or DB information? – jgraup Commented Aug 16, 2016 at 18:19
  • That is probably better aimed at WP CLI developers. :) – Rarst Commented Aug 16, 2016 at 18:20
  • It seems like this is the only good place to talk about it now. wp-cli.org/blog/support-policy.html hopefully some of the developers are on here. I don't mind making a PR if it doesn't feel too much like a loan wolf request. – jgraup Commented Aug 16, 2016 at 18:23
  • 1 I am aware of the post, but hardly anything we can do here if it's something WP CLI just doesn't seem to do. And its developers is whose opinion on PR would matter, unlike us. :) I see no harm in bringing that on issue tracker there, especially if it is something you are willing to develop and contribute. – Rarst Commented Aug 16, 2016 at 18:26
Add a comment  | 

1 Answer 1

Reset to default 1

You can use the wp db prefix command with the --url argument to determine the prefix for that particular site. If you need to determine the urls still, maybe consider using something like the wp site list command.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252562a55.html

最新回复(0)