Skip to content
Advertisement

WordPress Retrieve Value from Admin Options

I generated an Options Page for my WordPress using this tool: https://jeremyhixon.com/tool/wordpress-option-page-generator/

I inserted the code my functions.php, the options page is visible, however, now I’m trying to create a shortcode where I can output the value and it doesn’t seem to work.

This is my code:

function soonbox_date_func( $atts, $content = null ) {
    
    ob_start();

    $show_date = $soon_box_options['datum_location_0']; // the value from options page
    printf( $show_date );

    return ob_get_clean();
}
add_shortcode( 'soonbox_date', 'soonbox_date_func' );

But on the page it’s empty, shows nothing. Any idea what I’m doing wrong?

Advertisement

Answer

So $soon_box_options would not be defined in scope inside shortcode callback. So we’d expect that to be empty. Regardless of how you got the options value in the database, retrieving it should be standard by using the get_option function (See: https://developer.wordpress.org/reference/functions/get_option/). I don’t know if datum_location_0 is the options key stored in the database or not, but try something like:

function soonbox_date_func( $atts, $content = null ) {
    
    ob_start();

    $show_date = get_option('datum_location_0');
    printf( $show_date );

    return ob_get_clean();
}
add_shortcode( 'soonbox_date', 'soonbox_date_func' );

If datum_location_0 is not the key in the database, then replace that with the proper key.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement