Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a quote plugin which will save a quote to the WPDB using a form to input the quote.

I've created half this code and got the other piece from another programmer on here. I'm looking to try and understand these 3 lines better.

 if (is_null($quotes)) {
        $quotes = array();
    }

    if (isset($_GET['delete']) && is_numeric($_GET['delete'])) { // ???
        unset($quotes[$_GET['delete']]); // remove that quote from the array
        $quotes = array_values($quotes); // reorder the keys
        update_option('dw_quotes', serialize($quotes)); // store results
        echo '<p style="font-size:110%;color:green;"><strong>Quote Deleted</strong></p>';
    }

    if ($_POST && isset($_POST['random_quote']) && $_POST['random_quote'] !== '') { //???
        array_push($quotes, $_POST['random_quote']);
        update_option('dw_quotes', serialize($quotes));
        echo '<p style="font-size:110%;color:green;"><strong>Quote Added</strong></p>';
    }

However this isn't my only problem, I'm getting an error when trying to save the plugin quote.

Possible Cause:<form action="admin.php?page=wse140202.php" method="post">

Reason for possible Cause: Looking at when the error appears its got to be something to do with accessing this page.

error:"You do not have sufficient permissions to access this page."

Full plugin code:

    <?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
*/

/*

Function Reference 
is_numeric() - Finds whether a variable is a number or a numeric string
unserialize() - takes a single serialized variable and converts it back into a PHP value.
isset() - tests to see if a variable isset


*/

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

    //generating the random quote
function dw_get_random_quote() {
    $quotes = get_option('dw_quotes', null); 
    $quotes = unserialize($quotes);
    $rand_quotes = array_rand($quotes);
    $result_quote = $quotes[$rand_quotes]; // assigns $result_quote the random quote

    return $result_quote; 


}
    //styling the admin menu
function dw_styling_quotes_settings() {

    // load quotes
    $quotes = get_option('dw_quotes', null);
    $quotes = unserialize($quotes); // unserialize the data from the $quotes array

    if (is_null($quotes)) {
        $quotes = array();
    }

    if (isset($_GET['delete']) && is_numeric($_GET['delete'])) { // ???
        unset($quotes[$_GET['delete']]); // remove that quote from the array
        $quotes = array_values($quotes); // reorder the keys
        update_option('dw_quotes', serialize($quotes)); // store results
        echo '<p style="font-size:110%;color:green;"><strong>Quote Deleted</strong></p>';
    }

    if ($_POST && isset($_POST['random_quote']) && $_POST['random_quote'] !== '') { //???
        array_push($quotes, $_POST['random_quote']);
        update_option('dw_quotes', serialize($quotes));
        echo '<p style="font-size:110%;color:green;"><strong>Quote Added</strong></p>';
    }

    ?>



    <div class="wrap">
        <?php screen_icon( 'plugins' ); ?>
        <h2>Quotes Page</h2>


        <form action="admin.php?page=wse140202.php" method="post">
            Add Quote:  <input style="width:600px;" type="textarea" name="random_quote" value="" />
            <br/><input type="submit" />
        </form>
    </div>

    <h3>Current Quotes</h3>
    <ul>
        <?php
        if ($quotes !== null) { // checks if $quotes is not set to 0
            $index = 0; //counter
            if (is_array($quotes)) { // checks if $quotes is an array to prevent error
            foreach ($quotes as $quote) {
                echo '<li><strong>[ <a href="admin.php?page=wse140202.php&delete=' . $index . '">Delete</a> ]</strong>&nbsp;&nbsp;' . $quote . '</li>';
                $index++; //increment counter
                }
            }
        }?>
    </ul>

    <h3>A Random Quote</h3>

   <?php echo dw_get_random_quote();
}

?>
share|improve this question
    
This plugin works well enough for what you are trying to do; maybe you should start with this code: wordpress.org/plugins/quotes-collection –  Douglas.Sesar Apr 8 at 22:55
    
I'm wanting to develop my own as part of the learning curve. –  Dannyw24 Apr 9 at 9:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.