wordpress发布文章时图片自动同步到chevereto图床教程

我们仅支持更安全可靠的USDT充值!
Hack168.com本站也采用了这个方式,图站分离的模式来加速用户访问。
第一步:获取图床程序的API KEY

进入Chevereto搭建的图床后台,转到仪表盘-设置-API,将API v1 key记录下来。

第二步:图床后端设置

1、进入Chevereto的安装目录,将app/routes/route.api.php文件拷贝到app/routes/overrides/route.api.php文件。

2、打开app/routes/overrides/route.api.php,第二行(<?php后面)添加如下几行。

header('Access-Control-Allow-Origin: https://hack168.com');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type, Accept, Authorization, X-Requested-With, Origin, Accept');

PS:记得把域名https://hack168.com改成自己的或者*

3、在app/routes/overrides/route.api.php中,找到$uploaded_id = CHV\Image::uploadToWebsite($source);更改为

$uploaded_id = CHV\Image::uploadToWebsite($source,spirit);

将spirit替换为图床中的用户名

第三步:后台编辑器添加上传按钮

将下面代码添加到functions.php中。注意:图床的域名要换成你的,图床的API v1 key要换成你的。

add_action('rest_api_init', function () {
register_rest_route('chevereto/v1', '/image/upload', array(
'methods' => 'POST',
'callback' => 'upload_to_chevereto',
));
});

function upload_to_chevereto() {
//Authentication
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
$output = array('status' => 403,
'message' => 'Unauthorized client.',
'link' => $link,
);
$result = new WP_REST_Response($output, 403);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}
$image = file_get_contents($_FILES["chevereto_img_file"]["tmp_name"]);
$upload_url = '图床的域名/api/1/upload';
$args = array(
'body' => array(
'source' => base64_encode($image),
'key' => '图床的API v1 key',
),
);

$response = wp_remote_post($upload_url, $args);
$reply = json_decode($response["body"]);

if ($reply->status_txt == 'OK' && $reply->status_code == 200) {
$status = 200;
$message = "success";
$link = $reply->image->image->url;
} else {
$status = $reply->status_code;
$message = $reply->error->message;
$link = $link;
}
$output = array(
'status' => $status,
'message' => $message,
'link' => $link,
);
$result = new WP_REST_Response($output, $status);
$result->set_headers(array('Content-Type' => 'application/json'));
return $result;
}

//添加图床上传按钮
add_action('media_buttons', 'add_my_media_button');
function add_my_media_button() {
echo '
<input id="up_to_chevereto" type="file" accept="image/*" multiple="multiple"/>
<label for="up_to_chevereto" id="up_img_label"><i class="fa fa-picture-o" aria-hidden="true"></i> 上传图片到Chevereto</label>
';
?>
<style type="text/css">
#up_to_chevereto {
display: none;
}
#up_img_label {
color: #fff;
background-color: #16a085;
border-radius: 5px;
display: inline-block;
padding: 5.2px;
}
</style>
<script type="text/javascript">
jQuery('#up_to_chevereto').change(function() {
window.wpActiveEditor = null;
for (var i = 0; i < this.files.length; i++) {
var f=this.files[i];
var formData=new FormData();
formData.append('chevereto_img_file',f);
jQuery.ajax({
async:true,
crossDomain:true,
url:'<?php echo rest_url("chevereto/v1/image/upload") ."?_wpnonce=". wp_create_nonce("wp_rest"); ?>',
type : 'POST',
processData : false,
contentType : false,
data:formData,
beforeSend: function () {
jQuery('#up_img_label').html('<i class="fa fa-spinner rotating" aria-hidden="true"></i> Uploading...');
},
success:function(res){
if (res.status == 200) {
wp.media.editor.insert('<a href="'+res.link+'"><img src="'+res.link+'" alt="'+f.name+'"></img></a>');
jQuery("#up_img_label").html('<i class="fa fa-check" aria-hidden="true"></i> 上传成功,继续上传');
}else{
console.log("code: "+res.status+"message: "+res.message);
}
},
error: function (){
jQuery("#up_img_label").html('<i class="fa fa-times" aria-hidden="true"></i> 上传失败,重新上传');
}
});
}
});
</script>
<?php
}

1583496964 8bf2da9d8cfd15a

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容