Using WordPress ‘get_term_to_edit()’ PHP function

The get_term_to_edit() WordPress PHP function sanitizes a term for editing purposes.

Usage

$term_to_edit = get_term_to_edit( $term_id, $taxonomy );

Parameters

  • $term_id (int|object): The term ID or term object that you want to sanitize for editing.
  • $taxonomy (string): The taxonomy name associated with the term.

More information

See WordPress Developer Resources: get_term_to_edit()

Examples

Sanitize a category term for editing

In this example, we sanitize a category term with the ID 3 for editing purposes.

$term_id = 3;
$taxonomy = 'category';
$term_to_edit = get_term_to_edit( $term_id, $taxonomy );

Sanitize a custom taxonomy term for editing

In this example, we sanitize a term with the ID 5 from a custom taxonomy called ‘product_type’.

$term_id = 5;
$taxonomy = 'product_type';
$term_to_edit = get_term_to_edit( $term_id, $taxonomy );

Sanitize a tag term for editing using a term object

In this example, we sanitize a tag term using a term object.

$term_object = get_term( 7, 'post_tag' );
$taxonomy = 'post_tag';
$term_to_edit = get_term_to_edit( $term_object, $taxonomy );

Sanitize a term for editing and access its properties

In this example, we sanitize a term with the ID 9 for editing, and then access its name and description properties.

$term_id = 9;
$taxonomy = 'category';
$term_to_edit = get_term_to_edit( $term_id, $taxonomy );

$term_name = $term_to_edit->name;
$term_description = $term_to_edit->description;

Check if a term exists before sanitizing for editing

In this example, we check if a term exists in a custom taxonomy called ‘location’ before sanitizing it for editing.

$term_id = 11;
$taxonomy = 'location';

if ( term_exists( $term_id, $taxonomy ) ) {
    $term_to_edit = get_term_to_edit( $term_id, $taxonomy );
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *