WordPress网站删除文章的同时自动删除缩略图及图片附件

前端之家收集整理的这篇文章主要介绍了WordPress网站删除文章的同时自动删除缩略图及图片附件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

wordpress建站后期,当我们想要删除文章的时候,发现文章上传到媒体库的图片等附件并不会自动删除,这样占用了大量的网站空间,下面我们介绍一下在删除文章自动删除缩略图以及图片附件的方法,只需几句简单的代码便可实现,不用手动去媒体库寻找并删除,准确率和效率都非常高。

切换到主题目录,打开functions.PHP文件,加入以下代码

Crayon-5c891d2e529a3034651964" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-2">2
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-4">4
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-6">6
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-8">8
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-10">10
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-12">12
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-14">14
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d2e529a3034651964-16">16
Crayon-num" data-line="Crayon-5c891d2e529a3034651964-17">17
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d2e529a3034651964-1">Crayon-c">/* 删除文章删除图片附件
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-2">Crayon-c">/* ------------------------ */
Crayon-line" id="Crayon-5c891d2e529a3034651964-3">Crayon-t">functionCrayon-h"> Crayon-e">delete_post_and_attachmentsCrayon-sy">(Crayon-sy">$Crayon-v">post_IDCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-4">Crayon-h">Crayon-m">globalCrayon-h"> Crayon-sy">$Crayon-v">wpdbCrayon-sy">;
Crayon-line" id="Crayon-5c891d2e529a3034651964-5">Crayon-h">Crayon-c">//删除特色图片
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-6">Crayon-h">Crayon-sy">$Crayon-v">thumbnailsCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">wpdbCrayon-e">get_resultsCrayon-sy">(Crayon-h"> postMeta WHERE Meta_key = '_thumbnail_id' AND post_id = $post_ID"Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d2e529a3034651964-7">Crayon-h">Crayon-st">foreachCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-sy">$Crayon-e">thumbnails Crayon-st">asCrayon-h"> Crayon-sy">$Crayon-i">thumbnailCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-8">Crayon-h">Crayon-e">wp_delete_attachmentCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">thumbnailCrayon-v">Meta_valueCrayon-sy">,Crayon-h"> Crayon-t">trueCrayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d2e529a3034651964-9">Crayon-h">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-10">Crayon-h">Crayon-c">//删除图片附件
Crayon-line" id="Crayon-5c891d2e529a3034651964-11">Crayon-h">Crayon-sy">$Crayon-v">attachmentsCrayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">wpdbCrayon-e">get_resultsCrayon-sy">(Crayon-h"> posts WHERE post_parent = $post_ID AND post_type = 'attachment'"Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-12">Crayon-h">Crayon-st">foreachCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-sy">$Crayon-e">attachments Crayon-st">asCrayon-h"> Crayon-sy">$Crayon-i">attachmentCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891d2e529a3034651964-13">Crayon-h">Crayon-e">wp_delete_attachmentCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">attachmentCrayon-v">IDCrayon-sy">,Crayon-h"> Crayon-t">trueCrayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-14">Crayon-h">Crayon-sy">}
Crayon-line" id="Crayon-5c891d2e529a3034651964-15">Crayon-h">Crayon-sy">$Crayon-v">wpdbCrayon-e">queryCrayon-sy">(Crayon-h"> postMeta WHERE Meta_key = '_thumbnail_id' AND post_id = $post_ID"Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d2e529a3034651964-16">Crayon-sy">}
Crayon-line" id="Crayon-5c891d2e529a3034651964-17">Crayon-e">add_actionCrayon-sy">(Crayon-s">'before_delete_post'Crayon-sy">,Crayon-h"> Crayon-s">'delete_post_and_attachments'Crayon-sy">)Crayon-sy">;

注意:此处使用的是 before_delete_post 这个 action ,而不是 delete_post 。如果使用的是 delete_post 这个 action 将导致删除文章后因媒体附件与文章关联已取消而无法正确删除

原文链接:https://www.f2er.com/wordpress/238482.html

猜你在找的wordpress相关文章