Reputation: 33
My goal is to inside a plugin pull all the posts/pages/custom post types in the project and create a custom taxonomy if it does not exist. Whenever the plugin goes inside of the if statement the php stops running on the page. As you can see in the foreach statement, I echo the post type name and the taxonomy associated with it.
I just want to create a custom taxonomy on a publicly available post type if the taxonomy does not exist.
I am able to run the taxonomy code that is inside the if statement inside the functions.php file so long as I replace the variables with the proper post types.
I also tried to instead of using add_action( 'init', 'custom_taxo_cpt_taxonomy', 1 ); just call the function directly via custom_taxo_cpt_taxonomy() on the same line as add_action( 'init', 'custom_taxo_cpt_taxonomy', 1 );
The echos/var_dumps are able to give me the post types and taxonomies associated without issue on the page so I know that is being pulled fine on the page.
<?php
$args = array(
'public' => true,
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
$cpt_taxo_ar = get_object_taxonomies($post_type);
$cpt_taxo_ar = array_shift($cpt_taxo_ar);
echo '<p>' . $post_type . ' category: ' . $cpt_taxo_ar . '</p>';
if($cpt_taxo_ar != $post_type . '_custom_taxo'){
echo $post_type . '_custom_taxo';
// Register CustomTaxo Tags Taxonomy
function custom_taxo_cpt_taxonomy() {
$labels = array(
'name' => _x( 'CustomTaxo Tags', 'CustomTaxo Tags', 'custom_taxo_domain' ),
'singular_name' => _x( 'CustomTaxo Tag', 'CustomTaxo Tag', 'custom_taxo_domain' ),
'menu_name' => __( 'CustomTaxo Tags', 'custom_taxo_domain' ),
'all_items' => __( 'All Tags', 'custom_taxo_domain' ),
'parent_item' => __( 'Parent Tag', 'custom_taxo_domain' ),
'parent_item_colon' => __( 'Parent Tag:', 'custom_taxo_domain' ),
'new_item_name' => __( 'New Tag Name', 'custom_taxo_domain' ),
'add_new_item' => __( 'Add New Tag', 'custom_taxo_domain' ),
'edit_item' => __( 'Edit Tag', 'custom_taxo_domain' ),
'update_item' => __( 'Update Tag', 'custom_taxo_domain' ),
'view_item' => __( 'View Tag', 'custom_taxo_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'custom_taxo_domain' ),
'add_or_remove_items' => __( 'Add or remove tags', 'custom_taxo_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'custom_taxo_domain' ),
'popular_items' => __( 'Popular tags', 'custom_taxo_domain' ),
'search_items' => __( 'Search tags', 'custom_taxo_domain' ),
'not_found' => __( 'Not Found', 'custom_taxo_domain' ),
'no_terms' => __( 'No items', 'custom_taxo_domain' ),
'items_list' => __( 'Tags list', 'custom_taxo_domain' ),
'items_list_navigation' => __( 'Tags list navigation', 'custom_taxo_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy( $post_type . '_custom_taxo', 'page', $args );
} // end taxo function
add_action( 'init', 'custom_taxo_cpt_taxonomy', 1 );
} //end for loop
}
?>
Upvotes: 3
Views: 254
Reputation: 2126
According to the documentation:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )`
To resolve the issue : "Cannot redeclare custom_taxo_cpt_taxonomy()"
custom_taxo_cpt_taxonomy
must be callable
so in a loop to avoid name declaration issue, anonymous function can be used like this:
$custom_taxo_cpt_taxonomy = function() { ... }
Finally anonymous doesn't work because WordPress use call_user_func_array
in class-wp-hook at apply_filters
and not something like $myCallback();
EDIT: Function not anonymous outside the loop
<?php
function custom_taxo_cpt_taxonomy($params) {
register_taxonomy( $params['post_type'] . '_custom_taxo', 'page', $params['args'] );
} // end taxo function
foreach ( $post_types as $post_type ) {
$cpt_taxo_ar = get_object_taxonomies($post_type);
$cpt_taxo_ar = array_shift($cpt_taxo_ar);
echo '<p>' . $post_type . ' category: ' . $cpt_taxo_ar . '</p>';
if($cpt_taxo_ar != $post_type . '_custom_taxo'){
echo $post_type . '_custom_taxo';
// Register CustomTaxo Tags Taxonomy
// function custom_taxo_cpt_taxonomy() { <= issue here
// $custom_taxo_cpt_taxonomy = function() { <= other issue
$labels = array(
'name' => _x( 'CustomTaxo Tags', 'CustomTaxo Tags', 'custom_taxo_domain' ),
'singular_name' => _x( 'CustomTaxo Tag', 'CustomTaxo Tag', 'custom_taxo_domain' ),
'menu_name' => __( 'CustomTaxo Tags', 'custom_taxo_domain' ),
'all_items' => __( 'All Tags', 'custom_taxo_domain' ),
'parent_item' => __( 'Parent Tag', 'custom_taxo_domain' ),
'parent_item_colon' => __( 'Parent Tag:', 'custom_taxo_domain' ),
'new_item_name' => __( 'New Tag Name', 'custom_taxo_domain' ),
'add_new_item' => __( 'Add New Tag', 'custom_taxo_domain' ),
'edit_item' => __( 'Edit Tag', 'custom_taxo_domain' ),
'update_item' => __( 'Update Tag', 'custom_taxo_domain' ),
'view_item' => __( 'View Tag', 'custom_taxo_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'custom_taxo_domain' ),
'add_or_remove_items' => __( 'Add or remove tags', 'custom_taxo_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'custom_taxo_domain' ),
'popular_items' => __( 'Popular tags', 'custom_taxo_domain' ),
'search_items' => __( 'Search tags', 'custom_taxo_domain' ),
'not_found' => __( 'Not Found', 'custom_taxo_domain' ),
'no_terms' => __( 'No items', 'custom_taxo_domain' ),
'items_list' => __( 'Tags list', 'custom_taxo_domain' ),
'items_list_navigation' => __( 'Tags list navigation', 'custom_taxo_domain' )
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true
);
$params = array(
'post_type' => $post_type,
'args' => $args
);
// Pass callable not anonymous function
add_action( 'init', array('custom_taxo_cpt_taxonomy', $params), 1);
} //end for loop
}
?>
EDIT : IMPORTANT
So the code bellow doesn't work: syntax error, unexpected 'add_action' (T_STRING).
<?php
$args = array(
'public' => true
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
$cpt_taxo_ar = get_object_taxonomies($post_type);
$cpt_taxo_ar = array_shift($cpt_taxo_ar);
echo '<p>' . $post_type . ' category: ' . $cpt_taxo_ar . '</p>';
if($cpt_taxo_ar != $post_type . '_custom_taxo'){
echo $post_type . '_custom_taxo';
// Register CustomTaxo Tags Taxonomy
// function custom_taxo_cpt_taxonomy() { <= issue here
// Create anonymous function
$custom_taxo_cpt_taxonomy = function() {
$labels = array(
'name' => _x( 'CustomTaxo Tags', 'CustomTaxo Tags', 'custom_taxo_domain' ),
'singular_name' => _x( 'CustomTaxo Tag', 'CustomTaxo Tag', 'custom_taxo_domain' ),
'menu_name' => __( 'CustomTaxo Tags', 'custom_taxo_domain' ),
'all_items' => __( 'All Tags', 'custom_taxo_domain' ),
'parent_item' => __( 'Parent Tag', 'custom_taxo_domain' ),
'parent_item_colon' => __( 'Parent Tag:', 'custom_taxo_domain' ),
'new_item_name' => __( 'New Tag Name', 'custom_taxo_domain' ),
'add_new_item' => __( 'Add New Tag', 'custom_taxo_domain' ),
'edit_item' => __( 'Edit Tag', 'custom_taxo_domain' ),
'update_item' => __( 'Update Tag', 'custom_taxo_domain' ),
'view_item' => __( 'View Tag', 'custom_taxo_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'custom_taxo_domain' ),
'add_or_remove_items' => __( 'Add or remove tags', 'custom_taxo_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'custom_taxo_domain' ),
'popular_items' => __( 'Popular tags', 'custom_taxo_domain' ),
'search_items' => __( 'Search tags', 'custom_taxo_domain' ),
'not_found' => __( 'Not Found', 'custom_taxo_domain' ),
'no_terms' => __( 'No items', 'custom_taxo_domain' ),
'items_list' => __( 'Tags list', 'custom_taxo_domain' ),
'items_list_navigation' => __( 'Tags list navigation', 'custom_taxo_domain' )
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true
);
register_taxonomy( $post_type . '_custom_taxo', 'page', $args );
} // end taxo function
// Pass callable anonymous function
add_action( 'init', $custom_taxo_cpt_taxonomy, 1 );
} //end for loop
}
?>
,
in the end of arrays creates an extra empty field.
Upvotes: 0