Alchemy Options Docs
v1.0
v1.0
  • Read Me
  • Installation
  • Configuration
  • Meta Boxes
  • Samples
  • Field types
    • Text
    • Password
    • URL
    • Email
    • Tel
    • Textarea
    • Editor (WYSIWYG)
    • Datepicker
    • Image upload
    • Radio buttons
    • Select
    • Checkbox
    • Colorpicker
    • Image radio
    • Post type select
    • Datalist
    • Sections
    • Repeater
    • Button group
    • Slider
    • Taxonomy select
    • Field group
  • Functions
    • alch_options_id
    • alch_network_options_id
    • alch_get_option
    • alch_get_network_option
    • alch_delete_value
    • alch_get_post_meta
  • Javascript
    • getOption
    • getNetworkOption
    • getPostMeta
  • Filters
    • alch_options_id
    • alch_default_page_capabilities
    • alch_default_page_icon
    • alch_default_page_position
    • alch_network_options_id
    • alch_options_id() . '_args'
    • alch_network_options_id() . '_args'
    • alch_allowed_editor_html_tags
    • alch_allowed_editor_protocols
    • alch_{ $optionsPageID }_capabilities
    • alch_{ $optionsPageID }_icon
    • alch_{ $optionsPageID }_position
    • alch_value_{ $optionID }
    • alch_network_value_{ $optionID }
Powered by GitBook
On this page
  • Example usage
  • Note on passing non-existent IDs
  • Params
  1. Javascript

getOption

PreviousJavascriptNextgetNetworkOption

Last updated 6 years ago

Function getOption allows you to get the saved value of an option. Uses as a promises polyfill.

For server-side use .

Enqueue a script and . This will give you the global alchemyOptions variable.

Example usage

alchemyOptions
    .getOption('my-option-id')
    .then(value => {
        //value is a JSON string like {"success":true,"data":42}
    });

getOption returns the kew defer, so you can deal with the promise later.

const myOption = alchemyOptions.getOption('my-option-id');

//later in the code
myOption.promise.then(value => {
    // do something with the value
});

If for some reason getOption fails, you can deal with it in the fail method. It can happen if the AJAX request errored or was aborted.

alchemyOptions
    .getOption('my-option')
    .then(value => {
        // value is a JSON string like {"success":true,"data":42}
    })
    .fail(response => {
        // failed to get the value
    });

Note on passing non-existent IDs

Most of the time getOption will be successful, even if you pass non-existent ID to it. Consider the following example:

alchemyOptions
    .getOption('some-non-existent-option')
    .then(value => {
        // value is a JSON string like {"success":true,"data":""}
    })

If you pass the option ID that doesn't exist, the call will still be successful, the data property will be an empty string.

Params

Name

Type

Description

id

string

Unique ID that will be used to retrieve the value (required)

More info can be found in the .

kew
alch_get_option
add alchemy-options-client-scripts as its dependency
kew documentation