Skip to content

Instantly share code, notes, and snippets.

@nczz
Last active June 23, 2020 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/fb85403cc54294d41700fb78592d0459 to your computer and use it in GitHub Desktop.
Save nczz/fb85403cc54294d41700fb78592d0459 to your computer and use it in GitHub Desktop.
[WordPress] 抓取文章第一張媒體庫圖片設定為封面特色圖片 https://www.mxp.tw/8997/
<?php
include 'wp-load.php';
set_time_limit(0);
ini_set('memory_limit', '256M');
add_action('after_setup_theme', function () {
add_filter('intermediate_image_sizes', '__return_empty_array');
add_filter('wp_get_attachment_image_src', function ($image, $attachment_id, $size, $icon) {
// get a thumbnail or intermediate image if there is one
$image = image_downsize($attachment_id, 'full');
if (!$image) {
$src = false;
if ($icon && $src = wp_mime_type_icon($attachment_id)) {
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
$src_file = $icon_dir . '/' . wp_basename($src);
@list($width, $height) = getimagesize($src_file);
}
if ($src && $width && $height) {
$image = array($src, $width, $height);
}
}
return $image;
}, 999, 4);
});
function parsing_image($post_id, $content) {
preg_match_all('/<img\s+.*?src=[\"\'](http[s]{0,1}\:\/\/?[^\"\' >]*)[\"\']?[^>]*>/i', $content, $matches);
$images = $matches[1];
mxp_import_image($post_id, $images);
return $content;
}
function mxp_import_image($post_id, $imgs) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
global $wpdb;
$upload_dir = wp_upload_dir();
$img_post_id = 0;
for ($i = 0; $i < count($imgs); ++$i) {
if ($img_post_id == 0) {
$attachment_id = $wpdb->get_results(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid LIKE %s", '%' . pathinfo(str_replace($upload_dir['baseurl'] . "/", "", $imgs[$i]), PATHINFO_FILENAME) . '%'
),
ARRAY_A
);
if (isset($attachment_id[0]['ID'])) {
$img_post_id = $attachment_id[0]['ID'];
}
}
}
//有存特色圖片就不改了
if (!has_post_thumbnail($post_id)) {
echo "沒特色圖片,開始匯入操作".PHP_EOL;
if ($img_post_id == 0) {
// 如果沒找到半張媒體庫圖片,自行指定
$img_post_id = 999; //填入媒體庫圖片的 Post ID
echo "找不到媒體庫圖片,使用替代圖片:{$img_post_id}".PHP_EOL;
}
set_post_thumbnail($post_id, $img_post_id);
}
}
global $wpdb;
$querystr = "SELECT ID,post_content FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_date < NOW() ORDER BY $wpdb->posts.post_date DESC";
$posts = $wpdb->get_results($querystr, ARRAY_A);
foreach ($posts as $key => $post) {
$post_id = $post['ID'];
$post_content = $post['post_content'];
//匯入圖片
$update_attachment_post = array(
'ID' => $post_id,
'post_content' => parsing_image($post_id, $post_content),
);
// $update_attachment_post['post_excerpt'] = wp_trim_words($update_attachment_post['post_content'], 200, '...');
$upid = wp_update_post($update_attachment_post);
echo $post_id . " => 完成匯入!" . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment