0

I've tried using wp_delete_category() and wp_delete_term() to delete a category, but after the page reloads, the deleted category still shows up in the results of get_categories(). But if I reload the page a second time, the category is gone.

How can I make sure it goes away after the first reload?


I'm grabbing form data with a $_POST and handing to wp_delete_category(). This is all taking place on a plugin settings page. Here's a little snippet of my code. Suggestions welcome.

if ( isset($_POST['deleteSelectedCategories']) ){

    $cats = $_POST['catsToDelete'];
    $catsArray = explode(',', $cats);

    foreach ($catsArray as $catID) {
        // wp_delete_category( $catID );        
        wp_delete_term( $catID, 'category' );
    }
}
4
  • Do you have any caching plugins active on that site? Does your hosting use any caches? Feb 26, 2019 at 6:36
  • No caching plugins installed and nothing in the cpanel. Any other changes I make show up right away. Basically, no evidence of caching as far as I can tell.
    – thingEvery
    Feb 26, 2019 at 6:59
  • When exactly are you running wp_delete_category()? In what context? Feb 26, 2019 at 10:05
  • @JacobPeattie I'm calling it from a plugin settings page. I've updated my question.
    – thingEvery
    Feb 26, 2019 at 15:13

1 Answer 1

0

I've found a workaround by forcing the second refresh, but I'm sure there's probably a better way to do this.

In order to make the process smoother for the user, I'm thinking I'll have to do an AJAX post and then refresh on success to show the updated data.


if ( isset($_POST['deleteSelectedCategories']) ){

    $cats = $_POST['catsToDelete'];
    $catsArray = explode(',', $cats);

    $success = 0;

    foreach ($catsArray as $catID) {
        if ( wp_delete_term( $catID, 'category' ) ) {
            $success++;
        }
    }

    if ($success > 0) {
        echo '[Here I have some HTML to block out the screen 
            and show a message to the user, 
            and then the following meta refresh:] 
            <meta http-equiv="refresh" content="0" />';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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