share đoạn code tự động lưu ảnh vào host khi copy bài từ nguồn khác.
Ưu điểm của code này.
Tự động lưu tất cả hình ảnh khi copy từ nguồn khác chỉ thông qua 1 lần bấm save. Code ổn định không bị lỗi trên các phiên bản wordpress
Nhược điểm:
Nó sẽ tự động lưu nên mình ko kiểm soát được số lượng, dung lương cũng như tên của các file ảnh đó.
Chú ý: Nên kết hợp với
Cách đổi tên ảnh upload tự động theo title trong WordPress
Để sử đụng bạn copy đoạn code sau vào file functions.php trong thư mục theme bạn đang sử dụng nhé
class Auto_Save_Images{ function __construct(){ add_filter( 'content_save_pre',array($this,'post_save_images') ); } function post_save_images( $content ){ if( ($_POST['save'] || $_POST['publish'] )){ set_time_limit(240); global $post; $post_id=$post->ID; $preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($content),$matches); if($preg){ foreach($matches[1] as $image_url){ if(empty($image_url)) continue; $pos=strpos($image_url,$_SERVER['HTTP_HOST']); if($pos===false){ $res=$this->save_images($image_url,$post_id); $replace=$res['url']; $content=str_replace($image_url,$replace,$content); } } } } remove_filter( 'content_save_pre', array( $this, 'post_save_images' ) ); return $content; } function save_images($image_url,$post_id){ $file=file_get_contents($image_url); $post = get_post($post_id); $posttitle = $post->post_title; $postname = sanitize_title($posttitle); $im_name = "$postname-$post_id.jpg"; $res=wp_upload_bits($im_name,'',$file); $this->insert_attachment($res['file'],$post_id); return $res; } function insert_attachment($file,$id){ $dirs=wp_upload_dir(); $filetype=wp_check_filetype($file); $attachment=array( 'guid'=>$dirs['baseurl'].'/'._wp_relative_upload_path($file), 'post_mime_type'=>$filetype['type'], 'post_title'=>preg_replace('/\.[^.]+$/','',basename($file)), 'post_content'=>'', 'post_status'=>'inherit' ); $attach_id=wp_insert_attachment($attachment,$file,$id); $attach_data=wp_generate_attachment_metadata($attach_id,$file); wp_update_attachment_metadata($attach_id,$attach_data); return $attach_id; } } new Auto_Save_Images();
[url=https://drugstorepp.online/]world pharmacy india[/url]
[url=https://metformindi.online/]buy glucophage online[/url]
Wow, amazing weblog format! How lengthy have you been blogging for?
you made running a blog glance easy. The overall glance of your site is fantastic,
as smartly as the content material!
[url=https://synthroidx.com/]synthroid 112 mcg tablet[/url]
[url=http://olisinopril.online/]lisinopril 5 mg daily[/url]
sandyterrace.com
Zhu Houzhao는 눈을 가늘게 떴습니다. “Old Fang, 조금 이해하는 것 같습니다.”
lacolinaecuador.com
그제야 Zhu Zaimo는 위아래로 뛰었고 겁에 질린 내시는 재빨리 그를 따라갔습니다.
qiyezp.com
그런 다음 그는 자신을 위로하기 시작했습니다. 왕자는 사람이 아니라 진짜 용입니다.
[url=http://prednisonekx.online/]prednizone for sale[/url]
sandyterrace.com
Xie Gong은 아직 보고서를 보낼 시간이 있으므로 안으로 들어오는 것이 더 적절할 것입니다.
[url=https://prednisonexg.online/]prednisone 7.5 mg daily[/url]
[url=http://valtrexmedication.com/]valtrex 100 mg[/url]
[url=http://lisinoprilgp.online/]drug lisinopril 5 mg[/url]
[url=https://isynthroid.online/]how to buy synthroid[/url]
[url=http://asynthroid.com/]synthroid 150 mcg coupon[/url]
[url=http://tadalafilstd.com/]canada generic tadalafil[/url]
game1kb.com
이 세 사람은 어느 쪽이든 왕팡보다 훨씬 더 높은 공직을 가지고 있습니다.
[url=http://lisinoprilgp.com/]zestoretic 20-25 mg[/url]
[url=https://tadalafilgf.com/]where to buy cialis online in usa[/url]
Pretty great post. I just stumbled upon your weblog and wanted to mention that I have really loved
surfing around your weblog posts. After all I’ll be subscribing
in your feed and I am hoping you write again soon!
[url=http://olisinopril.online/]zestril pill[/url]
[url=https://lisinoprildrl.online/]lisinopril 20 mg brand name[/url]
onair2tv.com
이번에는 Fang Jifan을 물러서지 않았습니다.
[url=http://azithromycinmds.online/]zithromax 250 capsules[/url]
[url=https://oazithromycin.com/]how to buy azithromycin over the counter[/url]
[url=https://synthroidx.com/]synthroid 137[/url]
usareallservice.com
素晴らしい記事です。思わず共有したくなりました。
Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across.儿童色情
[url=https://tadalafi.online/]tadalafil cost 5mg[/url]
[url=https://bestmedsx.online/]offshore pharmacy no prescription[/url]
[url=http://olisinopril.com/]lisinopril without an rx[/url]
[url=http://happyfamilymedicalstore.online/]happy family pharmacy canada[/url]
Thank you great posting about essential oil. Hello Administ . 国产线播放免费人成视频播放
[url=https://tadalafilgf.com/]cheap daily cialis online[/url]
[url=http://ismetformin.online/]metformin prescription uk[/url]
mikaspa.com
“예.” 어린 내시는 떨면서 황급히 땅에 엎드렸다.
[url=http://prednisoneiv.online/]prednisone 25mg from canada[/url]
sandyterrace.com
Fang Jifan은 그다지 반응하지 않고 “하지만 두 삼촌은? “이라고 말했습니다.
[url=http://oazithromycin.com/]azithromycin 500mg pills[/url]
qiyezp.com
“사전에 유출하지 말라고 경고해주실 수 있나요?”
usareallservice.com
この記事から得られる知識は計り知れません。非常に感謝しています。