Serialize data for wp options

I am creating my first ever plugin, its purpose is to show a notice when some conditions are met, I created the function in jQuery and initially I had to manually edit the file everytime I wanted to change the notice message, so I decided to create a settings page so that the user could edit the message from the front end.

Thanks to another member on stackxchange I was able to use wp localize to show the custom message saved in the wp options instead. however, I now find my self having to structure the message being shown such as;

Read More
Title

Message

Image

I don’t want to create many options so I thought I could try and serialize the data but I dont know how to.

In the settings page I have a single textbox for the user to enter the message.

<textarea name="msg_data" id="msg_data" cols="40" rows="10" />
<?php echo get_option('notice_data'); ?>
</textarea>

Here is the localize

$custom_notice = get_option('notice_data', 'default_value');
  wp_localize_script( 'lu_ban', 'custom_notice', $custom_notice);

My goal is to create three different inputs, one for title, one for body message and one for image path, can anyone help me out please.

THanks

Related posts

1 comment

  1. To store Data use this Code : save the serialize values

    $title    = 'Your Title Value';
    $message  = 'Your message HTML..';
    $image    = 'http://www.domain.com/yourimage.jpg';
    
    $notice_data = array('title'   => $title, 
                         'message' => $message,
                         'image'   => $image
                        );
    
    
    if(get_option('notice_data') === FALSE){
        add_option('notice_data',  $notice_data );
    }else{
        update_option('notice_data', $notice_data );
    }
    

    Now you can get the serialize values and use in your code

    $notice_data =   get_option('notice_data')  ;  
    
    echo $notice_data['title'];
    
    var_dump($notice_data);
    

Comments are closed.