Skip to content

Instantly share code, notes, and snippets.

@shawnhooper
Created April 4, 2014 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnhooper/9967205 to your computer and use it in GitHub Desktop.
Save shawnhooper/9967205 to your computer and use it in GitHub Desktop.
Response to WordPress Stack Exchange : 140202
<?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_get_random_quote() {
$quotes = get_option('dw_quotes', null);
$quotes = unserialize($quotes);
$rand_quotes = array_rand( $quotes);
$result_quote = $quotes[$rand_quotes];
return $result_quote;
}
function dw_styling_quotes_settings() {
// load quotes
$quotes = get_option('dw_quotes', null);
$quotes = unserialize($quotes);
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) {
$index = 0;
foreach ($quotes as $quote) {
echo '<li><strong>[ <a href="admin.php?page=wse140202.php&delete=' . $index . '">Delete</a> ]</strong>&nbsp;&nbsp;' . $quote . '</li>';
$index++;
}
}?>
</ul>
<h3>A Random Quote</h3>
<?echo dw_get_random_quote();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment