2
  1. I'm not a complete novice with php or wordpress but am wanting to approach this problem the most efficient way so believe asking here will help.

  2. I've got a plugin that randomly generates a quote - I've got to the point where I need to save the quote to the mysql DB.

  3. I want to know the best way to save the data whether its using the settings api or simply saving it to an array.

  4. I'm also wanting to implement the option of deleting a quote.

  5. Using the input text field, I want to know how I can add new quotes in the textbox and pass the data to the array holding the quotes.

My code far (The quotes are stored in an array at the moment)

<?php

/*idea to develop further would be, add a text box that the user can input the quote in
this then gets added to the DB and passed to the $quotes array. From here the results get
output the same way*/
/*
Plugin Name: Random Quotes
Plugin URI: xxx 
Description: This Plugin randomly generates Quotes input by the user.
Version: 0.0.1
Author: xxx
Author URI: xxx
License: GPL2
*/

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', __FILE__, 'dw_styling_quotes_settings');
}

function dw_styling_quotes_settings() { ?>
    <div class="wrap">
        <?php screen_icon( 'plugins' ); ?>
        <h2>Quotes Page</h2>
    <table class="form-table">
        <tr valign="top">
            <th scope="row">Input Quotes in the textbox</th>
                <td><input type="textarea" name="random_quote" value="" /></td>
        </tr>
    </table>
    </div>
<?php } 

// add quotes to this list
$quotes = array(
            "one" => "The weak can never forgive. Forgiveness is the attribute of the strong",
            "two" => "Be strong when you are weak, Be brave when you are scared, Be humble when you are victorious",
            "three" => "Our success is achieved by uniting our strength, not by gathering our weaknesses",
            "four" => "One of the most common causes of failure is the habit of of quitting when one is overtaken by temporary defeat",
            "five" => "The struggles make you stronger and the changes make you wise! Happiness has its own way of taking its sweet time"
            );

// uses array_rand to randomly pick a quote
$rand_quotes = array_rand( $quotes);

// pass's the result of $array_rand to $result_quotes
$result_quote = $quotes[$rand_quotes];

// outputs the result
//echo $result_quote;

?>
2

I would store the array to the options able as a serialized value.

update_option('dw_quotes', serialize($quotes));

and retrieve with:

$quotes = get_option('dw_quotes', null);
if ($quotes !==  null) { $quotes = unserialize($quotes); }

Other things to consider:

Add handling in case the quotes option does not exist.

Also add handling to delete quotes from the option table when the plugin is deleted. See: http://codex.wordpress.org/Function_Reference/register_uninstall_hook

  • Using the input field, how would I go about adding more quotes via the user entering the quote in the textbox. – Dannyw24 Apr 3 '14 at 23:35
  • @Dannyw24 - I mocked up a quick working example for you. gist.github.com/shawnhooper/9967205 – Shawn H Apr 4 '14 at 2:56
  • 2
    You don't need to serialize() and unserialize() data when using get_options() and update_options() – RRikesh Apr 4 '14 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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