Reputation: 2215
i am writing a wordpress plugin. In order to decrease page load time i decided to go with AJAX.So what i mean is when the page loads an ajax must be triggered and the content must be loaded by a request. My question is that i am not writing a theme,so how can i do this with a plugin?
Upvotes: 0
Views: 2555
Reputation: 6308
You can use the wp_enqueue_script to load your js into the page and do the ajax request from that js file. Here we are using wp_head hook to place it into the <head>
section of your theme.
add_action('wp_head', load_my_scripts);
function load_my_scripts() {
wp_enqueue_script(
'my-js-file',
plugins_url('/js/my-js-file.js', __FILE__),
array('jquery')
);
}
Also if you are developing a custom theme then be sure to have <?php wp_head(); ?>
before closing you </head>
tag in your header.php
PS: I'm assuming you are using jQuery for you ajax requests so I included that library in the code as prerequisite, so Wordpress will include that library before your js file.
Upvotes: 3