Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
possible duplicate of Redeclare function (override) –  Sven Mar 7 at 11:07

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
awesome, this has worked! –  MaxNagler Mar 7 at 10:56
    
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. –  Pieter Goosen Mar 7 at 10:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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