Skip to content

Instantly share code, notes, and snippets.

@cdolek
Last active March 11, 2020 19:26
Show Gist options
  • Save cdolek/372fc6421feffcd39860 to your computer and use it in GitHub Desktop.
Save cdolek/372fc6421feffcd39860 to your computer and use it in GitHub Desktop.
Utility for wordpress: Find missing attachment files, non writable upload directories and meta errors
<?php
/**
* Template Name: Attachment Fixing Utility
*/
?>
<html>
<head>
<title>Wordpress Attachments Fixing Utility</title>
<style>
body {
margin: 40px;
font-size: 14px;
font-family: 'Arial', sans-serif;
}
</style>
</head>
<body>
<?php
/**
*
* Put this file in your theme directory
* Create a page called "fix" (slug)
* Visit http://yoursite/fix/ !
* Or use the template while creating the page.
*
*/
$i = 0;
$s = 0;
$ud = wp_upload_dir();
$missingArr = array();
$noMetaArr = array();
$directoriesToBeCreatedArr = array();
$directoriesNotWritableArr = array();
$args = array (
'post_type' => 'attachment',
'post_status' => null,
'posts_per_page' => -1
);
$attachments = get_posts( $args );
/* find the missing attachments */
foreach ( $attachments as $key => $attachment ) {
/* get meta data */
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( is_array( $meta ) ) {
$path = $ud['basedir'] .'/'. $meta['file'];
if ( !file_exists( $path ) ) {
$pathinfo = pathinfo($path);
$missingArr[$s]['path'] = $path;
$missingArr[$s]['edit_attachment'] = '<a href="'. get_edit_post_link( $attachment->ID ) . '">'. get_edit_post_link( $attachment->ID ) . '</a>';
$missingArr[$s]['edit_parent_post'] = '<a href="'.get_edit_post_link( $attachment->post_parent ).'">'.get_edit_post_link( $attachment->post_parent ).'</a>';
$missingArr[$s]['directory'] = $pathinfo['dirname'];
/* do we have the dir ? */
if ( ! file_exists($pathinfo['dirname']) ){
$directoriesToBeCreatedArr[] = $pathinfo['dirname'];
} else if ( ! is_writable($pathinfo['dirname']) ){
$directoriesNotWritableArr[] = $pathinfo['dirname'];
}
$s++;
}
} else {
$noMetaArr[$i]['ID'] = $attachment->ID;
$noMetaArr[$i]['meta'] = $meta;
$noMetaArr[$i]['parentID'] = $attachment->post_parent;
$noMetaArr[$i]['edit_attachment'] = '<a href="'. get_edit_post_link( $attachment->ID ) . '">'. get_edit_post_link( $attachment->ID ) . '</a>';
$noMetaArr[$i]['edit_parent_post'] = '<a href="'.get_edit_post_link( $attachment->post_parent ).'">'.get_edit_post_link( $attachment->post_parent ).'</a>';
$i++;
}
}
/* print results and do actions */
if ( sizeof( $directoriesNotWritableArr ) > 0 ) {
echo( '<h1>Directories Not Writable ('.sizeof( $directoriesNotWritableArr ).')</h1><pre>' );
print_r( $directoriesNotWritableArr );
echo( '</pre><hr>' );
}
if ( sizeof( $directoriesToBeCreatedArr ) > 0 ) {
echo( '<h1>Directories Created ('.sizeof( $directoriesToBeCreatedArr ).')</h1><pre>' );
print_r( $directoriesToBeCreatedArr );
echo( '</pre><hr>' );
}
if ( sizeof( $missingArr ) > 0 ) {
echo( '<h1>Missing Files ('.sizeof( $missingArr ).')</h1><pre>' );
print_r( $missingArr );
echo( '</pre><hr>' );
}
if ( sizeof( $noMetaArr ) > 0 ) {
echo( '<h1>No meta? ('.sizeof( $noMetaArr ).')</h1><pre>');
print_r( $noMetaArr );
echo( '</pre>' );
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment