Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following serialized data in a wp option a:2:{s:5:"Title";s:11:"your title";s:7:"Message";s:20:"Hola testing testing";}

I would like to return it to be used in a form, and show it also in the front end, I tried

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

this is the array

$notice_data = array(
    'Title' => '',
    'Message' => ''
);
add_option("notice_data", 'Default', '', 'yes');

I am creating a plugin that have a settings page with two forms that save the data in the wp option in order to be used by the jquery function inside a js file, here is part of the settings page;

<textarea name="notice_data[Title]" id="notice_data" />
<?php echo get_option('notice_data[Title]'); ?>
</textarea>
</td>
</tr>
<tr valign="top">
<th width="92" scope="row">Body Message</th>
<td width="406">
<textarea name="notice_data[Message]" id="notice_data" cols="40" rows="10" />
<?php echo get_option('notice_data[Message]'); ?>
</textarea>

I am also localizing the script to fetch the data saved in the wp option to be used inside a jquery function that shows the messages when certain conditions are met, so how do I fetch the serialized data?

share|improve this question
3  
"But it wont work" ... is no proper Error description. :) –  kaiser Jul 11 '13 at 17:46
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
but how do you show it in jQuery, I am using something like + notice_data + –  Jose David Garcia Llanos Jul 11 '13 at 21:57
    
it isnt working for me mate, it returns null, but when i dump the array, the data is in there, no idea what the problem is –  Jose David Garcia Llanos Jul 13 '13 at 14:35
    
What exactly returns null? Where do you dump the data? –  s_ha_dum Jul 13 '13 at 14:37
    
localizing returns null /* <![CDATA[ */ var lu_ban_object = {"title":null,"msg":null}; /* ]]> */ </script> –  Jose David Garcia Llanos Jul 13 '13 at 14:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.