hey guys, weird situation... I'm trying to use wp_ajax() for the first time. I'm normally using a normal jQuery ajax request but in my current case I get a lot of bugs so I thought about trying wp_ajax().
I don't get it though!
The following piece of code...
// embed the javascript file that makes the AJAX request
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js' );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
ends in...
<script type="text/javascript" src="http://example.com/wordpress/wp-content/plugins/myajax/js/ajax.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
var MyAjax = {
ajaxurl: "http://example.com/wordpress/wp-admin/admin-ajax.php"
};
/* ]]> */
However I don't have a plugin or anything that should use this, but my whole page has to do this ajax request. Therefore the entire first line with wp_enqueue_script()
doesn't make sens. I don't need to load a specific js-file for this because I have already my entire script.js
file manually embedded in my <head>
section. This is where the ajax request gets fired. However once I leave this line out (//wp_enqueue_script()...
) the second part doesn't work.
So there will no
<script type="text/javascript">
/* <![CDATA[ */
var MyAjax = {
ajaxurl: "http://example.com/wordpress/wp-admin/admin-ajax.php"
};
/* ]]> */
be printed into my section of my page. What am I getting wrong here? I really need to be able to fire a ajax request from within my normal script.js file.
Any ideas?
update: my script.js file (manually emebedded in my should call a an ajax request:
var response;
$.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : 'wp_ajax_nopriv_searchmap',
},
function( response ) {
$sr.html(response);
the function in my functions.php file I want to call looks like this:
// embed the javascript file that makes the AJAX request
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/ajax.js' );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
// this hook is fired if the current viewer is not logged in
if (isset($_GET['action'])) {
do_action( 'wp_ajax_nopriv_' . $_GET['action'] );
}
// if logged in:
if (isset($_POST['action'])) {
do_action( 'wp_ajax_' . $_POST['action'] );
}
if(is_admin()) {
add_action( 'wp_ajax_searchmap', 'my_searchmap_function' );
} else {
add_action( 'wp_ajax_nopriv_searchmap', 'my_searchmap_function' );
}
function my_searchmap_function() {
// Start output buffer
ob_start();
?>
div>
<h3>Pages</h3>
<ul>
<?php wp_list_pages('title_li=&depth=0&exclude='); ?>
</ul>
<h3>Posts</h3>
<?php $first = 0;?>
<ul>
<?php
$myposts = get_posts('numberposts=-1&offset=$first');
foreach($myposts as $post) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<h3>Categories</h3>
<ul>
<?php wp_list_categories('title_li=&orderby=name'); ?>
</ul>
</div>
<?php
$output = ob_get_contents();
// Stop output buffer
ob_end_clean();
$response = json_encode($output);
// response output
header( "Content-Type: application/json" );
echo $response;
// IMPORTANT: don't forget to "exit"
exit;
}
What do I missunderstand here? I simply want to be able to request data via ajax from within my normal javascript file. I need to process the html that comes back in my javascript file.
Any idea what I'm doing wrong or better, what I have to do in order to make this work?