views:

45

answers:

2

Hi, I am developing a plugin and have a quick question...I first developed my plugin as an individual 'webpage' and am now integrating (wrapping) it into a WP plugin. It all functioned correctly before WP and almost functions correctly now but I have run into an interesting issue. Anyways, to my question...is there some sort of issue in using a jQuery .post() function from within the WP sidebar? I am trying to do this type of thing...

 jQuery.post("php/draw_calendar.php",
 {month: currentMonth, year:
 currentYear - 2000, days:
 daysInMonth}, function(data){
      jQuery("#Calender").html(data);
      jQuery( "#eventDialog" ).dialog({ autoOpen: false, resizable: false,
 width: 300, minHeight: 200 });
    jQuery( "#eventDialog" ).bind(
 "dialogopen", function(event, ui) {});
   });

Everything in the jQuery works fine except that it seems that the data returned from the file is never added to the HTML and all that is returned is 'This is a Variable'.

I think this may have to do something with the how the paths might work in jQuery and Wordpress but I am at a bit of a loss on what to do. I haven't seen any other info on this on the web so maybe I am just doing something dumb.

Thanks.

+2  A: 

You should fetch the right directory for your WP Plugin with the WP API (bloginfo())... something like:

jQuery.post("<?php bloginfo('wpurl');  ?>/wp-content/plugins/calendar/draw_calendar.php",

As Brad points out you need to use wpurl. Also, as awats wrote, you might be able to just use the WP Constant for the plugin directory. I'm not sure quite how it's formatted:

jQuery.post("<?php echo PLUGINDIR;  ?>/calendar/draw_calendar.php",

Also from the determining plugin and content pages page, it looks like you can use plugin_basename();:

jQuery.post("<?php echo plugin_basename();  ?>/calendar/draw_calendar.php",
Peter Ajtai
I believe you'd actually want `bloginfo('wpurl')`, as `bloginfo('url')` would not be correct if the user's wordpress install location is not the same as their site address (which can be changed in the Wordpress admin panel). Also, you need the plugin name after /plugins/. Of course, you can only do this if you're using PHP to output the JavaScript.
Brad
Not a huge deal, but also there is a wordpress PHP constant for the plugin directory, "PLUGINDIR". http://striderweb.com/nerdaphernalia/2008/08/wordpress-constants/
awats
I gave that a go...for whatever reason "/wordpress/wp-content/plugins/swamped-event-calendar/js/test.php" works and "<?php bloginfo('url'); ?>/wp-content/plugins/draw_calendar.php" does not. Are there any issues with calling PHP from within a js file? I ask because it would also be convenient to use the WP_PLUGIN_URL variable from wordpress. EDIT: As Brad noted I can not do this becuase my JS is a file in itself and not outputted by PHP.
Ryan
So is it standard practice to output all JS from PHP?
Ryan
I must be doing something wrong here...I have my file structure EXACTLY as you said =) (nice coincidence) and used jQuery.post("../php/draw_calendar.php", initially. It seems that the current directory when this file is run though is NOT where the JS file is. Either that or this is a clear example of why hardware engineers should never write code =).
Ryan
@Ryan - Yeah. The current directory is actually the HTML page... my bad.
Peter Ajtai
@Peter - NP, the amount of support on this site is incredible. I will take any suggestions offered up =).
Ryan
This answer is not correct for how I have my files coded. You can't call PHP from within a JS file. Do most wordpress developers generate JS from within their PHP instead of calling an external JS file? That is the only way this could possibly work.
Ryan
@Ryan - I think you'd have to write out your JS to a `.js` file using PHP as part of the plugin install.... but I'm not sure. -------- Or you could put your JS in a `.php` file inside `<script>` tags... Then you could use php.
Peter Ajtai
@Peter - Ah, both of those make sense. And it does seem at least reasonable to do it as part of the plugin install. I will give both of those solutions a try and see if I have any luck.
Ryan
A: 

Is your plugin run on the admin side? In that case you could consider tapping into the built in AJAX processer. So you could do something like this...

<?php 

   add_action('wp_ajax_update_calender', 'update_calender_callback');

   function update_calender_callback(){
     //perhaps include your calender.php script here
     // and run the operations you need...
   }

  //break the php here in the plugin file and then your js is entered after ?>
<script type="text/javascript">
var calData = {
 month: currentMonth,
 year: currentYear - 2000, 
 days: daysInMonth 
 action: update_calender //notice we send an action here...
}

//The callback function I just copied and pasta from what you had...
jQuery.post('ajaxurl',calData,function(data){
      jQuery("#Calender").html(data);
      jQuery( "#eventDialog" ).dialog({ autoOpen: false, resizable: false,
 width: 300, minHeight: 200 });
    jQuery( "#eventDialog" ).bind(
 "dialogopen", function(event, ui) {});
   })
</script>   

http://codex.wordpress.org/AJAX_in_Plugins

Or for a less elegant, all js solution you could try...

jQuery.post(location.protocol+"//"+location.hostname+"/wp-content/plugins/yourpluginname/scriptname", data, function...
awats
This plugin actually functions as a widget on the forward facing sidebar. The AJAX call pulls in the data required from the database. It would be too large to pull the whole table so the widget only pulls in the data as required by the user (which isn't changing very often).
Ryan
In that case try the js on the last line. Hopefully that does the trick. jQuery.post(location.protocol+"//"+location.hostname+"/wp-content/plugins/yourpluginname/scriptname", data, function...
awats