add_action('save_post', 'wp_all_import_save_posts');
function wp_all_import_save_posts( $post_id ){
if ( !wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wp_all_import_save_posts');
// =======================
// START
// =======================
// Указываем тип записи для которой необходимо применить правки
$post_type = "post";
$get_post = get_post($post_id);
//текущий тип поста
$get_post_type = $get_post->post_type;
if ($get_post_type == $post_type ){
$get_post_content = $get_post->post_content;
// Удаляем все изображения
// $get_post_content_NOT_img = preg_replace('/]+./','',$get_post_content);
// Удаляем блок обвертку изображений
// if (strpos($get_post_content_NOT_img, 'sigplus-gallery')){
// $get_post_content_NOT_img = preg_replace('/
.*<\/div>/','',$get_post_content_NOT_img);
// }
// if (strpos($get_post_content_NOT_img, '
')){
// $get_post_content_NOT_img = preg_replace("/
]*><\\/p[^>]*>/",'',$get_post_content_NOT_img);
// }
//====================================
// Получаем вложенные изображения через URL
// START
//====================================
$doc=new DOMDocument();
//$doc->loadHTML("
Test
");
$doc->loadHTML($get_post_content);
$xml=simplexml_import_dom($doc); // just to make xpath more simple
$images=$xml->xpath('//img');
$IMGs_URLs;
$IMGs_IDs;
foreach ($images as $img) {
// Получаем имена файлов изображений
$IMGs_NAMEs .= basename($img['src']) . ",";
// добавляем имена в строку
$IMGs_URLs .= $img['src'] . ",";
global $wpdb;
// таблица постов, там же перечисленны и медиафайлы
$table = $wpdb->prefix . 'posts';
$attachment_id = $wpdb->get_var(
$wpdb->prepare( "SELECT ID FROM $table WHERE guid RLIKE %s", $img['src'] )
);
// Returns id
$IMGs_IDs .= $attachment_id.',';
// Добавляем изображения к посту (attachment)
wp_update_post( array(
'ID' => $attachment_id,
'post_parent' => $post_id
)
);
}
// Удаляем последний символ из конца строки
$IMGs_URLs = preg_replace("/,$/", "", $IMGs_URLs );
$IMGs_NAMEs = preg_replace("/,$/", "", $IMGs_NAMEs );
$IMGs_IDs = preg_replace("/,$/", "", $IMGs_IDs );
//dd($IMGs_IDs);
//s($IMGs_URLs);
//====================================
// END
// Получаем вложенные изображения через URL
//====================================
// Создаем массив данных
// vdd($get_post_content_NOT_img);
$my_post = array();
$my_post['ID'] = $post_id;
// https://www.kvcodes.com/2015/12/get-attachments-wordpress/
// Получаем все прикрепленные изображения к записи
// START
$attachments_ID = '';
$args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null, 'post_parent' => $post_id);
$attachments = get_posts($args);
if($attachments){
foreach($attachments as $attachment){
// here your code
// echo $attachment->ID;
$attachments_ID .= $attachment->ID.",";
}
}
// Удаляем последний символ из конца строки
$attachments_ID = preg_replace("/,$/", "", $attachments_ID );
// END
// Получаем все прикрепленные изображения к записи
// $my_post['post_content'] .= '[url-gallery imgs="'.$IMGs_URLs.'"]';
// Обновляем данные в БД
if ($attachments_ID != ''){
$gallery_of_images_from_all = "[gallery ids=\"".$attachments_ID."\" columns=\"3\" link=\"file\"]";
$get_post_content_NOT_img = preg_replace('/
]+./','',$get_post_content);
$my_post['post_content'] = $get_post_content_NOT_img.$gallery_of_images_from_all;
wp_update_post( $my_post );
} else if ($IMGs_IDs != '') {
$gallery_of_images_from_all = "[gallery ids=\"".$IMGs_IDs."\" columns=\"3\" link=\"file\"]";
$get_post_content_NOT_img = preg_replace('/
]+./','',$get_post_content);
// $my_post['post_content'] = $get_post_content_NOT_img.$gallery_of_images_from_all;
$my_post['post_content'] = $get_post_content.$gallery_of_images_from_all;
wp_update_post( $my_post );
}
}
// =======================
// END
// =======================
// re-hook this function
add_action('save_post', 'wp_all_import_save_posts');
}
}