SO网友:A. Tekay
谢谢我做了家庭作业,并编写了第一个插件来完成这项任务。我添加了一个短代码来调用此函数。我可以使用你的建议从数据库中获取数据,而且效果很好。
这是我的最终代码:
function return_treatment_type(){
global $wpdb;
$tableName = $wpdb->prefix . \'random\'; // Ensures that your table name matches the prefix in config.wp
$where = [\'random_ID\' => $_POST[hidden]];
$user = new WP_User(get_current_user_id());
$user->roles[0];
$current_user = wp_get_current_user();
$url = "http://some.domain";
$myData = $wpdb->get_results("SELECT random_ID, treat_type, user_id, allocationused_date, random_owner, randomnumero_used, random_number, comment, created, updated FROM `$tableName` WHERE randomnumero_used=0 ORDER BY random_number ASC LIMIT 1");
foreach ( $myData as $myNData )
{
echo "<form action=\'#\' method=post>";
echo "<input type=datetime name=allocationused_date value=" . $myNData->allocationused_date . "><br>";
echo "<input type=bigint(60) name=user_id value=" . $current_user->user_login . "><br>";
echo "<input type=varchar(191) name=random_owner value=" . $myNData->random_owner . "><br>";
echo "<input type=longtext name=treat_type value=" . $myNData->treat_type . "><br>";
echo "<input type=tinyint(2) name=randomnumero_used value=" . $myNData->randomnumero_used . "><br>";
echo "<input type=int(10) name=random_number value=" . $myNData->random_number . "><br>";
echo "<input type=longtext name=comment value=" . $myNData->comment . "><br>";
echo "<input type=bigint(20) name=random_ID value=" . $myNData->random_ID . ">";
echo "<input type=hidden name=hidden value=" . $myNData->random_ID . ">";
echo "<input type=submit name=update value=update" . ">";
echo "</form>";
}
if (isset($_POST[\'update\'])) {
foreach ([\'allocationused_date\', \'user_id\', \'random_owner\', \'random_number\', \'treat_type\', \'randomnumero_used\', \'comment\', \'created\', \'updated\', \'random_ID\',] as $field) {
$data[$field] = $_POST[$field];
}
if( $wpdb->update($tableName, $data, $where) === FALSE)
echo "Failed";
else
wp_redirect( $url );
exit;
}
}
add_shortcode(\'random\', \'return_treatment_type\');