创建我的第一个插件,允许人们创建字符。这就像在线RPG游戏一样。这里的目标是让一个wordpress用户有多个字符链接到该特定用户。以《魔兽世界》为例:你创建一个帐户,然后你可以创建几个角色,你可以删除或更新。
我在一个数据库中有两个表,我试图把它们放在一起。
第一个表wp_users有一个主键(ID),我想用它与第二个表建立主键/外键关系。
第二个表myth_char只有两个字段。外键(ID)和名为(char_name)的varchar。
因此,如果有人与5的user_id创建了一对字符,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的连接和插入查询。它将字符名插入到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文件。这可能就是它找不到任何身份的原因。然而,我试着在一个常规的主题页面上显示它,我没有运气显示它。
像这样更改表单,以便单独根据您的db文件获取charname和wpid。
您不需要在页面中复制该功能。只需调用函数get_current_user_id()
第一个文件(Change this)
删除:
因为这已经在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到数据库中。