Opened 3 years ago

Last modified 6 weeks ago

#14162 new enhancement

Introduce WP_Term class

Reported by: scribu Owned by:
Priority: normal Milestone: Future Release
Component: Taxonomy Version:
Severity: normal Keywords:
Cc: kevinB, sirzooro, mikeschinkel@…, andyadamscp@…

Description (last modified by scribu)

In the current taxonomy API, you end up having to pass the taxonomy name over and over again.

I propose we have a WP_Term class to avoid that.

Example

Current:

get_term_link( get_term_by( 'name', 'Term Name', 'taxname' ), 'taxname' )

Proposed:

get_term_by( 'name', 'Term Name', 'taxname' )->get_link()

get_term_by() would return a WP_Term instance instead of a stdClass instance.

Besides get_link(), the WP_Term class could also have an update() method that would replace (or complement) wp_update_term().

Inspired by #14156.

Attachments (2)

wp-term.php (748 bytes) - added by scribu 3 years ago.
implemented as a plugin: get_link(), get_children()
wp-term.2.php (807 bytes) - added by scribu 3 years ago.
extra check for get_terms()

Download all attachments as: .zip

Change History (15)

Chaining functions would have to wait until PHP 5.

  • Description modified (diff)

Sure, but this enhancement is not dependant on chaining. You could still do this:

$term = get_term_by( 'name', 'Term Name', 'taxname' );
echo $term->get_link();

which would still be better than:

$term = get_term_by( 'name', 'Term Name', 'taxname' );
echo get_term_link( $term, 'taxname' );

Found out that this is pretty easy to implement as a plugin. See wp-term.php

Having a WP_Term object is a great idea.

Instead of an update method that gets an array of arguments, why not have it save the current object's properties to the database?

$term->set_description('my new description');
$term->update(); // now the new description is saved to the database

As suggested above, it would also be nice to have getters and possibly setters for the other salient properties in addition to "link."

Yeah, another one would be WP_Term::get_children().

scribu3 years ago

implemented as a plugin: get_link(), get_children()

  • Cc kevinB added

comment:7 follow-up: ↓ 8   westi3 years ago

Personally, I'm not a big fan of this type of encapsulation working its way into WordPress.

I want to ensure that everything is simple to use and I'm not sure lots of objects like this is the way to go.

comment:8 in reply to: ↑ 7   filosofo3 years ago

Replying to westi:

Personally, I'm not a big fan of this type of encapsulation working its way into WordPress.

I want to ensure that everything is simple to use and I'm not sure lots of objects like this is the way to go.

Could you elaborate your objections? Terms already are objects, so this would extend existing functionality, not diminish it or require people to use a new API.

  • Cc sirzooro added

scribu3 years ago

extra check for get_terms()

  • Milestone changed from Awaiting Review to Future Release
  • Summary changed from Introduce WP_Term to Introduce WP_Term class
  • Cc mikeschinkel@… added
  • Cc andyadamscp@… added
Note: See TracTickets for help on using tickets.