Wp option (WP-CLI)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 16 jun 2022 om 21:34 (Nieuwe pagina aangemaakt met '''wp option'': beheer instellingen die in tabel <code>wp_options</code> worden bijgehouden. Dit zijn object-onafhankelijke instellingen en je kunt dus niet de ''opt...')
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen

wp option: beheer instellingen die in tabel wp_options worden bijgehouden. Dit zijn object-onafhankelijke instellingen en je kunt dus niet de options opvragen die bij een specifieke pagina, post of product horen. Dit commando is een belangrijk stuk gereedschap om een site volledig via de shell te beheren. Temeer daar dit commando overweg kan met serialised data - Iets waar ik op SQL-niveau moeite mee heb.

$ wp help option

NAME

  wp option

DESCRIPTION

  Retrieves and sets site options, including plugin and WordPress settings.

SYNOPSIS

  wp option <command>

SUBCOMMANDS

  add         Adds a new option value.
  delete      Deletes an option.
  get         Gets the value for an option.
  list        Lists options and their values.
  patch       Updates a nested value in an option.
  pluck       Gets a nested value from an option.
  update      Updates an option value.

  See the [Plugin Settings API][1] and the [Theme Options][2] for more information on adding customized options.

  ---
  [1] https://developer.wordpress.org/plugins/settings/settings-api/
  [2] https://developer.wordpress.org/themes/customize-api/

EXAMPLES

    # Get site URL.
    $ wp option get siteurl
    http://example.com

    # Add option.
    $ wp option add my_option foobar
    Success: Added 'my_option' option.

    # Update option.
    $ wp option update my_option '{"foo": "bar"}' --format=json
    Success: Updated 'my_option' option.

    # Delete option.
    $ wp option delete my_option
    Success: Deleted 'my_option' option.

wp option add

Adds a new option value.

$wp help option add

NAME

  wp option add

DESCRIPTION

  Adds a new option value.

SYNOPSIS

  wp option add <key> [<value>] [--format=<format>] [--autoload=<autoload>]

  Errors if the option already exists.

OPTIONS

  <key>
    The name of the option to add.

  [<value>]
    The value of the option to add. If ommited, the value is read from STDIN.

  [--format=<format>]
    The serialization format for the value.
    ---
    default: plaintext
    options:
      - plaintext
      - json
    ---

  [--autoload=<autoload>]
    Should this option be automatically loaded.
    ---
    options:
      - 'yes'
      - 'no'
    ---

EXAMPLES

    # Create an option by reading a JSON file.
    $ wp option add my_option --format=json < config.json
    Success: Added 'my_option' option.

wp option delete

Deletes an option.

wp option get

  • Gets the value for an option - Als het een veld met serialised data betreft, krijg je dat complete veld
  • Je kunt met dit commando alleen individuele velden opvragen - Niet een deel van de boom, oftewel subvelden binnen een serialised veld
  • Wil je een deel van de boom zien (subvelden), gebruik dan wp option pluck.
$ wp help option get

wp option get

DESCRIPTION

  Gets the value for an option.

SYNOPSIS

  wp option get <key> [--format=<format>]

OPTIONS

  <key>
    Key for the option.

  [--format=<format>]
    Get value in a particular format.
    ---
    default: var_export
    options:
      - var_export
      - json
      - yaml
    ---

EXAMPLES

    # Get option.
    $ wp option get home
    http://example.com

    # Get blog description.
    $ wp option get blogdescription
    A random blog description

    # Get blog name
    $ wp option get blogname
    A random blog name

    # Get admin email.
    $ wp option get admin_email
    someone@example.com

    # Get option in JSON format.
    $ wp option get active_plugins --format=json
    {"0":"dynamically-dynamic-sidebar\/dynamically-dynamic-sidebar.php","1":"monster-widget\/monster-widget.php","2":"show-current-template\/show-current-template.php","3":"theme-check\/theme-check.php","5":"wordpress-importer\/wordpress-importer.php"}

Voorbeelden

wp option get admin_email

info@example.com

Dit werkt ook voor serialised data:

wp option get options_footer_links_1_0_link


array (
  'title' => 'Widgets GLX',
  'url' => 'http://en.s1/shop/glx/',
  'target' => '',
)

wp option list

Met wp option list krijg je een lijst van alle opties + waardes. Dat kan een flinke lijst zijn en het commando doet er al snel een minuut over voordat output verschijnt.

Voorbeelden hoe dit handzamer te maken:

  • wp option list --field=option_name
  • wp option list | grep woocommerce > tmp.txt - Daarna openen in editor.

wp option patch

$ wp help option patch


NAME

  wp option patch

DESCRIPTION

  Updates a nested value in an option.

SYNOPSIS

  wp option patch <action> <key> <key-path>... [<value>] [--format=<format>]

OPTIONS

  <action>
    Patch action to perform.
    ---
    options:
      - insert
      - update
      - delete
    ---

  <key>
    The option name.

  <key-path>...
    The name(s) of the keys within the value to locate the value to patch.

  [<value>]
    The new value. If omitted, the value is read from STDIN.

  [--format=<format>]
    The serialization format for the value.
    ---
    default: plaintext
    options:
      - plaintext
      - json
    ---

wp option patch delete

wp option patch insert

wp option patch update

Update een serialised veld:

  • Subvelden geef je aan door alle tussenliggende elementen te benoemen (spatie-gescheiden)
  • LET OP: Het huidige serialised veld/subboom wordt overschreven door dit commando. Als je bv. tussenliggende elementen vergeet te vermelden, wis je het complete datamodel rondom dat veld
  • Mogelijk een veilige manier om dit commando toe te passen: Lees eerst de huidige waarde uit met wp option pluck en update het pas daarna met dit commando.
  • Numerieke waardes mogen zowel met als zonder enkele/dubbele aanhalingstekens

Voorbeeld (1)

$ wp option get aws_settings

array (
  'seamless' => 'true',
  'cache' => 'true',
  'autoupdates' => 'true',
  'search_in' => 
  array (
    'title' => 1,
    'content' => 0,
    'sku' => 1,
    'excerpt' => 0,
    'category' => 0,
    'tag' => 0,
    'id' => 0,
  ),
  'outofstock' => 'true',
  'stopwords' => 'a, also, am, an, and, are, as, at, be, but, by, call, can, co, con, de, do, due, eg, eight, etc, even, ever, every, for, from, full, go, had, has, hasnt, have, he, hence, her, here, his, how, ie, if, in, inc, into, is, it, its, ltd, me, my, no, none, nor, not, now, of, off, on, once, one, only, onto, or, our, ours, out, over, own, part, per, put, re, see, so, some, ten, than, that, the, their, there, these, they, this, three, thru, thus, to, too, top, un, up, us, very, via, was, we, well, were, what, when, where, who, why, will',
  'synonyms' => 'buy, pay, purchase, acquire
box, housing, unit, package',
  'use_analytics' => 'true',
  'search_field_text' => 'Zoek producten...',
  'show_more_text' => 'Laat alle resultaten zien',
  'not_found_text' => 'Oeps, geen zoekresultaten.',
  'min_chars' => '1',
  'show_loader' => 'true',
  'show_clear' => 'true',
  'show_more' => 'true',
  'mobile_overlay' => 'false',
  'show_page' => 'true',
  'buttons_order' => '2',
  'desc_source' => 'excerpt',
  'mark_words' => 'true',
  'excerpt_length' => '20',
  'results_num' => '10',
  'highlight' => 'true',
  'show_image' => 'true',
  'show_excerpt' => 'false',
  'show_price' => 'true',
  'show_outofstock_price' => 'true',
  'show_cats' => 'false',
  'show_tags' => 'false',
  'show_sale' => 'true',
  'show_sku' => 'false',
  'show_stock' => 'false',
  'show_featured' => 'false',
  'search_archives' => 
  array (
    'archive_category' => 0,
    'archive_tag' => 0,
  ),
)


$ wp option pluck aws_settings search_field_text

Zoek producten...


$ wp option patch update aws_settings search_field_text "Search products"

Success: Updated 'aws_settings' option.

Voorbeeld: Numerieke waardes & aanhalingstekens

$ wp option pluck woocommerce_dhlpwc_settings price_option_home

6.95

$ wp option patch update woocommerce_dhlpwc_settings price_option_home "79.95"   # Met dubbele aanhalingstekens

Success: Updated 'woocommerce_dhlpwc_settings' option.


$ wp option pluck woocommerce_dhlpwc_settings price_option_home

79.95


$ wp option patch update woocommerce_dhlpwc_settings price_option_home "79.95"   # Zonder aanhalingstekens

Success: Value passed for 'woocommerce_dhlpwc_settings' option is unchanged.

wp option pluck

Vraag een serialised waarde op

  • Je kunt hiermee een deelboom/subveld opvragen - Dit kan niet met wp option get
  • Geef het pad van elementen op (spatie-gescheiden) om bij de betreffende subboom of het subveld te komen.
$ wp help option pluck

NAME

  wp option pluck

DESCRIPTION

  Gets a nested value from an option.

SYNOPSIS

  wp option pluck <key> <key-path>... [--format=<format>]

OPTIONS

  <key>
    The option name.

  <key-path>...
    The name(s) of the keys within the value to locate the value to pluck.

  [--format=<format>]
    The output format of the value.
    ---
    default: plaintext
    options:
      - plaintext
      - json
      - yaml
    ---

Simpel voorbeeld

wp option get options_footer_links_1_0_link

array (
  'title' => 'Widget GLX',
  'url' => 'http://en.s1/shop/glx/',
  'target' => '',
)


wp option pluck options_footer_links_1_0_link title

Widget GLX

Voorbeeld nested tree

Nu wordt het leuker: Een serialised veld met een subboom:

# Display whole serial field
########################################
#
# With "wp option get", you cannot select a part of the tree, only the whole tree
#
wp option get aws_pro_settings

array (
  1 => 
  array (
    'search_instance' => 'Search Form',
    'filter_num' => '1',
    'search_field_text' => 'Zoek producten...',
    'show_more_text' => 'Laat alle resultaten zien',
    'not_found_text' => 'Oeps, geen zoekresultaten.',
    'min_chars' => '1',
    'show_loader' => 'true',
    'show_clear' => 'true',
    'show_more' => 'true',
    'mobile_overlay' => 'false',
    'show_page' => 'true',
    'show_addon' => 'false',
    'disable_smooth' => 'false',
    'buttons_order' => '2',
    'filters' => 
    array (
      1 => 
      array (
        'filter_name' => 'All',
        'style' => 'style-inline',
        'mark_words' => 'true',
        'excerpt_length' => '20',
        'results_num' => '10',
        'outofstock' => 'true',
        'var_rules' => 'parent',
        'on_sale' => 'true',
        'product_visibility' => 
        array (
          'visible' => '1',
          'catalog' => '1',
          'search' => '1',
          'hidden' => '0',
        ),
        'search_in' => 
        array (
          'title' => 0,
          'content' => 1,
          'sku' => 1,
          'excerpt' => 0,
          'category' => '0',
          'tag' => '0',
          'id' => '0',
          'attr' => 0,
          'tax' => 0,
          'meta' => 1,
        ),
        'search_archives' => 
        array (
          'archive_category' => '0',
          'archive_tag' => '0',
          'archive_tax' => '0',
          'archive_attr' => '0',
          'archive_users' => '0',
        ),
        'highlight' => 'true',
        'show_image' => 'true',
        'show_excerpt' => 'false',
        'show_result_cats' => 'false',
        'show_result_brands' => '',
        'show_rating' => 'false',
        'show_featured' => 'false',
        'show_variations' => 'false',
        'show_price' => 'true',
        'show_outofstock_price' => 'true',
        'show_sale' => 'true',
        'show_sku' => 'false',
        'show_cart' => 'false',
        'show_stock' => 'false',
        'exclude_rel' => 'exclude',
        'adv_filters' => '',
        'search_in_attr' => 
        array (
          'attr_pa_breedte' => 0,
        ),
        'search_in_meta' => 
        array (
          'meta_post_tools' => 1,
          'meta_sku_oem' => 1,
        ),
        'product_stock_status' => 
        array (
          'in_stock' => 1,
          'out_of_stock' => 1,
          'on_backorder' => 1,
        ),
      ),
    ),
    'cache' => 'true',
    'search_logic' => 'and',
    'search_exact' => 'false',
    'desc_source' => 'content',
    'target_blank' => 'false',
    'image_source' => 'featured,gallery,content,description,default',
    'default_img' => '',
    'use_analytics' => 'false',
  ),
)

# Select part of the tree
########################################
#
wp option pluck aws_pro_settings 1

array (
  'search_instance' => 'Search Form',
  'filter_num' => '1',
  'search_field_text' => 'Zoek producten...',
  'show_more_text' => 'Laat alle resultaten zien',
  'not_found_text' => 'Oeps, geen zoekresultaten.',
  'min_chars' => '1',
  'show_loader' => 'true',
  'show_clear' => 'true',
  'show_more' => 'true',
  'mobile_overlay' => 'false',
  'show_page' => 'true',
  'show_addon' => 'false',
  'disable_smooth' => 'false',
  'buttons_order' => '2',
  'filters' => 
  array (
    1 => 
    array (
      'filter_name' => 'All',
      'style' => 'style-inline',
      'mark_words' => 'true',
      'excerpt_length' => '20',
      'results_num' => '10',
      'outofstock' => 'true',
      'var_rules' => 'parent',
      'on_sale' => 'true',
      'product_visibility' => 
      array (
        'visible' => '1',
        'catalog' => '1',
        'search' => '1',
        'hidden' => '0',
      ),
      'search_in' => 
      array (
        'title' => 0,
        'content' => 1,
        'sku' => 1,
        'excerpt' => 0,
        'category' => '0',
        'tag' => '0',
        'id' => '0',
        'attr' => 0,
        'tax' => 0,
        'meta' => 1,
      ),
      'search_archives' => 
      array (
        'archive_category' => '0',
        'archive_tag' => '0',
        'archive_tax' => '0',
        'archive_attr' => '0',
        'archive_users' => '0',
      ),
      'highlight' => 'true',
      'show_image' => 'true',
      'show_excerpt' => 'false',
      'show_result_cats' => 'false',
      'show_result_brands' => '',
      'show_rating' => 'false',
      'show_featured' => 'false',
      'show_variations' => 'false',
      'show_price' => 'true',
      'show_outofstock_price' => 'true',
      'show_sale' => 'true',
      'show_sku' => 'false',
      'show_cart' => 'false',
      'show_stock' => 'false',
      'exclude_rel' => 'exclude',
      'adv_filters' => '',
      'search_in_attr' => 
      array (
        'attr_pa_breedte' => 0,
      ),
      'search_in_meta' => 
      array (
        'meta_post_tools' => 1,
        'meta_sku_oem' => 1,
      ),
      'product_stock_status' => 
      array (
        'in_stock' => 1,
        'out_of_stock' => 1,
        'on_backorder' => 1,
      ),
    ),
  ),
  'cache' => 'true',
  'search_logic' => 'and',
  'search_exact' => 'false',
  'desc_source' => 'content',
  'target_blank' => 'false',
  'image_source' => 'featured,gallery,content,description,default',
  'default_img' => '',
  'use_analytics' => 'false',
)

# Zoom in on just 1 value...
########################################
#
wp option pluck aws_pro_settings 1 search_instance

Search Form

Bronnen

wp option update

Updates an option value. Voorbeelden:

wp option update blogname "$site_title"    # Site title
wp option update blogdescription "$slogan" # Tagline