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' );
}
}
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' );
}
}
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" />';
}
}
wp_delete_category()
? In what context? – Jacob Peattie Commented Feb 26, 2019 at 10:05