Wordpress get_option() with index key from string

Wordpress get_option() with index key from string

By : user2172945
Date : October 22 2020, 08:10 AM
To fix this issue I would recommend not attempting to get it directly off the get_option() result, but rather get the value, then parse the path.
You can write your own function that accomplishes what you want. Something like so:
code :
function get_option_by_path( $path ) {
    // get all the "keys" from within the square braces
    preg_match_all( '/\[(.+?)\]/', $path, $matches );
    // get the initial "key" (eg, 'product_settings')
    $key = explode( '[', $path );
    // ensure base key is set, in case there were no square braces
    $key = ( ! empty( $key[0] ) ) ? $key[0] : $key;
    // load the option value from the DB
    $option = get_option( $key );
    if ( ! $option || ! is_array( $option ) ) {
        return FALSE;

    // if the passed-in path didn't have any square-brace keys, return the option
    if ( empty( $matches[1] ) ) {
        return $option;

    // loop over all the keys in the square braces
    foreach ( $matches[1] AS $key ) {
        // if $option is an array (still), and has the path, set it as the new $option value
        if ( is_array( $option ) && array_key_exists( $key, $option ) ) {
            $option = $option[ $key ];
        } else {
            // otherwise, can't parse properly, exit the loop

    // return the final value for the $option value
    return $option;
$get_option_srt = 'product_settings[product_01][style][color][primary]';
$value = get_option_by_path( $get_option_srt );

Share : facebook icon twitter icon
Wordpress get_option from shortcode

Wordpress get_option from shortcode

By : Kim
Date : March 29 2020, 07:55 AM
it helps some times I think it is pretty easy
first, create a function that will handle the short code
code :
 function handle_shortcode($para){
add_shortcode('getOption', 'handle_shortcode');
[getOption para="key"]
Wordpress get_option print

Wordpress get_option print

By : Luke
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Hello how can i print in php the function get_option('item') , See code below.
code :
include '_includes/items/'.get_option('item').'.php';
Wordpress get_option not working

Wordpress get_option not working

By : Edin Šehović
Date : March 29 2020, 07:55 AM
should help you out Remove esc_attr function in the below line. This function takes string as input and returns string. Since you are dealing with array this will cause error.
code :
$new_option = get_option('option2');
<input type="text" name="option2[first]" value="<?php echo esc_attr( $new_option['first'] ); ?>" />
Wordpress get_option checked do something

Wordpress get_option checked do something

By : Fonie Wong
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Hi I am wanting to test result of a checkbox in wordpress being true or false , Nevermind, it was quite easy
code :
  if (get_option('demo-checkbox') === '1') {?>
Wordpress get_option issues

Wordpress get_option issues

By : Nicholas Coles
Date : March 29 2020, 07:55 AM
it fixes the issue For theme options modified through the $wp_customize, use get_theme_mod() instead of get_option() to retrieve the value.
The difference is, that for the latter the settings are preserved per theme and thus you could possibly have different options on different themes you might switch on occasion.
code :
$wp_customize->add_setting('menu_color', array(
        'default' => '#000',
        'sanitize_callback' => 'sanitize_hex_color',
Related Posts Related Posts :
  • Running two PHP versions on the same server (STRETCH)
  • Checking if a file has been selected for upload
  • Model not updating laravel
  • How to add a description of words from text to an array, to an array key by passing words?
  • Nginx rewrite rule not not passing variables if "/" is at the end of the url
  • Modify decimal point and thousands separator without changing the number of decimals
  • Remove PHP errors from CLI and PHPMyAdmin
  • Yii2 rest api - Url manager add parameters
  • 1-query batch update array of users using PDO
  • Keep getting error: implode(): Invalid arguments passed when updating data
  • Laravel 5.4 - Correct Validation rule for a required parameter that can be zero
  • What does this code mean: $objPHPExcel->getActiveSheet()->toArray(null, true,true,true)
  • Data not being added to mysql table but echo's the data?
  • While fetch row in PHP
  • IP long string filter
  • How to view first image in list per user as its highlight Profile
  • Insert multiple rows from array
  • PHP : How to remove a duplicate array item ONCE
  • Print the input box text as simple text instead of html tag
  • How to read a string variable in php as $_GET or $_POST?
  • MySQL query fetch array not working
  • LogicException with message '... must return a relationship instance.'
  • PHP - create new array with only selected keys from an existing array
  • How to allow backgrounding a process to survive a session termination?
  • How to redirect to Home page (Default page) in zend framework 2?
  • I want to call a jquery variable in php syntax
  • Disabling the default /?add-to-cart= animation on buttons in Woocommerce
  • How to use query data for another sql query in yii2
  • Replace string enclosed in specified characters based on data type
  • Symfony FormType: how to get prototype in form type file
  • Handle reply messages properly after some operations in php
  • PHP Changed Domain, how to remove old Session Cookies
  • mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in php
  • Error: Call to a member function update() on array laravel
  • How to change the format of a float number?
  • run ajax requests by order
  • Wordpress Install 404 Error (Cannot view login in browser)
  • Calcinai xero-php pass config once
  • Fatal error calling to undefined function in PHP file
  • Laravel hasMany and belongsTo parameters
  • How do I call a Controller method in php Laravel?
  • How to replace "@" post tag with regex in PHP?
  • Laravel : when insert record in table. How can i add that Record in the list without querying the db
  • It is possible that I could user email address to get customer id from simPRO API?
  • PHP add and merge sql result
  • laravel ckeditor dark theme
  • Laravel belongsToMany not working
  • laravel 5.4 datatables not working jquery $ referenceError
  • Zend Form 2 multiselect field is empty after validation
  • PHP - Laravel 5 get data to html table from 3 SQL database tables with column DATA as headers
  • Can't use token authorization with Laravel Passport
  • splitting lines of dates and presenting in desired format
  • Laravel 5.2 Heroku Deployment Failed to install system packages
  • Laravel relationships - are there multiple *unnecessary* database calls?
  • htaccess rewrite rule for serving index.php from parent directory
  • PHP/MongoDB - $concat aggregation
  • Remove ajax.googleapis.com from my Wordpress site
  • Image saved always empty with Storage::put in Laravel
  • How to count and sort data in junction table
  • Get common number of values in a single array
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org