How can I code the following snippet in a more object-oriented way?

[ad_1]

I’m trying to teach myself how to approach WordPress in a more OOP way. Here’s a snippet from https://wordpress.stackexchange.com/a/380498

“`

function wpse301755_change_tags_labels( $args, $taxonomy ) {
if ( ‘post_tag’ === $taxonomy ) {
$args[‘labels’] = array(
‘name’ => ‘Locations’,
‘singular_name’ => ‘Location’,
‘menu_name’ => ‘Locations’,
);
}
return $args;
}
add_filter( ‘register_taxonomy_args’, ‘wpse301755_change_tags_labels’, 10, 2 );
“`

This function changes the default “Tag” taxonomy and changes some of the text labels to “Location”. I’d like to refactor this feature to be more OOP.

From the start, I’m pretty sure I’d want to create a “Locations” class, which to begin with would have some really simple properties such as “singular_label” and “plural_label”.

However, I don’t want to tie this class down into WordPress too much, so I’d like to avoid having any action or filter hooks in its constructor.

Where I’m confusing myself is:

* would it be more appropriate to create some sort of “Tags” class since its purpose is to modify the core Tags taxonomy, rather than register a new taxonomy?
* Do I need to create a more generic “Taxonomy” class and go from there?
* Do I need to create a class for adding the “register_taxonomy_args” filter?
* Where exactly should the core replacing logic go?
* how can I achieve this with Dependency Injection?
* Am I driving myself crazy over-engineering this?

Thank you

EDIT: this article has a pretty good demonstration of what I’m trying to do https://pressidium.com/blog/part-8-wordpress-and-object-oriented-programming/

[ad_2]
3 Comments
  1. You might have more luck asking in r/ProWordpress.

    *Am I driving myself crazy over-engineering this? -* Absolutely. There no reason taxonomies need to be complicated. They’re a simple set + forget type of thing.

    *”I don’t want to tie this class down into WordPress too much”* – umm, what else would you be wanting to “tie it into”

  2. >However, I don’t want to tie this class down into WordPress too much, so I’d like to avoid having any action or filter hooks in its constructor.

    What? :/ If you are using wordpress then everything is tied into wordpress and most of WP is Actions, Filters, and Custom Hooks.

    I feel like you’ve gotten caught up with terminology without fully understanding what it means. PHP (which is what wordpress is) is not a fully object oriented language. Yes it has OOP support but that is to be able to write classes yourself, use inheritance etc. Which is already present in that code.

    Stackoverflow solutions are generally on point (when they are marked as the answer).

    There are plenty of solutions (ACF, PODS etc.) for [creating taxonomies](https://docs.pods.io/creating-editing-pods/creating-a-custom-taxonomy/) with your requirements (dependencies included). You can use the plugin or just use the UI to set up the taxonomy and generate the code to use independently with a few clicks.

    >Am I driving myself crazy over-engineering this?

    Yes this is a case where there is no need to reinvent the wheel.

  3. You seem to miss the most important factor:

    In your OOP example they USE a WP class, not modify it. (sure, they also restructure the returned data to suit their needs)

    You on the other hand want to modify the Tag labels. Hooks are specifically designed for this!

    Hooks exist so you can safely modify existing WP classes without changing the core code, this way you can safely update WordPress.

    Some extra thoughts:

    You can absolutely extend the WP_Taxonomy or WP_Post_Type class and use it in your own Plugin, but in your situation, it’s not the case, you’re looking to repurpose the tags to locations.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer