Run a tools file in Concrete5 via ajax

In Concrete5 we can place php files in the tools directory, which we can run with ajax. This means we can load that php file without refreshing the page on page load, or when we click a link or button etc.

First of all we add our tools file under /application/tools.  For testing we can just add a log entry:

defined('C5_EXECUTE') or die("Access Denied.");

Log::AddEntry('Hello World');

Save this as test_tool.php.

Calling our tools file via ajax:

In the page or file where we want to call the tools file, we need to add the path to the tool at the top:

$tools_url = Loader::helper('concrete/urls')->getToolsURL('test_tool');

Note how you don't add .php to the end of the file and you don't put the whole path - Concrete5 knows what to do with just the above.

Then in our file we can add the ajax like so:

            url: "<?php echo $tools_url ?>", 
            context: document.body            
        }).done(function() {
            console.log('ajax complete');

Note, this will trigger the tools file on page load.

All you then need to do is refresh the page and check the logs and you should see your message from your tools file!

Who are we?

We are a digital agency specialising in Web Design, Development, Concrete5 and digital marketing, based in London & West Sussex.

We make digital simple. Our purpose is to simplify your frustrations in digital and solve the challenges you face to help make you more money and progressively grow your business or organisation.

Tell me more

Keep up to date

June special offer...

FREE hosting in year one

We'll give you FREE hosting when you buy a website from us. The hosting includes full 24x7 UK support, FREE SSL cert, FREE domain, daily backups and is optimised for high levels of speed and security. Worth up to £480 a year!

Tell me more
Call us