纯代码将 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> ecp_save_post(<span style="color: black;">$post_id</span>, <span style="color: black;">$post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> global <span style="color: black;">$wpdb</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">if</span> (<span style="color: black;">$post</span>->post_status == <span style="color: black;">publish</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$p</span> = <span style="color: black;">/<img.*[\s]src=[\"|\</span>](.*)[\"|\<span style="color: black;">].*>/iU</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">$num</span> = preg_match_all(<span style="color: black;">$p</span>, <span style="color: black;">$post</span>->post_content, <span style="color: black;">$matches</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">if</span> (<span style="color: black;">$num</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$wp_upload_dir</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> = wp_upload_dir();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> set_time_limit(0);</p> <span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> = curl_init();</p>curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_HEADER, <span style="color: black;">false</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>, CURLOPT_RETURNTRANSFER, <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>, CURLOPT_SSL_VERIFYPEER, <span style="color: black;">false</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>, 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> curl_setopt(<span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, CURLOPT_MAXREDIRS, 20);</p> curl_setopt(<span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, CURLOPT_CONNECTTIMEOUT, 30);</p> <span style="color: black;">$ecp_options</span> = <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> foreach (<span style="color: black;">$matches</span> as <span style="color: black;">$src</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">if</span> (isset(<span style="color: black;">$src</span>) && strpos(<span style="color: black;">$src</span>, <span style="color: black;">$ecp_options</span>) === <span style="color: black;">false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$file_info</span> = 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;">), null);</p> <span style="color: black;">if</span> (<span style="color: black;">$file_info</span>[<span style="color: black;">ext</span>] == <span style="color: black;">false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> 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> <span style="color: black;">$file_name</span> = date(<span style="color: black;">YmdHis-</span>) . dechex(mt_rand(100000, 999999)) . <span style="color: black;">.tmp</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> } <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">$file_name</span>= dechex(mt_rand(100000, 999999)) .<span style="color: black;">-</span> . 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;"> }</p> curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_URL, <span style="color: black;">$src</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">$file_path</span> = <span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">path</span>] . <span style="color: black;">/</span> . <span style="color: black;">$file_name</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">$img</span> = fopen(<span style="color: black;">$file_path</span>, <span style="color: black;">wb</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>, 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> <span style="color: black;">$img_data</span> = 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> <span style="color: black;">if</span> (file_exists(<span style="color: black;">$file_path</span>) && filesize(<span style="color: black;">$file_path</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) > 0) {</p> <span style="color: black;">$t</span> = curl_getinfo(<span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, CURLINFO_CONTENT_TYPE);</p> <span style="color: black;">$arr</span> = explode(<span style="color: black;">/</span>, <span style="color: black;">$t</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">if</span> (pathinfo(<span style="color: black;">$file_path</span>, PATHINFO_EXTENSION) == <span style="color: black;">tmp</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$file_path</span> = ecp_handle_ext(<span style="color: black;">$file_path</span>, <span style="color: black;">$arr</span>, <span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">path</span>], <span style="color: black;">$file_name</span>, <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;"> }</p> <span style="color: black;">$post</span>->post_content = str_replace(<span style="color: black;">$src</span>, <span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">url</span>] . <span style="color: black;">/</span> . basename(<span style="color: black;">$file_path</span>), <span style="color: black;">$post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">->post_content);</p> <span style="color: black;">$attachment</span>= ecp_get_attachment_post(basename(<span style="color: black;">$file_path</span>), <span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">url</span>] . <span style="color: black;">/</span> . 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> <span style="color: black;">$attach_id</span> = wp_insert_attachment(<span style="color: black;">$attachment</span>, ltrim(<span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">subdir</span>] . <span style="color: black;">/</span> . basename(<span style="color: black;">$file_path</span>), <span style="color: black;">/</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">), 0);</p> <span style="color: black;">$attach_data</span> = wp_generate_attachment_metadata(<span style="color: black;">$attach_id</span>, <span style="color: black;">$file_path</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">$ss</span> = wp_update_attachment_metadata(<span style="color: black;">$attach_id</span>, <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;"> }</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;"> }</p> 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> <span style="color: black;">$wpdb</span>->update(<span style="color: black;">$wpdb</span>->posts, array(<span style="color: black;">post_content</span> => <span style="color: black;">$post</span>->post_content), array(<span style="color: black;">ID</span> => <span style="color: black;">$post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">->ID));</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;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">function</span> ecp_handle_ext(<span style="color: black;">$file</span>, <span style="color: black;">$type</span>, <span style="color: black;">$file_dir</span>, <span style="color: black;">$file_name</span>, <span style="color: black;">$ext</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">if</span> (<span style="color: black;">$ext</span> === <span style="color: black;">tmp</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">if</span> (rename(<span style="color: black;">$file</span>, str_replace(<span style="color: black;">tmp</span>, <span style="color: black;">$type</span>, <span style="color: black;">$file</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">))) {</p> <span style="color: black;">return</span> <span style="color: black;">$file_dir</span> . <span style="color: black;">/</span> . str_replace(<span style="color: black;">tmp</span>, <span style="color: black;">$type</span>, <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;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span> <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> ecp_get_attachment_post(<span style="color: black;">$filename</span>, <span style="color: black;">$url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$file_info</span> = 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;">, null);</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> array(</p> <span style="color: black;">guid</span> => <span style="color: black;">$url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">post_type</span> => <span style="color: black;">attachement</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">post_mime_type</span> => <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> <span style="color: black;">post_title</span> => preg_replace(<span style="color: black;">/\.[^.]+$/</span>, , <span style="color: black;">$filename</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p> <span style="color: black;">post_content</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> => ,</p> <span style="color: black;">post_status</span> => <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;">}</p>add_action(<span style="color: black;">save_post</span>, <span style="color: black;">ecp_save_post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, 120, 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> ecp_save_post(<span style="color: black;">$post_id</span>, <span style="color: black;">$post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> global <span style="color: black;">$wpdb</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">if</span>(<span style="color: black;">$post</span>->post_status == <span style="color: black;">publish</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$p</span> = <span style="color: black;">/<img.*[\s]src=[\"|\</span>](.*)[\"|\<span style="color: black;">].*>/iU</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">$num</span> = preg_match_all(<span style="color: black;">$p</span>, <span style="color: black;">$post</span>->post_content, <span style="color: black;">$matches</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">if</span> (<span style="color: black;">$num</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$wp_upload_dir</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> = wp_upload_dir();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> set_time_limit(0);</p> <span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> = curl_init();</p> curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_HEADER, <span style="color: black;">false</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>, CURLOPT_RETURNTRANSFER, <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>, CURLOPT_SSL_VERIFYPEER, <span style="color: black;">false</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>, 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> curl_setopt(<span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, CURLOPT_MAXREDIRS, 20);</p> curl_setopt(<span style="color: black;">$ch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, CURLOPT_CONNECTTIMEOUT, 30);</p> <span style="color: black;">$ecp_options</span> = <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> foreach (<span style="color: black;">$matches</span> as <span style="color: black;">$src</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">if</span> (isset(<span style="color: black;">$src</span>) && strpos(<span style="color: black;">$src</span>, <span style="color: black;">$ecp_options</span>) === <span style="color: black;">false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$attachment_id</span>= 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> <span style="color: black;">if</span> (<span style="color: black;">$attachment_id</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <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;"> }</p> <span style="color: black;">$file_name</span> = basename(<span style="color: black;">$src</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">$file_path</span> = <span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">path</span>] . <span style="color: black;">/</span> . <span style="color: black;">$file_name</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">if</span> (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;">)) {</p> <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;"> }</p> curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_URL, <span style="color: black;">$src</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">$img</span> = fopen(<span style="color: black;">$file_path</span>, <span style="color: black;">wb</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>, 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> <span style="color: black;">$img_data</span> = 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> <span style="color: black;">if</span> (file_exists(<span style="color: black;">$file_path</span>) && filesize(<span style="color: black;">$file_path</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) > 0) {</p> <span style="color: black;">$attachment</span> = ecp_get_attachment_post(<span style="color: black;">$file_name</span>, <span style="color: black;">$wp_upload_dir</span>[<span style="color: black;">url</span>] . <span style="color: black;">/</span> . <span style="color: black;">$file_name</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">$attach_id</span> = wp_insert_attachment(<span style="color: black;">$attachment</span>, <span style="color: black;">$file_path</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, 0);</p> <span style="color: black;">$attach_data</span> = wp_generate_attachment_metadata(<span style="color: black;">$attach_id</span>, <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>, <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;"> }</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;"> }</p> 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;"> }</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;">}</p><span style="color: black;">function</span> ecp_get_attachment_post(<span style="color: black;">$filename</span>, <span style="color: black;">$url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$file_info</span> = 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;">, null);</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> array(</p> <span style="color: black;">guid</span> => <span style="color: black;">$url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">post_type</span> => <span style="color: black;">attachment</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">post_mime_type</span> => <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> <span style="color: black;">post_title</span> => preg_replace(<span style="color: black;">/\.[^.]+$/</span>, , <span style="color: black;">$filename</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p> <span style="color: black;">post_content</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> => ,</p> <span style="color: black;">post_status</span> => <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;">}</p>add_action(<span style="color: black;">save_post</span>, <span style="color: black;">ecp_save_post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, 120, 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> backup_external_images_to_media_library( <span style="color: black;">$post_id</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) {</p> <span style="color: black;">$post</span> = get_post( <span style="color: black;">$post_id</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> );</p> <span style="color: black;">$content</span> = <span style="color: black;">$post</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">->post_content;</p> preg_match_all( <span style="color: black;">/<img[^>]+src="([^">]+)"/i</span>, <span style="color: black;">$content</span>, <span style="color: black;">$matches</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> );</p> <span style="color: black;">if</span> ( isset( <span style="color: black;">$matches</span> ) && !empty( <span style="color: black;">$matches</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) ) {</p>foreach (<span style="color: black;">$matches</span> as <span style="color: black;">$image_url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) {</p> <span style="color: black;">if</span> ( strpos( <span style="color: black;">$image_url</span>, home_url() ) === <span style="color: black;">false</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) {</p> <span style="color: black;">$attachment_id</span> = attachment_url_to_postid( <span style="color: black;">$image_url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> );</p> <span style="color: black;">if</span> ( !<span style="color: black;">$attachment_id</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">$upload</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;"> );</p> <span style="color: black;">if</span> ( <span style="color: black;">$upload</span> && !is_wp_error( <span style="color: black;">$upload</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>
<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>
<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>
<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;"> ) {</p> <span style="color: black;">$image_data</span> = file_get_contents( <span style="color: black;">$image_url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> );</p> <span style="color: black;">if</span> ( !<span style="color: black;">$image_data</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) {</p> <span style="color: black;">return</span> new WP_Error( <span style="color: black;">image_download_failed</span>, __( <span style="color: black;">Failed to download image.</span>, <span style="color: black;">text-domain</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;">$filename</span> = basename( parse_url( <span style="color: black;">$image_url</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, PHP_URL_PATH ) );</p> <span style="color: black;">$upload_dir</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> = wp_upload_dir();</p> <span style="color: black;">$upload_path</span> = <span style="color: black;">$upload_dir</span>[<span style="color: black;">path</span>] . <span style="color: black;">/</span> . <span style="color: black;">$filename</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">if</span> ( file_exists( <span style="color: black;">$upload_path</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) ) {</p> <span style="color: black;">return</span> <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> <span style="color: black;">if</span> ( !file_put_contents( <span style="color: black;">$upload_path</span>, <span style="color: black;">$image_data</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) ) {</p> <span style="color: black;">return</span> new WP_Error( <span style="color: black;">image_save_failed</span>, __( <span style="color: black;">Failed to save image.</span>, <span style="color: black;">text-domain</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;">$file_type</span> = 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;">, null );</p> <span style="color: black;">$attachment</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> = array(</p> <span style="color: black;">guid</span> => <span style="color: black;">$upload_dir</span>[<span style="color: black;">url</span>] . <span style="color: black;">/</span> . basename( <span style="color: black;">$filename</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ),</p> <span style="color: black;">post_mime_type</span> => <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> <span style="color: black;">post_title</span> => sanitize_file_name( <span style="color: black;">$filename</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ),</p> <span style="color: black;">post_content</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> => ,</p> <span style="color: black;">post_status</span> => <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;"> );</p> <span style="color: black;">$attachment_id</span>= wp_insert_attachment(<span style="color: black;">$attachment</span>, <span style="color: black;">$upload_path</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> );</p> <span style="color: black;">if</span> ( !is_wp_error( <span style="color: black;">$attachment_id</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ) ) {</p> require_once( ABSPATH . <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;"> );</p> <span style="color: black;">$attachment_data</span>= wp_generate_attachment_metadata(<span style="color: black;">$attachment_id</span>, <span style="color: black;">$upload_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;">$attachment_id</span>, <span style="color: black;">$attachment_data</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> );</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> array(</p> <span style="color: black;">id</span> => <span style="color: black;">$attachment_id</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p> <span style="color: black;">url</span> => wp_get_attachment_url( <span style="color: black;">$attachment_id</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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span> <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( <span style="color: black;">save_post</span>, <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;"> );</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> - 文件上传,取消勾选”以年—月目录形式组织上传内容“就<span style="color: black;">能够</span>了。</p>
期待与你深入交流,共探知识的无穷魅力。 顶楼主,说得太好了!
页:
[1]