0

Background: I have a childtheme folder of my Wordpress theme which contains a functions.php. In this functions.php, at the beginning, I wrote code for setting up a custom post type that allows the user to add events to the site. Everything worked fine until today when in the same file I added code for a further separate custom post type that allows the user to add people.

Problem: After having added the second custom post type code, I cannot login to my site anymore.

Cause of the problem: Via the error logfiles of my domain provider I found out that my code for registering "place" as a category causes the problem. I tried to register this taxonomy via add_action('init', 'cpt_reg_tax');, followed by function cpt_reg_tax() { // code }; Now here comes what the error log file tells me: PHP Fatal error: Cannot redeclare cpt_reg_tax() (previously declared in ...

The thing is for the events custom post type at the beginning of functions.php I already registered a taxonomy. But why would PHP not allow me to register another taxonomy for another separate custom post type which comes later in the file?

How do I achieve that I can register a taxonomy for my second custom post type that I coded today without getting the redeclare-error again?

1

1 Answer 1

1

You cannot use the same function name more than once. Give the second function a different name:

add_action( 'wp_loaded', 'cpt_reg_tax1' );

function cpt_reg_tax1()
{
    // code for the first taxonomy here
}

add_action( 'wp_loaded', 'cpt_reg_tax2' );

function cpt_reg_tax2()
{
    // code for the second taxonomy here
}

Besides that, your code will terrible break for many users, because custom post types and taxonomies are plugin territory.

1
  • Just to add to @toscho, always use names for our functions that will be unique, something that is highly unlikely to be used by plugins. Commented Mar 7, 2014 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.