How to fetch serialized data from wordpress options

No, this won’t work:

<?php echo get_option('notice_data[Message]'); ?>

Because get_option pulls whole option value by option_name, it doesn’t pull by pieces of the serialized array. What you are asking for is a key (option_name) called literally notice_data[Message]. Assuming you’ve saved the option as notice_data you aren’t going to get a match, and I am not even sure if brackets are supported in the option name. I’ve never tried.

What you need is…

$notice_data = get_option('notice_data');
echo $notice_data['Message'];

You said that you are “localizing the script”. so for use by jQuery/Javascript you’d do something like this (almost completely cribbed from the Codex):

$notice_data = get_option('notice_data');

wp_enqueue_script( 'some_handle' );
$translation_array = array( 
    'notice_data' => $notice_data['Message'] 
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );

Your Javascript should have access to it as object_name.notice_data.

Leave a Comment