Shortcode Code - 注意启动前的第二个功能$form_output
在代码的后三分之一中:
//Creates table, displays and validates bid form, and inserts valid bid data into table.
function bid_form_display(){
//$error_msg = \'\';
global $wpdb;
// get post id for auction from post where auction is inserted. Used to track bids db.
$postid = get_the_id();
// creates jwp_bids table in database if it doesn\'t exist
$table = $wpdb->prefix . "jwp_bids";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` mediumint(15) NOT NULL AUTO_INCREMENT,
`bid_amt` decimal(10,2) NOT NULL,
`email` varchar(255) NOT NULL,
`bid_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`post_id` mediumint(15) NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
//Gets highest bid information and assigns to variables for display in post.
$highest_bid_info = $wpdb->get_results(
"Select max(bid_amt) AS bid, bid_time, email
FROM $table
WHERE post_id = $postid", ARRAY_A );
$highest_bid_info = array_shift ( $highest_bid_info );
$high_bid = $highest_bid_info[\'bid\'];
$high_bidder = $highest_bid_info[\'email\'];
$bid_time = $highest_bid_info[\'bid_time\'];
//assign error messages to variables
$not_human = "Human verification incorrect.";
$bid_invalid = "Invalid or no bid placed. Please enter a valid bid.";
$email_invalid = "Invalid or no email entered. Please enter a valid email address.";
$insert_failed = "Oops! Sorry. Website malfunction. Please try again.";
$bid_posted = "Thank you! Your bid was posted successfully.";
//sanitize bid form post variables and assign to new variables.
$email = htmlspecialchars($_POST["email"], ENT_QUOTES, \'UTF-8\');
$bid_amount = htmlspecialchars($_POST["bid_amount"], ENT_QUOTES, \'UTF-8\');
$human = htmlspecialchars($_POST["message_human"], ENT_QUOTES, \'UTF-8\');
//process form entries, generate errors, if no errors insert bid info in db table
if(!$human == 0){
if($human != 2){
generate_error_msg("error", $not_human);
} elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
generate_error_msg("error", $email_invalid);
} elseif(empty($bid_amount)) {
generate_error_msg("error", $bid_invalid);
} else {
//insert the posted form data into the database.
$insert_db = $wpdb->insert(
$table,
array(
\'email\' => $email,
\'bid_amt\' => $bid_amount,
\'post_id\' => $postid,
)
);
if( $insert_db ){
generate_error_msg("success", $bid_posted);
} else {
generate_error_msg("error", $insert_failed);
}
}
}
//function to generate error messages for bid form
function generate_error_msg($type, $message){
global $error_msg;
if($type == "success") {
$error_msg = "<div class=\'success\'>{$message}</div>";
} else {
$error_msg = "<div class=\'error\'>{$message}</div>";
}
}
//HTML for current status of bids: Highest bid, email of bidder, time of bid.
$form_output = \'\';
$form_output .= \'<div id="currentbid">\';
$form_output .= \'The current bid is $\' . $high_bid .\'.\';
// Display current bid + $2 as minimum for next bid.
$form_output .= \'Your minimum bid is $\' . $high_bid . \'.\';
$form_output .= \'</div>\';
//HTML for bid form
$form_output .= \'<div id="bid">\';
$form_output .= \'<?php echo\' . $error_msg . \'?>\';
$form_output .= \'<form method="post">\';
$form_output .= \'<p><label for="email">Notification email (IMPORTANT!) <span>*</span><br><input type="text" name="email" value="\' . $email . \'"></label></p>\';
$form_output .= \'<p><label for="bid_amount">Enter your bid<span>*</span> <br><input type="text" name="bid_amount" value="\' . $bid_amount . \'"></label></p>\';
$form_output .= \'<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>\';
$form_output .= \'<input type="hidden" name="submitted" value="1">\';
$form_output .= \'<input type="hidden" name="post_id" value="\' . $postid . \'">\';
$form_output .= \'<p><input type="submit"></p>\';
$form_output .= \'</form>\';
$form_output .= \'</div>\';
return $form_output;
}
add_shortcode(\'bid_form\', \'bid_form_display\');
Question:function_generate_error_msg()
正在处理中。我怀疑这是因为两个函数不能驻留在同一个短代码中。
是否有其他解决方案可以在原始函数中重复复制函数代码
最合适的回答,由SO网友:prosti 整理而成
Why not like this:
//function to generate error messages for bid form
function generate_error_msg($type, $message){
global $error_msg;
if($type == "success") {
$error_msg = "<div class=\'success\'>{$message}</div>";
} else {
$error_msg = "<div class=\'error\'>{$message}</div>";
}
}
//Creates table, displays and validates bid form, and inserts valid bid data into table.
function bid_form_display(){
//$error_msg = \'\';
global $wpdb;
// get post id for auction from post where auction is inserted. Used to track bids db.
$postid = get_the_id();
// creates jwp_bids table in database if it doesn\'t exist
$table = $wpdb->prefix . "jwp_bids";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
`id` mediumint(15) NOT NULL AUTO_INCREMENT,
`bid_amt` decimal(10,2) NOT NULL,
`email` varchar(255) NOT NULL,
`bid_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`post_id` mediumint(15) NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
//Gets highest bid information and assigns to variables for display in post.
$highest_bid_info = $wpdb->get_results(
"Select max(bid_amt) AS bid, bid_time, email
FROM $table
WHERE post_id = $postid", ARRAY_A );
$highest_bid_info = array_shift ( $highest_bid_info );
$high_bid = $highest_bid_info[\'bid\'];
$high_bidder = $highest_bid_info[\'email\'];
$bid_time = $highest_bid_info[\'bid_time\'];
//assign error messages to variables
$not_human = "Human verification incorrect.";
$bid_invalid = "Invalid or no bid placed. Please enter a valid bid.";
$email_invalid = "Invalid or no email entered. Please enter a valid email address.";
$insert_failed = "Oops! Sorry. Website malfunction. Please try again.";
$bid_posted = "Thank you! Your bid was posted successfully.";
//sanitize bid form post variables and assign to new variables.
$email = htmlspecialchars($_POST["email"], ENT_QUOTES, \'UTF-8\');
$bid_amount = htmlspecialchars($_POST["bid_amount"], ENT_QUOTES, \'UTF-8\');
$human = htmlspecialchars($_POST["message_human"], ENT_QUOTES, \'UTF-8\');
//process form entries, generate errors, if no errors insert bid info in db table
if(!$human == 0){
if($human != 2){
generate_error_msg("error", $not_human);
} elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
generate_error_msg("error", $email_invalid);
} elseif(empty($bid_amount)) {
generate_error_msg("error", $bid_invalid);
} else {
//insert the posted form data into the database.
$insert_db = $wpdb->insert(
$table,
array(
\'email\' => $email,
\'bid_amt\' => $bid_amount,
\'post_id\' => $postid,
)
);
if( $insert_db ){
generate_error_msg("success", $bid_posted);
} else {
generate_error_msg("error", $insert_failed);
}
}
}
//HTML for current status of bids: Highest bid, email of bidder, time of bid.
$form_output = \'\';
$form_output .= \'<div id="currentbid">\';
$form_output .= \'The current bid is $\' . $high_bid .\'.\';
// Display current bid + $2 as minimum for next bid.
$form_output .= \'Your minimum bid is $\' . $high_bid . \'.\';
$form_output .= \'</div>\';
//HTML for bid form
$form_output .= \'<div id="bid">\';
$form_output .= \'<?php echo\' . $error_msg . \'?>\';
$form_output .= \'<form method="post">\';
$form_output .= \'<p><label for="email">Notification email (IMPORTANT!) <span>*</span><br><input type="text" name="email" value="\' . $email . \'"></label></p>\';
$form_output .= \'<p><label for="bid_amount">Enter your bid<span>*</span> <br><input type="text" name="bid_amount" value="\' . $bid_amount . \'"></label></p>\';
$form_output .= \'<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>\';
$form_output .= \'<input type="hidden" name="submitted" value="1">\';
$form_output .= \'<input type="hidden" name="post_id" value="\' . $postid . \'">\';
$form_output .= \'<p><input type="submit"></p>\';
$form_output .= \'</form>\';
$form_output .= \'</div>\';
return $form_output;
}
add_shortcode(\'bid_form\', \'bid_form_display\');