One of many efforts that I’ve actually been engaged on this yr on Martech Zone is offering some easy internet purposes which are useful to our guests. There’s some primary growth behind these that embody each PHP and JavaScript (largely jQuery).
With WordPress, there’s not a extremely handy strategy to write pages or posts with the precise code that you really want within the header, although. I don’t need the code site-wide and don’t need to sluggish my web site down with a huge script file.
Once I first began writing the apps, I used to be doing all of it in capabilities.php of my little one theme and utilizing is_single
with the publish ID quantity. After fairly a couple of apps, although, my capabilities.php file started to get fairly unruly.
A nifty answer that I got here up with utilizing the WordPress API was so as to add a apps
listing to my little one theme whose contents are learn. When the filename matches the publish ID, it contains the JavaScript and/or PHP file based mostly on the file’s extension. A few of my apps have customized PHP, some simply JavaScript, and a few have each. This script works for any state of affairs!
Embody JavaScript or PHP File on Submit ID
Right here’s the great answer that I got here up with. I’d add that I acquired some help from ChatGPT on this answer, too! One other subject with this was that I didn’t need to execute the perform with each single customer hitting each single web page or publish on the positioning, so I take advantage of WordPress’ transient technique to truly cache the ends in the database… on this case for 1 hour (3600 seconds). I’ll change it to as soon as a day finally, however an hour is sweet for now.
perform include_app_file() {
// Verify if it is a single publish
if (is_single()) {
// Get the file path of the "apps" subdirectory from the transient cache
$apps_dir = get_transient('apps_dir');
// If the cache is empty, get the file path and retailer it within the cache
if (false === $apps_dir) {
$apps_dir = get_stylesheet_directory() . '/apps/';
set_transient('apps_dir', $apps_dir, 3600);
}
// Assemble the file names based mostly on the publish ID
$js_file_name = get_the_ID() . '.js';
$php_file_name = get_the_ID() . '.php';
// Verify if the JS file exists
if (file_exists($apps_dir . $js_file_name)) {
// If the JS file exists, embody it within the head part of the web page
wp_enqueue_script(get_the_ID(), get_stylesheet_directory_uri() . '/apps/' . $js_file_name, array(), null, true);
}
// Verify if the PHP file exists
if (file_exists($apps_dir . $php_file_name)) {
// If the PHP file exists, embody it
embody($apps_dir . $php_file_name);
}
}
}
add_action('wp_head', 'include_app_file');
Now I don’t even have to the touch my capabilities.php file and my JavaScript and PHP capabilities are neatly organized in my apps
listing! I’m not performed migrating all of the apps simply but… however I will probably be quickly after which I’ll be capable to quickly develop extra apps with loads much less effort.
If you happen to’d like to make use of this method, all you must do is add this code to your little one theme’s capabilities.php file after which add a listing referred to as app
to your little one theme folder. As you’re creating your web page the place you need to embody a particular javascript file or PHP file, you simply add these information with the Submit ID quantity because the title.
For instance, I’ve an app that converts rows to CSV or CSV to rows. This particular app makes use of JavaScript (and jQuery) solely, so I simply added a file to the apps
listing. The publish ID is 123884, so I added the file 123884.js
to my apps listing, pasted my code, and I used to be up and working!
If you wish to use this code, go for it… I’d simply recognize some credit score or maybe you may ship a tip my approach!
Disclosure: Martech Zone is utilizing its affiliate hyperlink for WordPress on this article.