Reputation: 11
I have a public function to attach taxonomies to custom post types. I am getting this error code in Wordpress when debugging is set to true:
Notice: Undefined offset: 0 in C:\xampp\htdocs\yanjep-dev\wp-includes\plugin.php on line 925
Notice: Undefined offset: 0 in C:\xampp\htdocs\yanjep-dev\wp-includes\plugin.php on line 943
This repeats 9 times each.
I have isolated the problem to an add_action function here:
add_action( 'init',
call_user_func_array( 'register_taxonomy', array( $taxonomy_name, $post_type_name, $args ) )
);
The entire function is here:
public function add_taxonomy( $name, $args = array(), $labels = array() )
{
if( ! empty( $name ) )
{
// We need to know the post type name, so the new taxonomy can be attached to it.
$post_type_name = $this->post_type_name;
// Taxonomy properties
$taxonomy_name = strtolower( str_replace( ' ', '_', $name ) );
$taxonomy_labels = $labels;
$taxonomy_args = $args;
if( ! taxonomy_exists( $taxonomy_name ) )
{
//Capitilize the words and make it plural
$name = ucwords( str_replace( '_', ' ', $name ) );
$plural = $name . 's';
// Default labels, overwrite them with the given labels.
$labels = array_merge(
// Default
array(
'name' => _x( $plural, 'taxonomy general name' ),
'singular_name' => _x( $name, 'taxonomy singular name' ),
'search_items' => __( 'Search ' . $plural ),
'all_items' => __( 'All ' . $plural ),
'parent_item' => __( 'Parent ' . $name ),
'parent_item_colon' => __( 'Parent ' . $name . ':' ),
'edit_item' => __( 'Edit ' . $name ),
'update_item' => __( 'Update ' . $name ),
'add_new_item' => __( 'Add New ' . $name ),
'new_item_name' => __( 'New ' . $name . ' Name' ),
'menu_name' => __( $name ),
),
// Given labels
$taxonomy_labels
);
// Default arguments, overwitten with the given arguments
$args = array_merge(
// Default
array(
'label' => $plural,
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'_builtin' => false,
),
// Given
$taxonomy_args
);
// Add the taxonomy to the post type
add_action( 'init',
call_user_func_array( 'register_taxonomy', array( $taxonomy_name, $post_type_name, $args ) )
);
}
else
{
add_action( 'init', array($this, 'add_taxonomy_to_post_type'));
}
}
}
I am working locally using Xampp. If I comment out the action, the notices disappear. I know it has something to to with the number of arguments, but I can't solve it.
Any help is appreciated.
Upvotes: 1
Views: 1435
Reputation: 26170
As you know, add_action requires two arguments: the filter (register_taxonomy
, and the function name
to call.
AFAIK, add_action
needs to reference an actual function, and you cannot pass in a call_user_func_array
call. If you trace the code in WP, you'll see that add_action
in turn calls add_filter
, which in turn calls _wp_filter_build_unique_id
, which is where your error is being thrown. If you investigate that code, you'll see it's expecting either a string - as in the function name to call (such as register_my_taxonomy
), or else an array for class methods (following the PHP standard way). Examples:
add_action('init', 'register_my_taxonomy'); // Call a standard function
add_action('init', array($this, 'register_my_taxonomy'); // call a public class method
add_action('init', array(__CLASS__, 'register_my_taxonomy'); // call a public static method
So, since it appears you are working in a class, in your add_action('init'...)
call, I'd recommend changing it to reference a class function:
add_action( 'init',
array($this, 'register_my_taxonomy')
);
And in your class, create the function register_my_taxonomy
which loops over your taxonomies and calls register_taxonomy
directly.
Upvotes: 1