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.
At the moment I don't get any success of failed message upon hitting the submit button.
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); ?>
1 Answer
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');
-
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– RickONOct 20, 2021 at 15:04
validate_dw_quote
?die;
after your//store the updated quote
if/else
block. Do you see the message now?