m5k1umn 发表于 2024-11-1 10:57:18

纯代码将 WordPress 文案外链照片备份为网站媒介库本地内链照片


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>的 WordPress 网站在<span style="color: black;">运用</span>了图床,<span style="color: black;">或</span>复制粘贴了别人网站内容的<span style="color: black;">状况</span>下,<span style="color: black;">文案</span>中会<span style="color: black;">显现</span><span style="color: black;">有些</span><span style="color: black;">外边</span><span style="color: black;">照片</span>(<span style="color: black;">位置</span>)。<span style="color: black;">倘若</span><span style="color: black;">运用</span>的图床停止服务(尤其是白嫖的免费图床),<span style="color: black;">或</span>别人的网站挂了,就会<span style="color: black;">引起</span><span style="color: black;">咱们</span>网站<span style="color: black;">文案</span>中的外链<span style="color: black;">照片</span><span style="color: black;">亦</span>随之失效,<span style="color: black;">没法</span><span style="color: black;">表示</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">此时,<span style="color: black;">倘若</span>提前备份了<span style="color: black;">照片</span>还好,重新上传<span style="color: black;">或</span>批量替换<span style="color: black;">位置</span>什么的就<span style="color: black;">能够</span>了,无非<span style="color: black;">便是</span>麻烦<span style="color: black;">有些</span>。但<span style="color: black;">倘若</span><span style="color: black;">照片</span><span style="color: black;">无</span>备份,那就糟糕了,重新给所有<span style="color: black;">文案</span>配图可不是一件容易的事。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>, <span style="color: black;">怎样</span>避免这个问题呢?最保险的做法自然是将<span style="color: black;">文案</span>中的<span style="color: black;">外边</span><span style="color: black;">照片</span>,下载并<span style="color: black;">保留</span>到 WordPress <span style="color: black;">媒介</span>库,并将<span style="color: black;">文案</span>中的<span style="color: black;">照片</span>链接替换为本地服务器的<span style="color: black;">照片</span>链接。实现<span style="color: black;">亦</span>很简单,无需安装插件,直接<strong style="color: blue;">将下方代码加入到主题 functions.php 文件中,之后更新发布<span style="color: black;">文案</span>就会看到效果了</strong>。</p><span style="color: black;">function</span>&nbsp;ecp_save_post(<span style="color: black;">$post_id</span>,&nbsp;<span style="color: black;">$post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;<span style="color: black;">$wpdb</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$post</span>-&gt;post_status&nbsp;==&nbsp;<span style="color: black;">publish</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$p</span>&nbsp;=&nbsp;<span style="color: black;">/&lt;img.*[\s]src=[\"|\</span>](.*)[\"|\<span style="color: black;">].*&gt;/iU</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$num</span>&nbsp;=&nbsp;preg_match_all(<span style="color: black;">$p</span>,&nbsp;<span style="color: black;">$post</span>-&gt;post_content,&nbsp;<span style="color: black;">$matches</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$num</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$wp_upload_dir</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;wp_upload_dir();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set_time_limit(0);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;curl_init();</p>curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_HEADER,&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;<span style="color: black;">true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_SSL_VERIFYPEER,&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_FOLLOWLOCATION,<span style="color: black;">true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;CURLOPT_MAXREDIRS,&nbsp;20);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;CURLOPT_CONNECTTIMEOUT,&nbsp;30);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$ecp_options</span>&nbsp;=&nbsp;<span style="color: black;">$_SERVER</span>[<span style="color: black;">HTTP_HOST</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">];</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(<span style="color: black;">$matches</span>&nbsp;as&nbsp;<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(isset(<span style="color: black;">$src</span>)&nbsp;&amp;&amp;&nbsp;strpos(<span style="color: black;">$src</span>,&nbsp;<span style="color: black;">$ecp_options</span>)&nbsp;===&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_info</span>&nbsp;=&nbsp;wp_check_filetype(basename(<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),&nbsp;null);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$file_info</span>[<span style="color: black;">ext</span>]&nbsp;==&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date_default_timezone_set(<span style="color: black;">PRC</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_name</span>&nbsp;=&nbsp;date(<span style="color: black;">YmdHis-</span>)&nbsp;.&nbsp;dechex(mt_rand(100000,&nbsp;999999))&nbsp;.&nbsp;<span style="color: black;">.tmp</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: black;">else</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_name</span>=&nbsp;dechex(mt_rand(100000,&nbsp;999999))&nbsp;.<span style="color: black;">-</span>&nbsp;.&nbsp;basename(<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_URL,&nbsp;<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_path</span>&nbsp;=&nbsp;<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">path</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;<span style="color: black;">$file_name</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$img</span>&nbsp;=&nbsp;fopen(<span style="color: black;">$file_path</span>,&nbsp;<span style="color: black;">wb</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_FILE,<span style="color: black;">$img</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$img_data</span>&nbsp;=&nbsp;curl_exec(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose(<span style="color: black;">$img</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(file_exists(<span style="color: black;">$file_path</span>)&nbsp;&amp;&amp;&nbsp;filesize(<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;&gt;&nbsp;0)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$t</span>&nbsp;=&nbsp;curl_getinfo(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;CURLINFO_CONTENT_TYPE);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$arr</span>&nbsp;=&nbsp;explode(<span style="color: black;">/</span>,&nbsp;<span style="color: black;">$t</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(pathinfo(<span style="color: black;">$file_path</span>,&nbsp;PATHINFO_EXTENSION)&nbsp;==&nbsp;<span style="color: black;">tmp</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_path</span>&nbsp;=&nbsp;ecp_handle_ext(<span style="color: black;">$file_path</span>,&nbsp;<span style="color: black;">$arr</span>,&nbsp;<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">path</span>],&nbsp;<span style="color: black;">$file_name</span>,&nbsp;<span style="color: black;">tmp</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$post</span>-&gt;post_content&nbsp;=&nbsp;str_replace(<span style="color: black;">$src</span>,&nbsp;<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">url</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;basename(<span style="color: black;">$file_path</span>),&nbsp;<span style="color: black;">$post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;post_content);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment</span>=&nbsp;ecp_get_attachment_post(basename(<span style="color: black;">$file_path</span>),&nbsp;<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">url</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;basename(<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">));</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attach_id</span>&nbsp;=&nbsp;wp_insert_attachment(<span style="color: black;">$attachment</span>,&nbsp;ltrim(<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">subdir</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;basename(<span style="color: black;">$file_path</span>),&nbsp;<span style="color: black;">/</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),&nbsp;0);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attach_data</span>&nbsp;=&nbsp;wp_generate_attachment_metadata(<span style="color: black;">$attach_id</span>,&nbsp;<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$ss</span>&nbsp;=&nbsp;wp_update_attachment_metadata(<span style="color: black;">$attach_id</span>,&nbsp;<span style="color: black;">$attach_data</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_close(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$wpdb</span>-&gt;update(<span style="color: black;">$wpdb</span>-&gt;posts,&nbsp;array(<span style="color: black;">post_content</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$post</span>-&gt;post_content),&nbsp;array(<span style="color: black;">ID</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;ID));</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">function</span>&nbsp;ecp_handle_ext(<span style="color: black;">$file</span>,&nbsp;<span style="color: black;">$type</span>,&nbsp;<span style="color: black;">$file_dir</span>,&nbsp;<span style="color: black;">$file_name</span>,&nbsp;<span style="color: black;">$ext</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$ext</span>&nbsp;===&nbsp;<span style="color: black;">tmp</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(rename(<span style="color: black;">$file</span>,&nbsp;str_replace(<span style="color: black;">tmp</span>,&nbsp;<span style="color: black;">$type</span>,&nbsp;<span style="color: black;">$file</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)))&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$file_dir</span>&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;str_replace(<span style="color: black;">tmp</span>,&nbsp;<span style="color: black;">$type</span>,&nbsp;<span style="color: black;">$file_name</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$file</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">function</span>&nbsp;ecp_get_attachment_post(<span style="color: black;">$filename</span>,&nbsp;<span style="color: black;">$url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_info</span>&nbsp;=&nbsp;wp_check_filetype(<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;null);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;array(</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">guid</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_type</span>&nbsp;=&gt;&nbsp;<span style="color: black;">attachement</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_mime_type</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$file_info</span>[<span style="color: black;">type</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">],</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_title</span>&nbsp;=&gt;&nbsp;preg_replace(<span style="color: black;">/\.[^.]+$/</span>,&nbsp;,&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_content</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&gt;&nbsp;,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_status</span>&nbsp;=&gt;&nbsp;<span style="color: black;">inherit</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>add_action(<span style="color: black;">save_post</span>,&nbsp;<span style="color: black;">ecp_save_post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;120,&nbsp;2);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不外</span>,<span style="color: black;">亦</span>有这种<span style="color: black;">状况</span>和<span style="color: black;">需要</span>:网站<span style="color: black;">运用</span>了国外服务器,但把<span style="color: black;">照片</span>放在国内服务器上(如利用国内大厂的某些服务当作免费图床)以<span style="color: black;">提高</span>加载速度。<span style="color: black;">因此呢</span><span style="color: black;">期盼</span><span style="color: black;">能够</span>暂时<span style="color: black;">运用</span>国内的<span style="color: black;">照片</span>外链,只是先将外链<span style="color: black;">照片</span>提前备份到<span style="color: black;">媒介</span>库。当某天万一外链<span style="color: black;">照片</span>挂了,再替换成内链<span style="color: black;">照片</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">怎样</span>实现呢?下面<span style="color: black;">供给</span>两段代码,任选其一<span style="color: black;">就可</span>。代码 1 是在<span style="color: black;">以上</span>代码<span style="color: black;">基本</span>上修改而来。</p><strong style="color: blue;">保持原外链<span style="color: black;">照片</span>文件名和格式</strong>,<span style="color: black;">再也不</span>自动重命名上传的<span style="color: black;">照片</span><strong style="color: blue;">仅上传外链<span style="color: black;">照片</span>至<span style="color: black;">媒介</span>库</strong>,<span style="color: black;">再也不</span>自动替换<span style="color: black;">文案</span>中的<span style="color: black;">照片</span>链接<span style="color: black;">位置</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">代码 1</strong></p><span style="color: black;">function</span>&nbsp;ecp_save_post(<span style="color: black;">$post_id</span>,&nbsp;<span style="color: black;">$post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;<span style="color: black;">$wpdb</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>(<span style="color: black;">$post</span>-&gt;post_status&nbsp;==&nbsp;<span style="color: black;">publish</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$p</span>&nbsp;=&nbsp;<span style="color: black;">/&lt;img.*[\s]src=[\"|\</span>](.*)[\"|\<span style="color: black;">].*&gt;/iU</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$num</span>&nbsp;=&nbsp;preg_match_all(<span style="color: black;">$p</span>,&nbsp;<span style="color: black;">$post</span>-&gt;post_content,&nbsp;<span style="color: black;">$matches</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$num</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$wp_upload_dir</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;wp_upload_dir();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set_time_limit(0);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;curl_init();</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_HEADER,&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_RETURNTRANSFER,&nbsp;<span style="color: black;">true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_SSL_VERIFYPEER,&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_FOLLOWLOCATION,&nbsp;<span style="color: black;">true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;CURLOPT_MAXREDIRS,&nbsp;20);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;CURLOPT_CONNECTTIMEOUT,&nbsp;30);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$ecp_options</span>&nbsp;=&nbsp;<span style="color: black;">$_SERVER</span>[<span style="color: black;">HTTP_HOST</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">];</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(<span style="color: black;">$matches</span>&nbsp;as&nbsp;<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(isset(<span style="color: black;">$src</span>)&nbsp;&amp;&amp;&nbsp;strpos(<span style="color: black;">$src</span>,&nbsp;<span style="color: black;">$ecp_options</span>)&nbsp;===&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment_id</span>=&nbsp;attachment_url_to_postid(<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(<span style="color: black;">$attachment_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">continue</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_name</span>&nbsp;=&nbsp;basename(<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_path</span>&nbsp;=&nbsp;<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">path</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;<span style="color: black;">$file_name</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(file_exists(<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">))&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">continue</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_URL,&nbsp;<span style="color: black;">$src</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$img</span>&nbsp;=&nbsp;fopen(<span style="color: black;">$file_path</span>,&nbsp;<span style="color: black;">wb</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt(<span style="color: black;">$ch</span>,&nbsp;CURLOPT_FILE,&nbsp;<span style="color: black;">$img</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$img_data</span>&nbsp;=&nbsp;curl_exec(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>fclose(<span style="color: black;">$img</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(file_exists(<span style="color: black;">$file_path</span>)&nbsp;&amp;&amp;&nbsp;filesize(<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;&gt;&nbsp;0)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment</span>&nbsp;=&nbsp;ecp_get_attachment_post(<span style="color: black;">$file_name</span>,&nbsp;<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">url</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;<span style="color: black;">$file_name</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attach_id</span>&nbsp;=&nbsp;wp_insert_attachment(<span style="color: black;">$attachment</span>,&nbsp;<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;0);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attach_data</span>&nbsp;=&nbsp;wp_generate_attachment_metadata(<span style="color: black;">$attach_id</span>,&nbsp;<span style="color: black;">$file_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>wp_update_attachment_metadata(<span style="color: black;">$attach_id</span>,&nbsp;<span style="color: black;">$attach_data</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_close(<span style="color: black;">$ch</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">function</span>&nbsp;ecp_get_attachment_post(<span style="color: black;">$filename</span>,&nbsp;<span style="color: black;">$url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_info</span>&nbsp;=&nbsp;wp_check_filetype(<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;null);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;array(</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">guid</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_type</span>&nbsp;=&gt;&nbsp;<span style="color: black;">attachment</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_mime_type</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$file_info</span>[<span style="color: black;">type</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">],</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_title</span>&nbsp;=&gt;&nbsp;preg_replace(<span style="color: black;">/\.[^.]+$/</span>,&nbsp;,&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_content</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&gt;&nbsp;,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_status</span>&nbsp;=&gt;&nbsp;<span style="color: black;">inherit</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>add_action(<span style="color: black;">save_post</span>,&nbsp;<span style="color: black;">ecp_save_post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;120,&nbsp;2);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">代码 2</strong></p><span style="color: black;">function</span>&nbsp;backup_external_images_to_media_library(&nbsp;<span style="color: black;">$post_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$post</span>&nbsp;=&nbsp;get_post(&nbsp;<span style="color: black;">$post_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$content</span>&nbsp;=&nbsp;<span style="color: black;">$post</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;post_content;</p>&nbsp;&nbsp;&nbsp;&nbsp;preg_match_all(&nbsp;<span style="color: black;">/&lt;img[^&gt;]+src="([^"&gt;]+)"/i</span>,&nbsp;<span style="color: black;">$content</span>,&nbsp;<span style="color: black;">$matches</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;isset(&nbsp;<span style="color: black;">$matches</span>&nbsp;)&nbsp;&amp;&amp;&nbsp;!empty(&nbsp;<span style="color: black;">$matches</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;)&nbsp;{</p>foreach&nbsp;(<span style="color: black;">$matches</span>&nbsp;as&nbsp;<span style="color: black;">$image_url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;strpos(&nbsp;<span style="color: black;">$image_url</span>,&nbsp;home_url()&nbsp;)&nbsp;===&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment_id</span>&nbsp;=&nbsp;attachment_url_to_postid(&nbsp;<span style="color: black;">$image_url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;!<span style="color: black;">$attachment_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$upload</span>&nbsp;=&nbsp;upload_image_to_media_library_without_replacement(&nbsp;<span style="color: black;">$image_url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;<span style="color: black;">$upload</span>&nbsp;&amp;&amp;&nbsp;!is_wp_error(&nbsp;<span style="color: black;">$upload</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;)&nbsp;{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">function</span>upload_image_to_media_library_without_replacement(<span style="color: black;">$image_url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$image_data</span>&nbsp;=&nbsp;file_get_contents(&nbsp;<span style="color: black;">$image_url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;!<span style="color: black;">$image_data</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;new&nbsp;WP_Error(&nbsp;<span style="color: black;">image_download_failed</span>,&nbsp;__(&nbsp;<span style="color: black;">Failed&nbsp;to&nbsp;download&nbsp;image.</span>,&nbsp;<span style="color: black;">text-domain</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$filename</span>&nbsp;=&nbsp;basename(&nbsp;parse_url(&nbsp;<span style="color: black;">$image_url</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;PHP_URL_PATH&nbsp;)&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$upload_dir</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;wp_upload_dir();</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$upload_path</span>&nbsp;=&nbsp;<span style="color: black;">$upload_dir</span>[<span style="color: black;">path</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;file_exists(&nbsp;<span style="color: black;">$upload_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;!file_put_contents(&nbsp;<span style="color: black;">$upload_path</span>,&nbsp;<span style="color: black;">$image_data</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;new&nbsp;WP_Error(&nbsp;<span style="color: black;">image_save_failed</span>,&nbsp;__(&nbsp;<span style="color: black;">Failed&nbsp;to&nbsp;save&nbsp;image.</span>,&nbsp;<span style="color: black;">text-domain</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$file_type</span>&nbsp;=&nbsp;wp_check_filetype(&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;null&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;=&nbsp;array(</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">guid</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;<span style="color: black;">$upload_dir</span>[<span style="color: black;">url</span>]&nbsp;.&nbsp;<span style="color: black;">/</span>&nbsp;.&nbsp;basename(&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;),</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_mime_type</span>&nbsp;=&gt;&nbsp;<span style="color: black;">$file_type</span>[<span style="color: black;">type</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">],</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_title</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;sanitize_file_name(&nbsp;<span style="color: black;">$filename</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;),</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_content</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;=&gt;&nbsp;,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">post_status</span>&nbsp;&nbsp;&nbsp;&nbsp;=&gt;&nbsp;<span style="color: black;">inherit</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment_id</span>=&nbsp;wp_insert_attachment(<span style="color: black;">$attachment</span>,&nbsp;<span style="color: black;">$upload_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;(&nbsp;!is_wp_error(&nbsp;<span style="color: black;">$attachment_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;)&nbsp;)&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require_once(&nbsp;ABSPATH&nbsp;.&nbsp;<span style="color: black;">wp-admin/includes/image.php</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$attachment_data</span>=&nbsp;wp_generate_attachment_metadata(<span style="color: black;">$attachment_id</span>,&nbsp;<span style="color: black;">$upload_path</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wp_update_attachment_metadata(&nbsp;<span style="color: black;">$attachment_id</span>,&nbsp;<span style="color: black;">$attachment_data</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;array(</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">id</span>&nbsp;&nbsp;=&gt;&nbsp;<span style="color: black;">$attachment_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">url</span>&nbsp;=&gt;&nbsp;wp_get_attachment_url(&nbsp;<span style="color: black;">$attachment_id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;),</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>add_action(&nbsp;<span style="color: black;">save_post</span>,&nbsp;<span style="color: black;">backup_external_images_to_media_library</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">哪段代码更好?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">代码 1</strong>的<span style="color: black;">优良</span>在于它<span style="color: black;">运用</span>了 cURL,<span style="color: black;">能够</span>更好地处理<span style="color: black;">繁杂</span>的网络请求,如大<span style="color: black;">照片</span>下载、重定向等场景。<span style="color: black;">倘若</span>需要处理<span style="color: black;">更加多</span>不确定性<span style="color: black;">原因</span>(如<span style="color: black;">外边</span>服务器响应时间较长、<span style="color: black;">繁杂</span>的重定向<span style="color: black;">状况</span>),<span style="color: black;">第1</span>段代码可能更适合,但<span style="color: black;">亦</span>需要更<span style="color: black;">繁杂</span>的<span style="color: black;">守护</span>和优化。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">代码 2</strong>更加简洁、易读、模块化,且错误处理更清晰,适合大<span style="color: black;">都数</span><span style="color: black;">平常</span>场景。它的实现更符合现代编程习惯,代码<span style="color: black;">守护</span>性较好。<span style="color: black;">倘若</span><span style="color: black;">无</span>特殊的超时或重定向处理<span style="color: black;">需要</span>,它的实现方式较优。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此呢</span>,<span style="color: black;">倘若</span><span style="color: black;">目的</span>是<strong style="color: blue;">简单的下载和备份<span style="color: black;">外边</span><span style="color: black;">照片</span></strong>,<span style="color: black;">举荐</span><span style="color: black;">运用</span> <strong style="color: blue;">代码 2</strong>,<span style="color: black;">由于</span>它更加简洁,且易于扩展和<span style="color: black;">守护</span>。<span style="color: black;">倘若</span>需要处理<span style="color: black;">更加多</span><strong style="color: blue;"><span style="color: black;">繁杂</span>的网络请求</strong>,如跨站点的大文件下载或重定向,<strong style="color: blue;">代码 1</strong> 的 cURL 方式可能更适合。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">倘若</span>仅仅先备份<span style="color: black;">照片</span>到<span style="color: black;">媒介</span>库,小白<span style="color: black;">意见</span>不要在内链<span style="color: black;">照片</span><span style="color: black;">位置</span>中<span style="color: black;">包括</span>年份和月份(日期),以方便以后替换链接。只需要在 WordPress 网站后台 - 设置 - <span style="color: black;">媒介</span> - &nbsp;文件上传,取消勾选”以年—月目录形式组织上传内容“就<span style="color: black;">能够</span>了。</p>




qzmjef 发表于 2024-11-1 13:05:12

期待与你深入交流,共探知识的无穷魅力。

j8typz 发表于 2024-11-9 20:06:37

顶楼主,说得太好了!

wrjc1hod 发表于 5 天前

百度seo优化论坛 http://www.fok120.com/
页: [1]
查看完整版本: 纯代码将 WordPress 文案外链照片备份为网站媒介库本地内链照片