我想在 wordpress 的常规设置 TAB 中添加一个自定义字段。 这是 wordpress 默认拥有的当前字段。
- 网站标题
- 标语
- WordPress 地址 URL ...等等
我想添加一个自定义字段,比如我想要一个图片上传字段。
为此我不得不编辑
options-general.php
,
options.php
,
general-template.php
,
我必须在数据库的 wp-options
表中插入一个条目
现在,当我使用简单的 文本输入类型
对其进行测试时,它运行良好。
但是,当我将 input type as file
设置为我的 Logo 上传时,它不起作用,这是我下面的代码。
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 环境中工作,或者我遗漏了什么,请提出建议。
最佳答案
停止机器!
你是 _doing_it_wrong()
,虽然这个函数没有检测到你在做什么:)
我们不碰核心文件,we do plugins .请将您的 WP 恢复到全新状态。
回答问题标题
Adding custom field in General setting TAB in wordpress
遵循 WordPress Answers 中的示例.关键功能是add_settings_field
, 现在它添加了一个完整的 WP 编辑器,但可以调整它以显示任何类型的字段。 nice article解释了设置 API。
<?php
/**
* Plugin Name: My Custom General Setting
*/
add_action( 'admin_init', 'wpse_57647_register_settings' );
/*
* Register settings
*/
function wpse_57647_register_settings()
{
register_setting(
'general',
'html_guidelines_message',
'esc_html' // <--- Customize this if there are multiple fields
);
add_settings_section(
'site-guide',
'Publishing Guidelines',
'__return_false',
'general'
);
add_settings_field(
'html_guidelines_message',
'Enter custom message',
'wpse_57647_print_text_editor',
'general',
'site-guide'
);
}
/*
* Print settings field content
*/
function wpse_57647_print_text_editor()
{
$the_guides = html_entity_decode( get_option( 'html_guidelines_message' ) );
echo wp_editor(
$the_guides,
'sitepublishingguidelines',
array( 'textarea_name' => 'html_guidelines_message' )
);
}
关于php - 在wordpress的常规设置选项卡中添加自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354263/