Wp eval (WP-CLI): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 1: Regel 1:
''wp eval'' executes arbitrary PHP code within or within a website.
+
''wp eval'' executes arbitrary PHP code within or not within a website.
  
 
<pre>
 
<pre>
Regel 36: Regel 36:
 
     $ wp eval 'echo rand();' --skip-wordpress
 
     $ wp eval 'echo rand();' --skip-wordpress
 
     479620423
 
     479620423
 +
</pre>
 +
 +
== Voorbeeld ==
 +
 +
<pre>
 +
#!/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"
 
</pre>
 
</pre>

Versie van 29 jul 2022 12:26

wp eval executes arbitrary PHP code within or not within a website.

$ 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

Voorbeeld

#!/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"