-
-
Save tommcfarlin/599ffb32f53142c7ebae to your computer and use it in GitHub Desktop.
[WordPress] Programmatically delete files using WordPress and remove associated post meta data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$meta_key = $_REQUEST['meta-key']; | |
delete_post_meta( $_REQUEST['post_id'], $meta_key ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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