Saturday, November 25, 2023
HomeMobile MarketingWordPress: Dynamically Embrace JavaScript or PHP Utilizing the Publish ID

WordPress: Dynamically Embrace JavaScript or PHP Utilizing the Publish ID


One of many efforts that I’ve actually been engaged on this 12 months on Martech Zone is offering some easy internet purposes which are useful to our guests. There’s some primary improvement behind these that embrace each PHP and JavaScript (largely jQuery).

With WordPress, there’s not a extremely handy technique 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 wish to gradual 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 features.php of my little one theme and utilizing is_single with the submit ID quantity. After fairly a couple of apps, although, my features.php file started to get fairly unruly.

A nifty resolution 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 submit ID, it consists of 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 situation!

Embrace JavaScript or PHP File on Publish ID

Right here’s the good resolution that I got here up with. I’d add that I received some help from ChatGPT on this resolution, too! One other difficulty with this was that I didn’t wish to execute the perform with each single customer hitting each single web page or submit on the location, so I take advantage of WordPress’ transient technique to really cache the leads to the database… on this case for 1 hour (3600 seconds). I could change it to as soon as a day ultimately, however an hour is nice for now.

perform include_app_file() {
    // Verify if it is a single submit
    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 submit 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, embrace 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, embrace it
            embrace($apps_dir . $php_file_name);
        }
    }
}
add_action('wp_head', 'include_app_file');

Now I don’t even have to the touch my features.php file and my JavaScript and PHP features are neatly organized in my apps listing! I’m not completed migrating all of the apps simply but… however I might be quickly after which I’ll have the ability to quickly develop extra apps with quite a bit much less effort.

In the event you’d like to make use of this strategy, all you must do is add this code to your little one theme’s features.php file after which add a listing known as app to your little one theme folder. As you’re creating your web page the place you wish to embrace a particular javascript file or PHP file, you simply add these information with the Publish ID quantity because the title.

For example, 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 submit ID is 123884, so I added the file 123884.js to my apps listing, pasted my code, and I used to be up and operating!

If you wish to use this code, go for it… I’d simply respect some credit score or maybe you’ll be able to ship a tip my manner!

Ship Douglas Karr a Tip!

Disclosure: Martech Zone is utilizing its affiliate hyperlink for WordPress on this article.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments