Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active Aug 29, 2015
Embed
What would you like to do?
[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