时间:2021-07-01 10:21:17 帮助过:6人阅读
// Check that the nonce is valid, and the user can edit this post.
if (
isset($_POST['my_image_upload_nonce'], $_POST['post_id'])
&& wp_verify_nonce($_POST['my_image_upload_nonce'], 'my_image_upload')
&& current_user_can('edit_post', $_POST['post_id'])
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$url=WPWSL_PLUGIN_URL . '/img/' . $imageSize . '.png';
// update_option('upload_path',WP_CONTENT_DIR.'/uploads');
update_option('upload_path',WPWSL_PLUGIN_URL.'/img/');
$attachment_id = media_handle_upload('my_image_upload', $_POST['post_id']);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "There was an error uploading your file.
";
} else {
// The image was uploaded successfully!
echo "The image was uploaded successfully!
";
add_filter('img', $attachment_id);
// add_filter('img', 'custom_upload_directory');
}
} else {
echo "The security check failed, maybe show the user an error
";
// The security check failed, maybe show the user an error.
}
列表项目
回复内容:
开发了个插件中有个上传图片的功能,插件下面有个目录是img保存图片的,想问如何课一保存上传的啊图片到这个目录?
// Check that the nonce is valid, and the user can edit this post.
if (
isset($_POST['my_image_upload_nonce'], $_POST['post_id'])
&& wp_verify_nonce($_POST['my_image_upload_nonce'], 'my_image_upload')
&& current_user_can('edit_post', $_POST['post_id'])
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.
// These files need to be included as dependencies when on the front end.
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
// Let WordPress handle the upload.
// Remember, 'my_image_upload' is the name of our file input in our form above.
$url=WPWSL_PLUGIN_URL . '/img/' . $imageSize . '.png';
// update_option('upload_path',WP_CONTENT_DIR.'/uploads');
update_option('upload_path',WPWSL_PLUGIN_URL.'/img/');
$attachment_id = media_handle_upload('my_image_upload', $_POST['post_id']);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "There was an error uploading your file.
";
} else {
// The image was uploaded successfully!
echo "The image was uploaded successfully!
";
add_filter('img', $attachment_id);
// add_filter('img', 'custom_upload_directory');
}
} else {
echo "The security check failed, maybe show the user an error
";
// The security check failed, maybe show the user an error.
}
列表项目
人气教程排行
-
174次
1
php如何获取跳转前的url
-
174次
2
php格林威治时间转换成当前时间的方法
-
174次
3
为什么php不能做大型系统?
-
174次
4
range函数怎么用
-
174次
5
php中计算页面加载时间几种方法总结_PHP教程
-
174次
6
求帮助,关于paypal支付返回值修改订单状态
-
174次
7
typecho怎么配置文章内容页?
-
174次
8
PhpStorm左侧structure不显示文件的方法列表是这么回事?
-
174次
9
查看PHP的环境变量_PHP
-
174次
10
PHP Primary script unknown 解决方法总结
-
174次
11
php的命名空间与自动加载实现方法
-
174次
12
解决laravel 出现ajax请求419(unknown status)的问题
-
173次
13
php 如何删除mysql记录
-
173次
14
PHP如何替换数组中的指定元素
-
173次
15
怎么去除字符串中非汉字、非字母、非数字的字符
-
173次
16
mysql如何一次执行多条SQL语句
-
173次
17
修改header里面的Connection为close解决方法
-
173次
18
PHP基于session.upload_progress 实现文件上传进度显示功能详解
-
173次
19
php5.6.x到php7.0.x特性小结
-
172次
20
php为什么会出现504错误