Skip to content

Commit

Permalink
fix error with registering taxonomies fixes #14
Browse files Browse the repository at this point in the history
moving the register_taxonomy action into the class construct allows taxonomies to be registered across multiple post types
  • Loading branch information
jjgrainger committed Jun 6, 2014
1 parent 3c0d2ef commit 16eddce
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/CPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class CPT {
public $taxonomies;


/*
@var array $taxonomy_settings
public variable that holds an array of the taxonomies associated with the post type and their options
used when registering the taxonomies
assigined on register_taxonomy()
*/

public $taxonomy_settings;



/*
@var array $filters
Expand Down Expand Up @@ -117,7 +127,7 @@ class CPT {
/*
@function __contructor(@post_type_name, @options)
@param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singlualr)
@param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singluar)
@param array $options User submitted options
*/

Expand Down Expand Up @@ -181,11 +191,18 @@ function __construct($post_type_names, $options = array()) {
// register the post type
$this->add_action('init', array(&$this, 'register_post_type'));

// register taxonomies
$this->add_action('init', array(&$this, 'register_taxonomies'));

// add taxonomy to admin edit columns
$this->add_filter('manage_edit-' . $this->post_type_name . '_columns', array(&$this, 'add_admin_columns'));

// populate the taxonomy columns with the posts terms
$this->add_action('manage_' . $this->post_type_name . '_posts_custom_column', array(&$this, 'populate_admin_columns'), 10, 2);

// add filter select option to admin edit
$this->add_action('restrict_manage_posts', array(&$this, 'add_taxonomy_filters'));

}


Expand Down Expand Up @@ -584,36 +601,43 @@ function register_taxonomy($taxonomy_names, $options = array()) {
// merge default options with user submitted options
$options = $this->options_merge($defaults, $options);

// register the taxonomy if it doesn't exist
if(!taxonomy_exists($taxonomy_name)) {
// add the taxonomy to the object array
// this is used to add columns and filters to admin pannel
$this->taxonomies[] = $taxonomy_name;

// register the taxonomy with Wordpress
$this->add_action('init', function() use($taxonomy_name, $post_type, $options) {
register_taxonomy($taxonomy_name, $post_type, $options);
});
// create array used when registering taxonomies
$this->taxonomy_settings[$taxonomy_name] = $options;

}

} else {

// if taxonomy exists, attach exisiting taxonomy to post type
$this->add_action('init', function() use($taxonomy_name, $post_type) {
register_taxonomy_for_object_type($taxonomy_name, $post_type);
});

}
/*
function register_taxonomies
cycles through taxonomies added with the class and registers them
// add the taxonomy to the object array
// this is used to add columns and filters to admin pannel
$this->taxonomies[] = $taxonomy_name;
function is used with add_action
*/
function register_taxonomies() {

// add taxonomy to admin edit columns
$this->add_filter('manage_edit-' . $post_type . '_columns', array(&$this, 'add_admin_columns'));
// foreach taxonomy registered with the post type
foreach($this->taxonomy_settings as $taxonomy_name => $options) {

// populate the taxonomy columns with the posts terms
$this->add_action('manage_' . $post_type . '_posts_custom_column', array(&$this, 'populate_admin_columns'), 10, 2);
// register the taxonomy if it doesn't exist
if(!taxonomy_exists($taxonomy_name)) {

// register the taxonomy with Wordpress
register_taxonomy($taxonomy_name, $this->post_type_name, $options);


} else {

// if taxonomy exists, attach exisiting taxonomy to post type
register_taxonomy_for_object_type($taxonomy_name, $this->post_type_name);

}
}

// add filter select option to admin edit
$this->add_action('restrict_manage_posts', array(&$this, 'add_taxonomy_filters'));

}

Expand Down Expand Up @@ -1099,4 +1123,4 @@ function menu_icon($icon = "dashicons-admin-page") {

}

}
}

0 comments on commit 16eddce

Please sign in to comment.