1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
ShrockCo
  • 357
  • 1
  • 13
  • What is your question? How to split a large PHP file? – hakre Jul 17 '21 at 21:46
  • for some reason I was think my function would not fire if its in `~custom-theme/custom/posttypes.php` and not somehow linked to the functions.php page? – ShrockCo Jul 17 '21 at 21:59

1 Answers1

1

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:


hakre
  • 193,403
  • 52
  • 435
  • 836
  • Perfect, That is exactly what I was looking for!! Although for some reason the `declare(strict_types=1);` just ran into an error: `~Fatal error: strict_types declaration must be the very first statement ....` was I supposed to put that in someplace else? – ShrockCo Jul 18 '21 at 01:06
  • 1
    @ShrockCo: It must be at the beginning of the file, see as well https://stackoverflow.com/q/48723637/367456 which I added. – hakre Jul 18 '21 at 06:42
  • Okay so you mean that would go at the top of my `~custom-theme/custom/posttypes.php` and `taxonomy.php` files? From php manual it would look like I need to wrap my code inside the curly brackets then? `` – ShrockCo Jul 20 '21 at 12:17
  • 1
    no idea where you found an example for [`declare()`](https://php.net/declare) with curly brackets ... they are not required and I wonder why someone put them there (they probably don't hurt, but look entirely superfluous). – hakre Jul 20 '21 at 14:08
  • I got it from [php.net/declare](https://www.php.net/declare) but good to know that the brackets aren't required then. – ShrockCo Jul 20 '21 at 18:17
  • 1
    Ah now I see it in there. Don't use the curly brackets (`{`, `}`) with `declare(strict_types=1);`. This was specifically mentioned in the documentation for `declare(ticks=1)` and does _**not**_ apply with `declare(strict_types=1)`. See also the code in my answer above, it does not have it and ` – hakre Jul 21 '21 at 12:23
  • Thank-you again for your enlightenment! Would you mind sharing what exactly `declare(strict_types=1);` really does? – ShrockCo Jul 21 '21 at 22:53
  • 1
    @ShrockCo: No idea what your background is. The _short_ version is that it _enables strict type checking for scalar types in PHP_ (since PHP 7.0, see [Docs](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict)). Let me know if this is already useful and perhaps if on Stackoverflow the info appears lost or if on PHP.net it can be improved. Strict comparisons on scalar types are normally pretty useful to divide between request processing code (the basic input) and your own handling code (e.g. in a functions.php in Wordpress). It works per file. – hakre Jul 21 '21 at 23:02