1

I would find best practices in performance, clean code and child theme ready, for plugin that add some custom post types to my theme.

What di you think about something like this?

if ( ! function_exists( 'custom_taxonomy_1' ) ) {

    function custom_taxonomy_1() {

        $args = array(
            ...
        );
        register_taxonomy( 'custom_taxonomy_1', array( 'custom_post_type_1' ), $args );
    }
}

if ( ! function_exists( 'custom_post_type_1' ) ) {

    function custom_post_type_1() {

        $args = array(
            ...
        );
         register_post_type( 'custom_post_type_1', $args );
    }
}

if ( ! function_exists( 'custom_post_type_2' ) ) {

    function custom_post_type_2() {

        $args = array(
            ...
        );
        register_post_type( 'custom_post_type_2', $args );
    }
}



if ( ! function_exists('my_init_register_types') ) {
    function my_init_register_types(){
        custom_taxonomy_1();
        custom_post_type_1();
        custom_post_type_2();
    }

    add_action( 'init', 'my_init_register_types' );
}

1 Answer 1

1

A few thoughts on your code

  • The conditional function_exists() actually will never work because plugins are loaded first, then child theme functions then parent theme functions. You can just drop these conditional checks. The function_exists() function is basically meant for parent themes and allows for a function to be overridden by plugins or child themes

  • Register all taxonomies and custom post types in one function and then hook that one function to init. It just makes more sense than having 10 functions doing almost the same exact thing hooked to init

  • Prefix function names in a plugin with the plugin name, this will ensure uniqueness and will avoid fatal errors in future. The example names in your question is easy to reproduce which will break the site should any other plugin use the same name.

  • As this is a plugin, I would just use a closure which will eliminate the need for any unique function names. Example:

    add_action( 'init', function () 
    {
        // Your code to register taxonomies and custom post types here
    });
    
2
  • Thank you for you response and especially thanks for my error with function_exist. About names, they was only example names. Last question about functions number, if there is only one init, you don't think that have a function for each type or taxonomy and call temm in init is more clear?
    – Antonio
    Sep 8, 2015 at 12:22
  • There is no set rule that states each cpt and each taxonomy should be defined in separate functions, neither are there any rules saying they all need to be in the same function. It is all up to you how you want to do it. Readability and maintainability of all code is very important though, so I always try to group my code and make it as short and easy as possible and I tend to stick cpt's and taxonomies in one function. Do what fit you best here and with what you are comfortable with. ;-) Sep 8, 2015 at 12:32

Your Answer

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

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