I'm trying to create a plugin that will delete pictures and additional info from database on deleting custom post (sp_venue) via admin panel (wp-admin/edit-tags.php)

In the plugin I'm using this to catch the event:

add_action( 'delete_post', 'kg_delete_post' );

function kg_delete_post($postId) {

    $post = get_post($postId);

    if ($post->post_type != 'attachment') {
        return false;

    $url = str_replace($dirs['baseurl'],'',$post->guid);
    $urlParts = explode("/",$url);
    $numberOfParts = sizeof($urlParts) - 1;

    $dirs = wp_upload_dir();
    $fileNameParts = explode(".", $urlParts[$numberOfParts]);
    $fileName = str_replace('.' . end($fileNameParts), '', $urlParts[$numberOfParts]) . "-*." . end($fileNameParts);
    $path =$dirs['basedir'] ."/".  $urlParts[$numberOfParts-2] . "/" . $urlParts[$numberOfParts-1] . "/";
    $fullPath = $path  . $urlParts[$numberOfParts];
    $fullPathSearch = $path . $fileName;

    foreach (glob($fullPathSearch) as $filename) {
        @unlink($path . $filename);

It works with:

wp_delete_post($Id, true)

But looks like the event on deleting via admin panel is no the same.

What should i use to make it works? Thank you.

  • Do not cross post around the network. Stick with your original question please.
    – kaiser
    Jun 30 '14 at 12:57
  • Also please fix your formatting.
    – kaiser
    Jun 30 '14 at 13:05
  • @kaiser sorry, i didn't receive any answers on my original post that's why i came here to more specific site.
    – CrazyWu
    Jun 30 '14 at 13:42
  • As cross posting is forbidden on StackExchange per the FAQ, please decide which question you want to leave and which one you close or delete. Thank you.
    – kaiser
    Jun 30 '14 at 19:36

I would take a look at http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post as the example seems to be doing exactly what you want to do in a situation. Your actual code as far as hooking into WordPress looks fine to me, but a key note to remember from the page I linked to is that this event is triggered by post deletion from the database, and not directly from clicking the button to trash a post in the admin panel.

So yeah, test with delete_post() and get back to me.

  • I've used this to write my example, but original code or changing the priorities changes nothing.
    – CrazyWu
    Jul 1 '14 at 10:51

Solved, by adding my own js in admin panel to customize the click on delete button.

  • Please add an explanation to your answer: why and how exactly did that solve the problem?
    – fuxia
    Jul 2 '14 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.