Setting boolean and array values using wp theme mod set

I need to set a couple of Theme Mods to boolean and array values using WP CLI but can't find any way to do achieve it.

I have already tried the following for booleans, but they appear to be treated as string:

wp theme mod set my_theme_mod true
wp theme mod set my_theme_mod 1

For arrays, I don't even know where to start, so have not tried anything yet.

Any pointers please?

Topic wp-cli Wordpress

Category Web


Funny how you find the answer just after posting a question. Posting here, in case someone comes looking for it.

As per @Kero's comment above, that's correct. The command-line option will only recognise strings.

There is however an eval-file command that can be used to achieve this (and now that I have discovered it, a lot more).

Here are the steps for achieving the goal I posted in my question, however as the eval-file WP CLI command can be used to execute a PHP file, we can code anything that we deem fit.

Step 1: Create a PHP file with the required code. In my example here, I added the following:

<?php
set_theme_mod('my_boolean_mod', false);
set_theme_mod('my_array_mod', array(1, 2, 3));

The above code will use the WordPress set_theme_mod function to create/update mods named my_boolean_mod and my_array_mod.

Step 2: Execute the above file using the WP CLI eval-file command

 wp eval-file my_file.php

That's it. Hope this is useful!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.