创建我的第一个允许人们创建角色的插件。这应该像在线RPG游戏一样工作。这里的目标是让wordpress用户将多个字符链接到该特定用户。 以“魔兽世界”为例:您创建了一个帐户,然后您可以创建多个可以删除或更新的角色。
我在数据库中有两个表,我试图把它放在一起。
第一个表,wp_users有一个主键(ID),我想用它作为我的第二个表的主/外键关系。
第二个表myth_char只有两个字段。外键(ID)和varchar(char_name)。
因此,如果user_id为5的人创建了几个字符,Thork,Mork和Spork,我希望这些字符连接到5的user_id。为了实现这一点,我使用了两个不同的PHP文件。
第一个文件包含用户可以插入字符名称的表单。 这是我遇到麻烦的部分。我使用了内置的wordpress函数get_current_user_id,但它似乎并没有抓住用户ID。我还制作了一个调用该函数的变量。
<?php
//THIS FUNCTION GRABS CURRENT USER ID
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) )
return 0;
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
// CALL THIS FUNCTION NOW WHILE THE USER IS HERE AND LOGGED IN. THEN THE NEXT PHP PAGE WILL REQUIRE THIS FUNCTION AND INSERT IT INTO THE DB TABLE.
$wpid = get_current_user_id();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>CREATE YOUR CHARACTER</title>
<link href="stylesheets/public.css" media="all"
rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>CREATE YOUR CHARACTER HERE</h1>
</div>
<p>Please insert a name for your character.</p><br />
<form action="form_processor.php" method="post">
Character Name: <input type="text" name="charname" value"" /><br />
<br />
<input type ="submit" name ="submit" value="submit" />
</form>
</body>
</html>
第二个文件具有与DB的连接和插入查询。它将字符名称插入到数据库表myth_char_two中,并且它也支持插入Wordpress用户ID。
<?php
require_once('/home/mythlarp/public_html/wp-content/plugins/character_creation/form.php');
function redirect_to($new_location) {
header("Location: " . $new_location);
}
//1. CONNECT TO DATABASE
$dbhost = "localhost";
$dbuser = "*****";
$dbpass = "*****";
$dbname = "*****";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//2. GRAB INFORMATION FROM THE FORM IN "form.php" AND ALSO GRAB THE CURRENT WORDPRESS USER'S ID with the variable "$wpid".
// $charactername = if charactername is set to the form value then use the form value. Otherwise set it to nothing.
$charname = isset($_POST['charname']) ? $_POST['charname'] : "";
// This grabs the current Wordpress user ID from "form.php".
// 3.INSERT FORM VALUES INTO DB TABLE
$query = "INSERT INTO myth_char_two (";
$query .= "charname id";
$query .= ") VALUES (";
$query .= " '{$charname}', {$wpid}";
$query .= ")";
//$result calls the $connection and $query.
$result = mysqli_query($connection, $query);
// 4.TEST FOR QUERY ERROR.
if ($result) {
redirect_to("character_finish.php");
} else {
die("Database query failed.");
}
?>
此表单也与任何常规wordpress页面分开。现在它只是一个没有样式的常规php文件。这可能就是为什么它找不到任何ID。但是我试图在一个常规主题页面上显示它并且我没有运气显示它。
答案 0 :(得分:1)
更改这样的表单,以便根据您的db文件单独获取charname和wpid。
您无需将该功能复制到您的页面中。您只需调用函数 get_current_user_id()
第一个文件(更改此内容)
删除:强>
因为这已经在Wordpress中定义了
//THIS FUNCTION GRABS CURRENT USER ID
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) )
return 0;
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
更改表格
<form action="form_processor.php" method="post">
<input type="hidden" name="user_id" value="<?php echo $wpid; ?>" />
Character Name: <input type="text" name="charname" value"" /><br />
<br />
<input type ="submit" name ="submit" value="submit" />
</form>
第二个DB文件(在$ _POST ['charname']的isset下面添加此行)
$wpid = isset($_POST['user_id']) ? $_POST['user_id'] : "";
更改插入查询
$query = "INSERT INTO myth_char_two (";
$query .= "charname,id";
$query .= ") VALUES (";
$query .= " '{$charname}','{$wpid}'";
$query .= ")";
因此,这会将ID插入数据库。