2

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?

2
  • I'm not sure if that's possible. Looking at the implementation it just passes the $value argument. So if it doesn't behave correctly right now, I'm not sure how you can better force it. You can create an issue in their GitHub.
    – kero
    Mar 9, 2022 at 12:50
  • Thanks, found a way to achieve it. A bit of a round about way to do it, but gets the required result and actually a lot more.
    – Dash Desai
    Mar 9, 2022 at 13:12

1 Answer 1

1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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