Skip to content

Instantly share code, notes, and snippets.

@davidwolfpaw
Created November 9, 2018 20:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwolfpaw/8b7aac98f96d27d9478db4b57381d329 to your computer and use it in GitHub Desktop.
Save davidwolfpaw/8b7aac98f96d27d9478db4b57381d329 to your computer and use it in GitHub Desktop.
A sample WP-CLI script to setup a new site with some defaults of our choosing.
#!/bin/zsh
# delete the default plugins and themes that we don't need
wp plugin delete hello
wp theme delete twentyfifteen
wp theme delete twentysixteen
# install the themes and plugins that we do want
wp plugin install gutenberg --activate
wp plugin activate akismet
# delete sample post
wp post delete "$(wp post list --post_type=post --posts_per_page=1 --post_status=publish --postname="hello-world" --field=ID --format=ids)" --force
# delete sample page, and create homepage
wp post delete "$(wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids)" --force
# Add a comma separated list of pages
allpages="Home,About,Contact,Blog"
# create all of the pages
IFS=","
for page in $allpages
do
wp post create --post_type=page --post_status=publish --post_author="$(wp user get $wpuser --field=ID)" --post_title="$(echo $page | sed -e 's/^ *//' -e 's/ *$//')"
done
# set page as front page
wp option update show_on_front 'page'
# set "Home" to be the new page
wp option update page_on_front "$(wp post list --post_type=page --post_status=publish --posts_per_page=1 --pagename=home --field=ID)"
# set "Blog" to be the new blogpage
wp option update page_for_posts "$(wp post list --post_type=page --post_status=publish --posts_per_page=1 --pagename=blog --field=ID)"
# Create a navigation bar
wp menu create "Main\ Navigation"
# Add pages to navigation
IFS=" "
for pageid in $(wp post list --order="ASC" --orderby="date" --post_type=page --post_status=publish --posts_per_page=-1 --field=ID --format=ids);
do
wp menu item add-post main-navigation "$pageid"
done
# Assign navigation to primary location
wp menu location assign main-navigation primary
# Remove default widgets from sidebar
widgetids=$(wp widget list sidebar-1 --format=ids)
wp widget delete $widgetids
# Create a category called "News" and set it as default
wp term create category News
wp option update default_category "$(wp term list category --name=news --field=id)"
# update and add general options
wp option update date_format 'j F Y'
wp option update links_updated_date_format 'F j, Y g:i a'
wp option update timezone_string 'America/New_York'
wp option update permalink_structure '/%postname%/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment