Update embedded images in imported post content
-
Does anyone know how to edit this PHP custom script?
My problem is the embedded links in my posts are still linked to original website. But I have downloaded the images to my media files.Please check my example problem and the URL from github source code
https://prnt.sc/e5dkbe
https://gist.github.com/mbissett/276dffed1de57b523c8b7ebd0967676a<?php
// Hat tip to Trey Mills for the original code inspirationadd_action(‘pmxi_saved_post’, ‘update_images_in_post_content’, 10, 3);
function update_images_in_post_content($id) {
$media = get_attached_media( ‘image’, $id );
$img_array = [];
foreach($media as $item) {
$img_array[] = wp_get_attachment_url($item->ID);
}$thepost = get_post($id);
remove_all_filters(‘the_content’);
$html = $thepost->post_content;$doc = new DOMDocument();
@$doc->loadHTML( $html );
$imageTags = $doc->getElementsByTagName( ‘img’ );$i = 0;
foreach( $imageTags as $tag ) {
$tag->setAttribute( ‘src’, $img_array[$i] );
$i++;
}
$newhtml = preg_replace(array(‘/<!DOCTYPE.*\n?<html><body><p>/’, ‘/<\/p><\/body><\/html>/’), ”, $doc->saveHTML());$args = array(
‘ID’ => $id,
‘post_content’ => $newhtml,
);wp_update_post( $args );
}
- The topic ‘Update embedded images in imported post content’ is closed to new replies.