Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extending taxonomy #162

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace ExtCPTs;

use WP_Taxonomy;

class Taxonomy {

/**
Expand Down Expand Up @@ -196,8 +198,14 @@
add_filter( 'rewrite_testing_tests', [ $this, 'rewrite_testing_tests' ], 1 );
}

# Register taxonomy:
$this->register_taxonomy();
$existing = get_taxonomy( $this->taxonomy );

if ( empty( $existing ) ) {
# Register taxonomy:
$this->register_taxonomy();
} else {
$this->extend( $existing );
}

/**
* Fired when the extended taxonomy instance is set up.
Expand All @@ -209,6 +217,23 @@
do_action( "ext-taxos/{$this->taxonomy}/instance", $this );
}

/**
* Extends an existing taxonomy object. Currently only handles labels.
*
* @param WP_Taxonomy $taxonomy A taxonomy object.
*/
public function extend( WP_Taxonomy $taxonomy ) {

Check failure on line 225 in src/Taxonomy.php

View workflow job for this annotation

GitHub Actions / PHP / PHP 7.4 / PHP / PHP 7.4

Method ExtCPTs\Taxonomy::extend() has no return type specified.

Check failure on line 225 in src/Taxonomy.php

View workflow job for this annotation

GitHub Actions / PHP / PHP 8.3 / PHP / PHP 8.3

Method ExtCPTs\Taxonomy::extend() has no return type specified.
# Merge core with overridden labels
$this->args['labels'] = array_merge( (array) get_taxonomy_labels( $taxonomy ), $this->args['labels'] );

$GLOBALS['wp_taxonomies'][ $taxonomy->name ]->labels = (object) $this->args['labels'];

// Register taxonomy for object types
foreach ( $this->object_type as $object_type ) {
register_taxonomy_for_object_type( $this->taxonomy, $object_type );
}
}

/**
* Add our rewrite tests to the Rewrite Rule Testing tests array.
*
Expand Down
Loading