Rất đơn giản thôi, bạn chỉ cần copy đoạn code sau vào file functions.php trong thư mục child-theme bạn đang sử dụng là ok 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();
Chúc các bạn thành công
Có thể bạn nên xem:
-
Animo – Creative & Clean Multi-Purpose WordPress T
Ngày đăng:22/03/2024Animo is a creative, clean & avant-garde WordPress theme made specifically for business, agency, creative, freelancer, blog, portfolio, photography, corporate & app or other landing page sites. Animo will give you the power to create any kind of website; the possibilities are endless! Also, how often do you get to build your site with an …
390,000đXem chi tiết
Xem thêm:
- Phần 2 : Các hàm hook trong trang sản phẩm woocommerce
- Siêu Phẩm Airdrop ECOX 2025 – Kiếm Điểm GREEN & Quy Đổi sang ECX Miễn Phí
- Tích hợp Owl Carousel Slider vào wordpress không sử dụng plugin
- Cách Backup Website Bằng All In One Migration: Hướng Dẫn Từ A Đến Z
- Tạo nút “mua ngay” bên dưới sản phẩm trong Woocommerce

Post Custom Templates
Footzone - Footwear Shoes & Sandals Shopify Theme
Harmic – Organic Food Shopify Theme
Ecolife - Multipurpose Opencart Theme
Breeze — Responsive OpenCart Theme
Remart - Multipurpose MarketPlace Opencart 3 Theme
Quixly - Multipurpose Opencart Responsive theme
Vogue - Plant Store Opencart Theme
Mazlay - Car Accessories OpenCart Theme
Corano - Jewellery OpenCart Theme
Makali - Cosmetics & Beauty OpenCart Theme
Limupa - Technology OpenCart Theme
Sneaker - Color Swatches OpenCart Theme
Aboss - Opencart 3 Theme for Sport Shop
Aero - Car Accessories Opencart 3.x Theme
Luma - Education HTML Learning Management System
Vristo – Tailwind Angular Admin Template
NobleUI - Angular 14+ Admin Template
Jobcy - Tailwind CSS Job Listing & Board Template
Veltrix - CodeIgniter Admin & Dashboard Template
Dashtrans - Bootstrap5 Admin Template
Synadmin - Bootstrap 5 Admin Template
Dark Fashion - Watch, Jewel & Blazer Shopify Them
Fendy - Clothing & Fashion Shopify 2.0 Theme
Natches - Handbags & Shopping Clothes Shopify
Soapic - Handmade Soap & Cosmetics Beauty Shopify 2.0 Theme
Deros - Furniture & Decor Responsive Shopify Theme
Topkid - Baby Clothes & Accessories Shopify Theme | Giải Pháp Đa Năng Cho Mọi Cửa Hàng Trực Tuyến
Brighty - Lighting & Interior Lights Shopify Theme
Teani - Tea Shop & Organic Store Shopify Theme
Winire - Jewelry & Accessories Shopify Theme
Juce - Fruits Organic Food Shopify Theme
Aaraa - Multipurpose Shopify OS 2.0 Theme
Amolia - Clothing & Fashion Shopify Theme
Autona - Auto Parts & Car Accessories Shopify
Minia - ASP.Net Admin Dashboard Template
Attex - PHP Admin & Dashboard Template
Webadmin - Laravel Admin & Dashboard Template
Jidox - NodeJS Admin Dashboard Template
Vristo – Tailwind Admin Template
Furen - Furniture & Decor Shopify 2.0 Theme
Energiso - Chủ đề WordPress về năng lượng mặt trời
Masco - Khởi nghiệp phần mềm Saas WordPress
Fiore - Cửa hàng hoa và tiệm hoa Elementor Pro
Vamary - Chủ đề WordPress về phẫu thuật thẩm mỹ
Bizzix - Multipurpose Business Elementor WordPress
Peace – Insurance Agency WordPress Theme
Advanced Carousel Portfolio Builder 















