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:
- Is there more efficient ways to validate the quotes using the wordpress api?
- What steps would I take to display the quote via a shortcode?
- 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 } ?>