1

I've been working on this plugin the past couple of nights and have been jotting down idea's of how to improve it.

My knowledge of php isn't the strongest and I'm learning as I go along most of the time.

Questions:

  1. Is there more efficient ways to validate the quotes using the wordpress api?
  2. What steps would I take to display the quote via a shortcode?
  3. What would be the best way to delete selected quotes?

idea to develop further would be to pass the quote into a shortcode and display on a website via the shortcode.*/

        /*
        Plugin Name: Random Quotes
        Plugin URI: xxx
        Description: This Plugin randomly generates Quotes input by the user.
        Version: 0.0.3
        Author: xxx
        Author URI: xxx
        License: GPL2
        */

        /*
        Function Reference 
        array_rand($array) - Randomises an array can be used with shuffle().
        ucfirst($string) - replaces the first character of the string as uppercase.
        substr($string, $start, int $length) - Returns the portion of string specified by the start and length parameters.
        strtolower($string) - converts all characters in the string to lowercase.


        */

        // call the functions
        add_action('admin_menu', 'dw_quotes_create_menu');
        add_action('init', 'validate_dw_quote');
        add_action('init', 'dw_get_random_quote'); 
        add_action('init', 'grammer_dw_quote'); 

        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() {
        //call the quote from the database
            $quote = get_option('list_of_quotes');

        //randomise the quote
            $random_quote = array_rand($quote);

        //call a value and add it to $output
            $output = $quote[$random_quote];

        //return the result
            return $output;
        }

        function grammer_dw_quote() {
        //call the quote from the previous func
            $grammer = dw_get_random_quote();

        //add a capital letter to the first letter - make the rest lowercase
            ucfirst(strtolower($grammer));

        //if the last character is not a period then append one to the string
            if(substr($grammer, -1) != '.') {
                $grammer.= '.';
            }
        //return the result
            return $grammer;
        }

        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 "<p>Success your quote was added!</p>"; 
                    } else {
                        echo "<p>Failed to add quote!</p>";
                    } 
                }   

            }
        }

        //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);
        ?>
        <h2>Current Quote displayed</h2>
        <label for="display"> <?php echo grammer_dw_quote(); ?></label>

        <?php } ?>

1 Answer 1

4

Is there more efficient ways to validate the quotes using the WordPress api?

  1. I think you should set a nonce for the form
  2. You also should check if current user has capability to save quote
  3. To get properly sanitize values form $_POST look at filter_input and/or filter_input_array

I don't see performance issues on validation, but a possible performance issue is save all the quotes in one big serialized array in a single option: if you have a lot of quotes, then you have to pull from db a very big serialized string (so fill a big amount of memory), unserialize it (serialization/unserialization on big strings can be slow) and all that just to diplay a little portion (one quote).

A performance improvement can be store quotes separately.

What steps would I take to display the quote via a shortcode?

Add shortcode is very simple (maybe this is the reason some developers over-use it), see add_shortcode doumentation.

If your grammer_dw_quote() would return the quote text instead of echo it, then the single line

add_shortcode( 'dw_quote', 'grammer_dw_quote' ); 

would be enough to add a [dw_quote] shortcode that print a random quote.

What would be the best way to delete selected quotes?

Using your way: save all the quotes together, than is hard to locate a specific quote and delete it... you should set a sort of quote id, e.g. where you have

$list_of_quotes[] = $_POST['adding_quote'];

you should probably have

$list_of_quotes[$a_unique_id] = $_POST['adding_quote'];

In this way would be simple get a specific quote and delete it, i.e. unset from the quotes array and then save it again. What this unique id should be is up to you.

However, now that I've answered you question I want give you a suggestion.

If you create a custom post type for quotes you solve pretty all your problems:

  • The menu item and the form are both created by WordPress
  • The unique id handling is done by WordPress, just like the UI for creating, listing, deleting, trashing, untrashing, scheduling...
  • The validation, the sanitization, the capability check and all the other security stuff are done by WordPress
  • retrieve a random post (for shortcode or not) or the full list of shortcode can be done using core functions

Writing less code you can have a lot of features that can be a pain to implement by yourself, and probably make no sense.

You can make an idea looking at first part of this answer. There the CPT is for 'jokes', you have 'quotes', but there is no much difference.

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.