Ler*_*nzi 3 php mysql wordpress image-uploading
我想在 wordpress 的常规设置选项卡中添加一个自定义字段。这是 wordpress 默认拥有的当前字段。
我想添加一个自定义字段,例如,我想要一个图像上传字段。
对于我不得不编辑
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>
Run Code Online (Sandbox Code Playgroud)
如您所见,我已将图像字段放在博客描述字段的正下方,此表单的操作将我带到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 */
Run Code Online (Sandbox Code Playgroud)
上面我添加了简单的图片上传脚本,有些是怎么做的,文件也没有上传到目录。
核心 PHP 代码是否适用于 WP 环境或者我遗漏了什么,请建议。
你是_doing_it_wrong(),虽然这个功能没有检测到你在做什么 :)
我们不接触核心文件,我们做插件。请将您的 WP 恢复到新状态。
在wordpress的常规设置选项卡中添加自定义字段
遵循WordPress Answers 中的示例。关键功能是add_settings_field,现在它添加了一个完整的 WP 编辑器,但可以对其进行调整以显示任何类型的字段。一篇很好的文章,解释了设置 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' )
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8502 次 |
| 最近记录: |