This is how I create a database stack and a directory for WordPress in my Ubuntu environment:

cat <<-DBSTACK | mysql -u root -p"${dbrootp}"
    CREATE USER "${domain}"@"localhost" IDENTIFIED BY "${dbuserp}";
    CREATE DATABASE "${domain}";
    GRANT ALL PRIVILEGES ON ${domain}.* TO "${domain}"@"localhost";
DBSTACK

curl -L http://wordpress.org/latest.tar.gz | tar -zx -C ${domain}/
cp ${domain}/wp-config-sample.php ${domain}/wp-config.php
sed -ir "s/username_here|database_name_here/${domain}/g ; s/password_here/${dbuserp}/g" ${domain}"/ ${domain}/wp-config.php

How would you reduce the amount of code with WP-CLI to get the same result?

up vote 2 down vote accepted

WP-CLI has a bunch of helpful commands, including some to perform basic database operations.

However, it's not its job to create new users in MySQL and grant them permissions on a newly created database. So that part of your script can't really be replaced with WP-CLI.

You're still in luck though. The wp core command will help you download, install, update and manage a WordPress install. Plus wp config helps you manage the wp-config.php file.

Instead of curl -L http://wordpress.org/latest.tar.gz | tar -zx -C ${domain}/ you can simply use wp core download to install the latest version of WordPress.

To add the database credentials to the config file, you can use wp config create --dname="${domain}" --dbuser="${domain}" --dbpass=${dbuserp}.

But we're not done yet! Why not fully install WordPress while we're at it? This can be done using wp core install:

wp core install --url=example.com --title=Example --admin_user=supervisor --admin_password=strongpassword --admin_email=info@example.com

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.