最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp cli - Setting boolean and array values using wp theme mod set

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Mar 9, 2022 at 12:29 Dash DesaiDash Desai 313 bronze badges 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 Commented 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 Commented Mar 9, 2022 at 13:12
Add a comment  | 

1 Answer 1

Reset to default 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!

发布评论

评论列表(0)

  1. 暂无评论