Friday, February 24, 2023
HomeMobile MarketingWordPress: Dynamically Embrace JavaScript or PHP Utilizing the Submit ID

WordPress: Dynamically Embrace JavaScript or PHP Utilizing the Submit ID


One of many efforts that I’ve actually been engaged on this yr on Martech Zone is offering some easy net purposes which might be useful to our guests. There’s some easy growth behind these that embody each PHP and JavaScript (principally jQuery).

With WordPress, there’s not a extremely easy solution to write pages or posts with particular code that you really want within the header, although. I don’t need the code site-wide and don’t wish to gradual my website down with a big script file.

After 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 number of apps, although, my capabilities.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 publish ID, it consists of the JavaScript and/or PHP file primarily based 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!

Embrace JavaScript or PHP File on Submit ID

Right here’s the good resolution that I got here up with. I’d add that I acquired 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 publish on the positioning, so I exploit WordPress’ transient methodology to really cache the ends in the database… on this case for 1 hour (3600 seconds). I’ll change it to as soon as a day ultimately, however an hour is sweet for now.

perform include_app_file() {
    // Test 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 primarily based on the publish ID
        $js_file_name = get_the_ID() . '.js';
        $php_file_name = get_the_ID() . '.php';
        
        // Test 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);
        }
        
        // Test 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 completed migrating all of the apps simply but… however I can be quickly after which I’ll be capable of quickly develop extra apps with lots much less effort.

If you happen to’d like to make use of this method, all it’s important to do is add this code to your little one theme’s capabilities.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 embody a particular javascript file or PHP file, you simply add these information with the Submit 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 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 operating!

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

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

- Advertisment -
Google search engine

Most Popular

Recent Comments