To briefly state my background I am not a web developer, but an electrical engineer.
The key question I have is where to put the PHP library in the file tree so the code embedded into the webpage can access it. To those in the know this must be trivial, but I cannot find any information about this online.
Here is the library:
Here is what goes in the webpage: .php
Here is the file tree:
To briefly state my background I am not a web developer, but an electrical engineer.
The key question I have is where to put the PHP library in the file tree so the code embedded into the webpage can access it. To those in the know this must be trivial, but I cannot find any information about this online.
Here is the library: https://github/PlayerNguyen/OptEco/tree/master/web_integration/api
Here is what goes in the webpage: https://github/PlayerNguyen/OptEco/blob/master/web_integration/example_top.php
Here is the file tree:
Share Improve this question edited Apr 13, 2020 at 18:52 Peter Mortensen 2682 silver badges10 bronze badges asked Apr 13, 2020 at 5:33 super7800super7800 411 silver badge3 bronze badges 2- This is a FAQ, and there is actually documentation about this on the main WordPress site (though it doesn't seem to work well, seems to broken to some extent, and some of the instructions don't work with the current version of WordPress). And a central Stack Overflow question is How can I integrate PHP code into a WordPress page?. With some themes (e.g., some of the official ones) you will get an annoying "Page not found". – Peter Mortensen Commented Apr 13, 2020 at 15:24
- I have used it in my service. GitHub. – Peter Mortensen Commented May 4, 2020 at 11:39
2 Answers
Reset to default 6The best way would be to create a custom plugin for your site. Take a look at the plugin handbook to get started https://developer.wordpress/plugins/
In the plugin, you can just put the libraries in a folder, and include them in your main plugin code. Your plugin can then do one of these
- Register a shortcode
- Create a custom template tag, which would be just a function that outputs your content, and can be used in any other php file in your site.
- Create a block for the new block editor
Technically it's possible to just put this in your theme, but it's advised to put any custom code like this in a plugin for better maintenance, and ease of changing themes in the future.
Writing all custom code as a plugin is a best practice, but it's really not needed in many circumstances.
You can create a folder anywhere in the folder that contains the folders and files for your theme or its child theme and keep your library's code there. That's going to be somewhere under the wp-content folder.
Note: even though we can put our own code into the theme's folders it is a best practice to always use a child theme for our code whether our code is a plugin or not.
Then once in a child-theme, you need to learn how to "enqueue" the PHP library so it can be loaded into memory and used by your code that calls on its functionality. That's done in the functions.php file that must be in the root of every theme or child theme folder.
That's why IMO it can be rather specious to argue against keeping assets in the theme/child theme, because we have to go there to write the code to use the library anyway and if you need portability you can use that thing called copy and paste out of your child theme into whatever you may need in the future. Furthermore, the maintenance is not affected at all. It's like saying I'm smart enough to write my code, but I don't know where it is when I may need to maintain it.
If you have the income I recommend buying into some training from Tonya Mork because the question asked is not so easy to understand by those with no coding skills yet and her instructional style is top shelf.