过去几天晚上,我一直在开发这个插件,并记下了如何改进它的想法。
我对php的知识不是最渊博的,大部分时间我都在学习。
问题:
是否有更有效的方法使用wordpress api验证报价进一步开发的想法是将报价传递到一个短代码中,并通过该短代码显示在网站上*/
/*
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 } ?>