我想在 wordpress 的常规设置选项卡中添加一个自定义字段。这是 wordpress 默认拥有的当前字段。
- 网站标题
- 标语
- Wordpress 地址 URL ...等
我想添加一个自定义字段,例如,我想要一个图片上传字段。
为此我必须编辑
options-general.php
,
options.php
,
, 我必须在我的数据库general-template.php
的表中插入一个条目wp-options
现在,当我用一个简单的方法对其进行测试时,input type as text
它运行良好。但是当我input type as file
为我的徽标上传设置它不起作用时,这是我下面的代码。
options-general.php
<tr valign="top">
<th scope="row"><label for="logo"><?php _e('Logo') ?></label></th>
<td><input name="file" type="file"/></td>
</tr>
如您所见,我已将图像字段放在博客描述字段下方,此表单的操作将我带到options.php
. 这是我的option.php
if ( is_multisite() && !is_super_admin() && 'update' != $action )
wp_die(__('Cheatin’ uh?'));
/* image upload function goes here */
if($_POST['submit']){
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("images/logo/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"images/logo/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
/* image upload function ends here */
上面我已经添加了简单的图片上传脚本,一些它是如何不起作用的,也不是文件上传到目录。
核心 PHP 代码是否在 WP 环境中工作或者我缺少某些东西,请建议。