Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/599ffb32f53142c7ebae to your computer and use it in GitHub Desktop.
Save tommcfarlin/599ffb32f53142c7ebae to your computer and use it in GitHub Desktop.
[WordPress] Programmatically delete files using WordPress and remove associated post meta data.
<?php
$meta_key = $_REQUEST['meta-key'] . '-fs';
// As long as the meta data for the path to the file on the system exists, we can use it
// to remove the file from the file system.
if ( '' !== ( $fs_url = trim( get_post_meta( $_REQUEST['post_id'], $meta_key, true ) ) ) ) {
unlink ( $fs_url );
}
<?php
function acme_remove_file() {
if ( wp_verify_nonce( $_REQUEST['nonce'], 'example-jpg-nonce' ) ) {
// Retrieve the meta key. In this example, the key is actually being sent
// as part of the request; however, it may be the case that you have
// to generate it based on some type of incoming information from the
// request if you're using greater security measures, random key generation,
// or something similar.
$meta_key = '';
if ( isset( $_REQUEST['meta-key'] ) && '' !== trim( $_REQUEST['meta-key'] ) ) {
// From the first article, we know that we're storing the value in the file system
// using the meta key with a '-fs' suffix.
$meta_key = $_REQUEST['meta-key'] . '-fs';
// As long as the meta data for the path to the file on the system exists, we can use it
// to remove the file from the file system.
if ( '' !== ( $fs_url = trim( get_post_meta( $_REQUEST['post_id'], $meta_key, true ) ) ) ) {
unlink ( $fs_url );
}
// Next, we'll remove the the rest of the associated meta data from the post
$meta_key = $_REQUEST['meta-key'];
delete_post_meta( $_REQUEST['post_id'], $meta_key, true );
} // end if
} // end if
}
<?php
$meta_key = $_REQUEST['meta-key'];
delete_post_meta( $_REQUEST['post_id'], $meta_key );
<?php
// Retrieve the meta key. In this example, the key is actually being sent
// as part of the request; however, it may be the case that you have
// to generate it based on some type of incoming information from the
// request if you're using greater security measures, random key generation,
// or something similar.
$meta_key = '';
if ( isset( $_REQUEST['meta-key'] ) && '' !== trim( $_REQUEST['meta-key'] ) ) {
// From the first article, we know that we're storing the value in the file system
// using the meta key with a '-fs' suffix.
$meta_key = $_REQUEST['meta-key'] . '-fs';
// Next, we'll remove the the rest of the associated meta data from the post
$meta_key = $_REQUEST['meta-key'];
} // end if
<?php
function acme_remove_file() {
if ( wp_verify_nonce( $_REQUEST['nonce'], 'example-jpg-nonce' ) ) {
// TODO
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment