Wp eval (WP-CLI)
Naar navigatie springen
Naar zoeken springen
wp eval executes arbitrary PHP code within or outside a website. This just might be one of the main reasons why I love working in Bash: There is such easy access to other environments, like PHP and SQL.
$ wp help eval NAME wp eval DESCRIPTION Executes arbitrary PHP code. SYNOPSIS wp eval <php-code> [--skip-wordpress] Note: because code is executed within a method, global variables need to be explicitly globalized. OPTIONS <php-code> The code to execute, as a string. [--skip-wordpress] Execute code without loading WordPress. EXAMPLES # Display WordPress content directory. $ wp eval 'echo WP_CONTENT_DIR;' /var/www/wordpress/wp-content # Generate a random number. $ wp eval 'echo rand();' --skip-wordpress 479620423
Example
#!/bin/bash # # Interact with ACF fields through PHP-API # ############################################################### # Go to example.com ############################################################### # cd /var/www/example.com ############################################################### # Use the_field (ACF command) ############################################################### # php_string='the_field("hero_text",7);' wp eval "$php_string"
Serialize & unserialize
AFAIK (2023.11), WP-CLI doesn't have its own commands for serializing and deserializing of serialzed database values. However, this might be fixed by accessing the PHP functions for this, through wp eval
- Except that I don't get the data in a usefull format, I suspect.
Also, I haven't got it working yet (2023.11.27):
i_ser='a:1:{s:13:"administrator";b:1;}' php_string="unserialize ('${i_ser}')" echo "" echo "### php_string:" echo "$php_string" wp eval "$php_string"
Output:
### php_string: unserialize ('a:1:{s:13:"administrator";b:1;}') Parse error: syntax error, unexpected end of file in phar:///opt/wp/vendor/wp-cli/eval-command/src/Eval_Command.php(37) : eval()'d code on line 1 Error: There has been a critical error on this website.Learn more about troubleshooting WordPress. There has been a critical error on this website.