$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How can I re-use PHP code across different pages?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How can I re-use PHP code across different pages?

matteradmin8PV0评论

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?

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?

Share Improve this question asked Aug 7, 2018 at 19:49 ellenellen 3451 gold badge5 silver badges16 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default -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.

Post a comment

comment list (0)

  1. No comments so far