Make WordPress Core

Opened 22 months ago

Closed 22 months ago

Last modified 10 months ago

#21150 closed defect (bug) (invalid)

Add a way to prevent a term from being deleted

Reported by: wpsmith Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords:
Focuses: Cc:


Currently, WordPress uses an option ('default_category') to prevent the deletion of the default WordPress category. However, someone creates a default term for a custom taxonomy, there is currently no easy method to prevent the deletion of the term.

I propose a simple filter that would enable this.

Attachments (1)

delete_term.patch (544 bytes) - added by wpsmith 22 months ago.

Download all attachments as: .zip

Change History (10)

wpsmith22 months ago

comment:1 wpsmith22 months ago

Just saw this in WP_Terms_List_Table:

$default_term = get_option( 'default_' . $taxonomy );

comment:2 wpsmith22 months ago

  • Keywords has-patch 2nd-opinion dev-feedback removed
  • Resolution set to wontfix
  • Status changed from new to closed

comment:3 helenyhou22 months ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

comment:4 helenyhou22 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from reopened to closed

comment:5 maxaud10 months ago

I currently don't see a way to stop a term from being deleted in wp_delete_term function.
I can recreate it after it's already been deleted using the do_action at the end but this is after all the associations have been removed.

I have a custom taxonomy that I need to prevent some terms from being removed.

comment:6 markoheijnen10 months ago

This ticket is about having a default term for a taxonomy. You can set the option 'default' . $taxonomy to the id of the default term. You can also use a filter if desired or needed. But this only works for the list table row actions and not when using wp_delete_term() directly. What happens when you do bulk delete.

In your case you can use tag_row_actions or {$taxonomy}_row_actions with the same flaws. I don't think the filter 'has_cap' will do any good. Not sure if there is a ticket for meta_cap support for taxonomies.

comment:7 maxaud10 months ago

I don't think this ticket is about having a default term set for taxonomies.
The reason it was brought up as a solution is because the current code only checks if it's the default term and doesn't delete if it is. It's an incorrect approach to fix the real problem.

Essentially there should be a way to easily block the removal of any term just by using a filter before anything happens.

comment:8 markoheijnen10 months ago

The title of this ticket says something different then the description. This ticket is about having a default term for a custom taxonomy. Otherwise I don't think that the reporter would close it himself.

You want capabilities like delete_term and then you would need something like meta_caps. You can create a ticket for that.

comment:9 maxaud10 months ago

What I would like is the ability to stop a couple terms from being deleted (preferably from a certain taxonomy by ID). The title and ticket description say just this. The comments following focused on only current way that it's being stopped and only works for one term rather than multiple.

The recommended approach in the above comments is to remove the delete button on the front end where the function itself needs the ability to prevent terms from being deleted through plugins and other ways that circumvent the UI.

Additionally, a capability would lead me to believe that you want to block the ability to delete all terms for a specific roles. This is not what I see this ticket being.

Last edited 10 months ago by maxaud (previous) (diff)
Note: See TracTickets for help on using tickets.