In this members-only tutorial, we share a simplified blueprint/template for dynamically fetching/loading any piece of content (PHP code) when a link/button is clicked i.e., on-demand in WordPress.
Here's what happens behind the scenes in a nutshell:
- User clicks on the button
- Our custom JavaScript code executes
ajax()
function while passing it a) the URL to the file in WordPress AJAX API responsible for handling AJAX requests and b) name of an "action" - Request gets sent to
admin-ajax.php
admin-ajax.php
looks at the registered action and runs the function hooked to this action- The response gets sent back to JavaScript and we display that on the front end
After implementing the steps in this tutorial and seeing them work, you should be able to easily duplicate/extend this for your own specific requirements.
Step 1
Install and activate My Custom Functionality plugin.
Connect to your hosting account using a FTP client and navigate to site's /wp-content/plugins/my-custom-functionality-master
.
Create a file named say, ajax-blueprint.js
in the plugin's assets/js
directory.
Edit plugin.php
and
a) add the following inside the custom_enqueue_files()
function:
This is a premium members-only content.
To view the rest of the content, please sign up for membership ($47/month or $599 one-time).
Already a member? Log in below or here.