Support » Plugins » Hacks » How do I specify the URL in an Ajax call?

  • I am trying to use jQuery.post() in a page on the front end of WordPress. The first argument is the URL of the php file to which the post request is sent. Outside of WordPress, I simply enter the relative URL of the php file. Inside WordPress, that does not work (because of Apache mod_rewrite?). How do I specify the URL of the php file from within WordPress?

    Following is the code in question. The post request never gets to file testpost.php.

    jQuery(document).ready(function() {
    
    	jQuery( "#test-post" )
    		.button()
    		.click(function() {
    		//	alert("here");
    		        jQuery.post( "testpost.php", function(data){
    	   			alert(data);
    	    		     });
    		});
    
    });

Viewing 2 replies - 1 through 2 (of 2 total)
  • Couldn’t you set this URL as a global JS variable and just call that. Just make sure you set it with the absolute path using something like

    bloginfo(‘template_url’) . ‘/testpost.php’

    If your JS code is inside of a PHP file there is no need to set a global variable just do something like this

    jQuery.post( "<?php echo bloginfo('template_url') . '/testpost.php'; ?>", function(data){ ...

    Nice! It worked. I created a global javascript variable in my child theme functions.php file as follows:

    function civil_addto_header() { ?>
    <script>testpost_url = '<?php echo bloginfo('stylesheet_directory') . '/testpost.php'; ?>';</script>
    <?php }
    
    add_action('wp_head', 'civil_addto_header');

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I specify the URL in an Ajax call?’ is closed to new replies.