4

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.com/PlayerNguyen/OptEco/tree/master/web_integration/api

Here is what goes in the webpage: https://github.com/PlayerNguyen/OptEco/blob/master/web_integration/example_top.php

Here is the file tree:

File tree

2

2 Answers 2

6

The 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.org/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

  1. Register a shortcode
  2. 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.
  3. 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.

1
  • 1
    Plugins are the preferred and optimal way to add functionality to WordPress. Doing it by calling obscured, unregistered and undocumented code may lead to severe migraines...
    – Henrique
    Commented Apr 13, 2020 at 19:22
1

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.

2
  • A child theme will still suffer with the issue of losing functionality whren changing themes. Putting custom code in a plugin will make it easier to switch themes in the future. Commented Apr 21, 2020 at 9:39
  • i ended up just putting it directly into the theme as i never plan on changing themes. thanks. its working great now. i also loaded one of the files into the snipets plugin, so perhaps in the future i might load it all there. thanks.
    – super7800
    Commented Apr 23, 2020 at 17:15

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.