【问题标题】:Using jQuery post() or load() within a Wordpress plugin在 Wordpress 插件中使用 jQuery post() 或 load()
【发布时间】:2010-10-21 19:35:40
【问题描述】:

我正在开发一个插件并有一个快速的问题...我首先将我的插件开发为一个单独的“网页”,现在正在将它集成(包装)到一个 WP 插件中。它在 WP 之前都正常运行,现在几乎正常运行,但我遇到了一个有趣的问题。无论如何,对于我的问题...在 WP 侧边栏中使用 jQuery .post() 函数是否存在某种问题?我正在尝试做这种事情......

 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) {});
   });

jQuery 中的所有内容都可以正常工作,只是从文件返回的数据似乎从未添加到 HTML 中,并且返回的只是“这是一个变量”。

我认为这可能与路径在 jQuery 和 Wordpress 中的工作方式有关,但我不知道该怎么做。我没有在网上看到任何其他信息,所以也许我只是在做一些愚蠢的事情。

谢谢。

【问题讨论】:

  • 好吧,我想出了部分解决方案……只是带来了另一个问题。如果我在绝对路径中编码,这可以正常工作......即http://localhost:8888/wordpress/wp-content/plugins/swamped-event-calendar/js/ 或类似的东西。显然,这是一种糟糕的做法。所以,我的问题归结为我如何能够在这里使用相对路径。 wordpress 中的 jQuery 显然不使用类似于 CSS 的引用,其中当前目录是 CSS 文件路径本身。任何信息将不胜感激!
  • 你可以尝试在你的路径之前使用 / 吗 --> "/php/draw_calendar.php" ? / 表示当前路径的相对...
  • 这是我尝试的第一件事。它没有用。我可以调用像 /wordpress/wp-content/plugins/swamped-event-calendar 这样的相对路径,但这并不比我认为的绝对路径更好。因为它会将插件名称硬编码到源代码中。

标签: jquery ajax wordpress


【解决方案1】:

您应该使用 WP API (bloginfo()) 为您的 WP Plugin 获取正确的目录...类似于:

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

正如 Brad 指出的那样,您需要使用 wpurl。 此外,正如 awats 所写,您也许可以将 WP 常量用于插件目录。我不太确定它的格式:

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

同样来自determining plugin and content pages page,看来你可以使用plugin_basename();

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

【讨论】:

  • 我相信您实际上想要bloginfo('wpurl'),因为如果用户的 wordpress 安装位置与其站点地址不同(可以在 Wordpress 管理员中更改),bloginfo('url') 将不正确控制板)。此外,您需要 /plugins/ 之后的插件名称。当然,只有在使用 PHP 输出 JavaScript 时才能这样做。
  • 没什么大不了的,但插件目录“PLUGINDIR”还有一个wordpress PHP常量。 striderweb.com/nerdaphernalia/2008/08/wordpress-constants
  • 我试一试......无论出于何种原因,“/wordpress/wp-content/plugins/swamped-event-calendar/js/test.php”有效,“/wp-content/plugins/draw_calendar.php" 没有。从 js 文件中调用 PHP 是否有任何问题?我问是因为使用 wordpress 中的 WP_PLUGIN_URL 变量也很方便。编辑:正如布拉德所说,我不能这样做,因为我的 JS 本身就是一个文件,而不是由 PHP 输出。
  • 那么从PHP输出所有JS是标准做法吗?
  • 我一定是做错了什么...我的文件结构和你说的完全一样 =) (很巧合)并使用了 jQuery.post("../php/draw_calendar.php",最初。似乎运行此文件时的当前目录不是 JS 文件所在的位置。无论是那个还是这都是为什么硬件工程师不应该编写代码的一个明显示例=)。
【解决方案2】:

您的插件是否在管理员端运行?在这种情况下,您可以考虑利用内置的 AJAX 处理器。所以你可以做这样的事情......

<?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

或者对于不太优雅的全 js 解决方案,您可以尝试...

jQuery.post(location.protocol+"//"+location.hostname+"/wp-content/plugins/yourpluginname/scriptname", data, function...

【讨论】:

  • 这个插件实际上作为前向侧边栏上的一个小部件。 AJAX 调用从数据库中提取所需的数据。拉整个表格太大了,所以小部件只根据用户的要求提取数据(这不会经常改变)。
  • 在这种情况下尝试最后一行的 js。希望这能解决问题。 jQuery.post(location.protocol+"//"+location.hostname+"/wp-content/plugins/yourpluginname/scriptname", 数据, 函数...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
相关资源
最近更新 更多