Search code examples
phpwordpressfunctionpostenqueue

How do I enqueue or activate a page of code in WordPress?


I am developing a WordPress custom-theme where I would like to register my Custom Post Type and Taxonomy upon theme install, I have all the generated cpt-code inside my ~custom-theme/custom/posttypes.php and taxonomy.php See it here on Pastebin.

I am very new a PHP but know that I must put some add_action or enqueue function inside my functions.php file so this post type will get registered on theme activation.

The function name in this example for the PostType is function cptui_register_my_cpts_testimonials() and for the Taxonomy is function cptui_register_my_taxes_client-types()

The reason I don't want all this code inside function.php is it will get too long, so hoping for a way to just fire this with a line or two of code from my functions.php file.


Solution

  • This looks merely as a question how you would like to organize the code across files to me.

    In PHP this can be done by requiring files. For example in functions.php as it is always loaded by Wordpress with the theme, you can require the custom post-types and taxonomy (just showing the beginning of the file exemplary):

    <?php declare(strict_types=1);
    
    /*
     * my theme - functions.php
     */
    
    require_once __DIR__ . '/custom/posttypes.php';
    require_once __DIR__ . '/custom/taxonomy.php';
    
    ...
    

    Commonly such requires (sometimes called require lines) are towards the very top of the file so that it is clear on which other files it depends (and what is available to write code with) immediately.


    References: