php Wordpress:在管理选项页面上传图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22874469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress: Upload Image in Admin Options Page
提问by bbbbbbbbbb
I am developing my first wordpress plugin. It only needs to allow a user to change a logo in a custom template and change a color scheme in the custom template.
我正在开发我的第一个 wordpress 插件。它只需要允许用户更改自定义模板中的徽标并更改自定义模板中的配色方案。
I have made an admin options page and now want to add a field to allow a user to upload an image. How can I upload an image to the wp-content/uploads folder. So far, I have this within a table:
我制作了一个管理选项页面,现在想要添加一个字段以允许用户上传图像。如何将图像上传到 wp-content/uploads 文件夹。到目前为止,我在一个表中有这个:
<td><input name="logo_image" type="file" id="logo_image" value="" /></td>
Is this the right approach? If so, how do I direct the file to the right folder? Doesn't wordpress have it's own way of handling file uploads?
这是正确的方法吗?如果是这样,我如何将文件定向到正确的文件夹?wordpress 没有自己处理文件上传的方式吗?
回答by ravi patel
Add this code to your global custom option function.
将此代码添加到您的全局自定义选项函数。
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
<p><strong>Header Logo Image URL:</strong><br />
<img class="header_logo" src="<?php echo get_option('header_logo'); ?>" height="100" width="100"/>
<input class="header_logo_url" type="text" name="header_logo" size="60" value="<?php echo get_option('header_logo'); ?>">
<a href="#" class="header_logo_upload">Upload</a>
</p>
<script>
jQuery(document).ready(function($) {
$('.header_logo_upload').click(function(e) {
e.preventDefault();
var custom_uploader = wp.media({
title: 'Custom Image',
button: {
text: 'Upload Image'
},
multiple: false // Set this to true to allow multiple files to be selected
})
.on('select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
$('.header_logo').attr('src', attachment.url);
$('.header_logo_url').val(attachment.url);
})
.open();
});
});
</script>
or
或者
Media uploader in theme and plugin
回答by Magesh Kumaar
You can use the inbuilt function of wordpress
您可以使用wordpress的内置功能
<?php wp_handle_upload( $file, $overrides, $time ); ?>
This will automatically move the file to the uploads folder
这将自动将文件移动到上传文件夹
Or
或者
You can write your own PHP
function.
您可以编写自己的PHP
函数。
Additional Details can be found here -> Wordpress File Upload
可以在此处找到其他详细信息 -> Wordpress 文件上传