0
  1. I'm trying to save an array to get_options in wordpress, I'm aware that I don't have to serialize the array due to get_options doing that.

  2. At the moment I don't get any success of failed message upon hitting the submit button.

  3. Am I adding the input data correctly to the database using get_options and update_options. If not how can my code be tweaked to accomplish this.

    add_action('admin_menu', 'dw_quotes_create_menu');

    function dw_quotes_create_menu() {
        //create custom top-level menu
        add_menu_page('Quotes Settings', 'Quotes Styling', 'manage_options', 'dw_quotes', 'dw_styling_quotes_settings');
    }
    
        //generating the random quote
    function dw_get_random_quote() {
    
    
    }
    
    function validate_dw_quote() {
        //validation here
        if (isset($_POST['submit'] ) ) {
    
            //checking that $_POST['adding_quote'] is set
            if( isset( $_POST['adding_quote'] ) ) {
    
                //retrieve the stored values
                if( get_option( 'list_of_quotes' ) )
                    $list_of_quotes = get_option('list_of_quotes');
                else 
                    $list_of_quotes = array();
    
                //add the new quote to the end of the array 
                $list_of_quotes[] = $_POST['adding_quote'];
    
                //store the updated quote
                if ( update_option( 'list_of_quotes', $list_of_quotes ) ) {
                    echo "Success your quote was added!";
                } else {
                    echo "Failed to add quote!";
                }
            }
        }
    }
    
        //styling the admin menu
    function dw_styling_quotes_settings() { ?>
        <h2>Quote Setting</h2>
    
        <form action="admin.php?page=dw_quotes" method="post">
        <label for="add">Enter your quote</label>
        <input type="textarea"  name="adding_quote" />
        <input type="submit" name="submit" value="add the new quote" />
    
        <?php //test the output 
        var_dump($list_of_quotes);
        ?>
    
2
  • where do you call validate_dw_quote?
    – Milo
    Apr 15, 2014 at 0:59
  • 1
    Put die; after your //store the updated quote if/else block. Do you see the message now?
    – s_ha_dum
    Apr 15, 2014 at 1:09

1 Answer 1

0

In order for it to save the data you need to hook your validate_dw_quote() onto to an action, such as admin_init or init.

add_action('admin_init', 'validate_dw_quote'); 
1
  • Hi Eric, could you tell me if the options are shared between users or user specific? I was thinking about using them on snippets. Thanks
    – RickON
    Oct 20, 2021 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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