Roots Discourse

2021 best practice for deleting WordPress generic themes from Bedrock?

If you’ve tried using Trellis + Bedrock, you’ve perhaps come across this:

A screen telling you that you should remove most of those default WordPress themes.

If you’re not on Bedrock, you can probably just remove the theme directories from your WordPress source. But on Bedrock, by default it installs them - outside of source control - so you can’t do that.

So what’s the best way in Trellis to delete these? Ideally it’d be in the composer.json file as described here. However, that had no effect for me (for reasons unknown).

Eventually I successfully removed the themes using the Trellis deploy hooks. Here’s how, for the benefit of others.

In the file trellis/group_vars/all/main.yml, add these lines (I stuck them at the bottom):

deploy_after:
  - "{{ playbook_dir }}/roles/deploy/hooks/deploy-after.yml"

That tells Trellis to look in a new file we’re about to make, called trellis/roles/deploy/hooks/deploy-after.yml. Create that file now, and in it, we’ll add two tasks:

---
- name: Custom Deploy Hook Notice
  debug:
    msg: "In this custom hook, we will delete the generic WordPress themes"

- name: Hook: Deploy After - Remove unused/old/generic WP themes
  file:
    path: "{{ deploy_helper.new_release_path }}/web/wp/wp-content/themes/{{ item }}"
    state: absent
  with_items:
    - twentyten
    - twentyeleven
    - twentytwelve
    - twentythirteen
    - twentyfourteen
    - twentyfifteen
    - twentysixteen
    - twentyseventeen
    - twentynineteen
    - twentytwenty

The first task is just a debug helper and note to your future self. It uses the Ansible builtin debug module. You can delete the task if you want less verbosity.

The second task iterates over a list of old themes we’re not going to use (change this as needed if you use one), and uses the builtin Ansible file module to set those directories to the absent state which has the effect of removing the directories.

That should do it.


For extra context, I also tried this command but it didn’t seem to do anything. I’m not sure why.

 - name: Deploy After - Remove unused/old/generic WP themes
   command: |
     wp theme delete twentyten
     wp theme delete twentyeleven
     # ... etc
   args:
     chdir: "{{ deploy_helper.new_release_path }}"
1 Like

Thanks for the post! If you don’t need any of the WP core themes, you could get rid of mu-plugins/register-theme-directory.php

This kind of stuff won’t be required once https://github.com/roots/wordpress/issues/3 is resolved

2 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.