do_random_quote(); template tag somwhere in you template to actually see the quote.
Author: Chris Shaffer
Version: 0.2
Author URI: http://kickthedonkey.net
*/
$dq_quote = false;
/*
this function is called during the wp_head action to setup the random quote
*/
function dq_assign_quote()
{
global $wpdb;
$num_quotes = $wpdb->get_var('SELECT MAX(quote_id) FROM ' . get_option('dq_table_nm'));
srand((double)microtime()*1000000);
$rand_quote = rand(1,$num_quotes);
$flag = false;
while( !$flag )
{
$dq_quote = fetchQuote($rand_quote);
if($dq_quote == false )
{
$flag = false;
//pull a new random number incase the other one was empty.
$rand_quote = rand(1,$num_quotes);
}
else
{
$flag = true;
}
}
return $dq_quote;
}
/*
template tag to display random quote.
*/
function do_random_quote($preTag='
', $postTag = '
' , $sepTag = ' -')
{
global $dq_quote;
if ($dq_quote === false)
{
$dq_quote = dq_assign_quote();
}
$output = $preTag;
$output .= $dq_quote['text'];
$output .= $sepTag;
if(!empty($dq_quote['src']) && isset($dq_quote['src']) )
{
if(!empty($dq_quote['src_url']) && isset($dq_quote['src_url']) )
{
$output .= '';
}
$output .= $dq_quote['src'];
if(!empty($dq_quote['src_url']) && isset($dq_quote['src_url']) )
{
$output .= '';
}
}
else
{
$output .= 'Unknown';
}
$output .= $postTag;
print $output;
}
// dq_add_pages() is the sink function for the 'admin_menu' hook
function dq_add_pages()
{
// Add a new menu under Manage:
add_management_page('Quotes', 'Quotes', 5, __FILE__, 'dq_manage_page');
//Add subment to 'write' page:
add_submenu_page('post.php', 'Quote', 'Quote', 5, __FILE__, 'dq_write_quote');
// Add a new menu under Options:
add_options_page('Quotes', 'Quotes', 8, __FILE__, 'dq_options_page');
}
/*
Handles the 'Write' menu functionality for this plugin
*/
function dq_write_quote()
{
global $wpdb, $user_ID;
$quote_id = isset($_GET['quote']) ? $_GET['quote'] : 0;
$messages[1] = __('Quote updated');
//what am I doing?
if($_GET['action'] == 'edit')
{
if($quote_id < 1)
{
//no quote provided!
$error = true;
$displayForm = false;
$feedbackMsg = 'You did not provide a Quote to edit! Perhaps you should look at the Manage Quotes page?';
}
else
{
//make sure the quote exists!
$dq_quote = fetchQuote($quote_id);
$displayForm = true;
if($dq_quote == false)
{
$error = true;
$displayForm = false;
$feedbackMsg = 'Listen, I looked all over the database, but I just couldn\'t find the quote (ID \'' . $quote_id . '\') you were wanting to'
.' edit. Maybe you should look at the Manage Quotes page, and select one from that list?';
}
//setup to display the edit form...
$form_action = 'edit';
$form_extra = "";
}
}
elseif($_POST['action'] == 'edit')
{
//they've already seen the form... actually update the quote...
$form_action = 'update';
$quote_id = isset($_POST['quote_id']) ? $_POST['quote_id'] : 0;
$quotetxt = $_POST['quotetxt'];
$quote_source = $_POST['quote_source'];
$quote_source_url = $_POST['quote_source_url'];
$displayForm = false;
if(strlen($quotetxt) < 1)
{
$error = true;
$displayForm = true;
$feedbackMsg = 'Looks like you didn\'t fill in the Quote field. Care to try again?';
$dq_quote['text'] = $quotetxt;
$dq_quote['src'] = $quote_source;
$dq_quote['src_url'] = $quote_source_url;
$form_action = 'edit';
$form_extra = "";
}
elseif($quote_id < 1)
{
$error = true;
$displayForm = false;
$feedbackMsg = 'You did not provide a Quote to edit! Perhaps you should look at the Manage Quotes page?';
}
else
{
//they've made it this far. Write the quote to the database.
$quote_source = strlen($quote_source) < 1 ? 'NULL' : '\'' . $quote_source . '\'';
$quote_source_url = strlen($quote_source_url) < 1 ? 'NULL' : '\'' . $quote_source_url . '\'';
$quotetxt = apply_filters('content_save_pre', $quotetxt);
$quotetxt = '\'' . $quotetxt . '\'';
$query = "UPDATE " . get_option('dq_table_nm') . " SET quote_text = $quotetxt, quote_src = $quote_source, quote_src_url = $quote_source_url WHERE quote_id = $quote_id";
//execute query, and see what happened:
$wpdb->query($query);
$error = false;
$displayForm = false;
$feedbackMsg = "Quote '$quote_id' successfully updated!
Manage Quotes Create New Quote";
}
}
elseif($_POST['action'] == 'new')
{
//They've filled in the form for a new quote. Add it.
$form_action = 'writenew';
$quote_source = strlen($_POST['quote_source']) < 1 ? 'NULL' : '\'' . $_POST['quote_source'] . '\'';
$quote_source_url = strlen($_POST['quote_source_url']) < 1 ? 'NULL' : '\'' . $_POST['quote_source_url'] . '\'';
$displayForm = false;
if(strlen($quotetxt) < 1)
{
$error = true;
$displayForm = true;
$feedbackMsg = 'Looks like you didn\'t fill in the Quote field. Care to try again?';
$dq_quote['src'] = $quote_source;
$dq_quote['text'] = $quotetxt;
$dq_quote['src_url'] = $quote_source_url;
$form_action = 'new';
}
$quotetxt = apply_filters('content_save_pre', $_POST['quotetxt']);
$quotetxt = '\'' . $quotetxt . '\'';
$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '" . get_option('dq_table_nm') . "'");
$quote_id = $id_result->Auto_increment;
$query = "INSERT INTO " . get_option('dq_table_nm') . " VALUES($quote_id,$user_ID,$quotetxt,$quote_source, $quote_source_url, NOW())";
//execute query, and see what happened:
$wpdb->query($query);
$error = false;
$displayForm = false;
$feedbackMsg = "Quote successfully added as ID '$quote_id'!
Quote submenu
function dq_manage_page()
{
global $wpdb, $dq_table_nm, $user_ID, $user_level;
if($_GET['action'] == 'delete')
{
if(isset($_GET['quote']))
{
//delete the puppy!
$result = $wpdb->query("DELETE FROM " . get_option('dq_table_nm') . " WHERE quote_id = " . $_GET['quote']);
//check to see what happened...
if($result)
{
$error = false;
$feedbackMsg = 'Quote \''. $_GET['quote'] . '\' successfully removed! You deserve a cookie.';
}
else
{
$error = true;
$feedbackMsg = 'Listen, I looked all over the database, but I just couldn\'t find the quote (ID \'' . $_GET['quote'] . '\') you where wanting to'
.' delete. Sorry. Its probably my fault. However, just select another quote from the list below '
. 'if you\'re still in a deleting mood.';
}
}
else
{
$error = true;
$feedbackMsg = 'You did not provide a Quote to delete! Perhaps you should look at the Manage Quotes page?';
}
}
?>
ERROR: '; } echo $feedbackMsg; ?>
users WHERE wp_quotes.quote_author = $wpdb->users.ID ORDER BY quote_id DESC";
if (isset($user_ID) && ('' != intval($user_ID))) {
$quotes = $wpdb->get_results($query);
} else {
$quotes = $wpdb->get_results($query);
}
if ($quotes)
{
?>
Quotes table.
*/
function previewText($text)
{
// Change to the number of characters you want to display
$chars = get_option('dq_previewTextLength');
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text."...";
return $text;
}
/*
Fetches a single quote, by ID.
*/
function fetchQuote( $quote_id )
{
global $wpdb;
//make sure the quote exists!
$res = $wpdb->get_results('SELECT quote_text, quote_src, quote_src_url FROM ' . get_option('dq_table_nm') . ' WHERE quote_id = ' . $quote_id);
if($res)
{
//there should only be one quote here...
foreach($res as $quote)
{
$dq_quote['text'] = $quote->quote_text;
$dq_quote['src'] = $quote->quote_src;
$dq_quote['src_url'] = $quote->quote_src_url;
}
/*print "
";
print_r($dq_quote);
print "
";
exit();*/
return $dq_quote;
}
else
{
return false;
}
}
// Synch up with the rest of Wordpress.
add_action ( 'wp_head', 'dq_assign_quote', 10 );
add_action('admin_menu', 'dq_add_pages');
//build default options, if they don't exist.
add_option('dq_table_nm', 'wp_quotes', 'Table where the quotes are stored.');
add_option('dq_previewTextLength','45', 'How much of the quote should be previewed on the Manage Quotes page.');
add_option('dq_quote_desc', 'Quotes are interesting, insiteful, of funny phrases that you may find add value to display on your blog.', 'Describes what quotes are.');
?>