0

I would like to re-use some PHP classes and functions that I have written to display tables.

Right now, I am using XYZ PHP Code to display tables on multiple pages. In each snippet, I am defining a table class and related functions. I believe it would be much cleaner if this table class was defined only once.

How do I do this properly?

2
  • are you aware of functions.php, and PHP includes?
    – Tom J Nowell
    Commented Aug 7, 2018 at 20:18
  • Put files wherever you would like and include or require them from functions.php which is the main file in your theme that WordPress automatically includes on every script execution. All of WordPress and all other proper php code uses includes extensively to avoid declaring classes or functions twice.
    – Joel M
    Commented Jan 2, 2020 at 3:42

1 Answer 1

-1

You're looking for something similar to PHPs require or include function.

In WordPress we can use get_template_part( 'example-folder/template' ); to get template.php from the example-folder.

You should also look into the get_template_part function to see the capabilities. If you're wanting to include outside variables into these, you can use PHP globals.

2
  • You should not use get_template_part for all includes. Only use it for template files inside of the templates folder in your theme and even in that case i would suggest you may as well just use a regular old include.
    – Joel M
    Commented Jan 2, 2020 at 3:40
  • template-parts folder **
    – Joel M
    Commented Jan 2, 2020 at 3:43

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.