字preSS:如何调用与Ajax调用插件功能? [英] Wordpress: how to call a plugin function with an ajax call?

查看:167
本文介绍了字preSS:如何调用与Ajax调用插件功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个字preSS MU插件,它包括与每个帖子的链接,我想使用Ajax,当用户点击该链接调用该插件的功能之一,并动态更新链接 - 文本与该函数的输出。

I'm writing a Wordpress MU plugin, it includes a link with each post and I want to use ajax to call one of the plugin functions when the user clicks on this link, and then dynamically update the link-text with output from that function.

我卡与阿贾克斯查询。我有这个复杂的,显然劈十岁上下,方式做到这一点,但它是不是很努力。什么是正确的或字preSS'的方式,包括在一个插件Ajax功能?

I'm stuck with the ajax query. I've got this complicated, clearly hack-ish, way to do it, but it is not quite working. What is the 'correct' or 'wordpress' way to include ajax functionality in a plugin?

(我现在的黑客code是如下。当我点击生成的链接我没有得到相同的输出我得到的WP页面,当我直接去采样ajax.php在我的浏览器。)

(My current hack code is below. When I click the generate link I don't get the same output I get in the wp page as when I go directly to sample-ajax.php in my browser.)

我有我的code [1]设置如下:

I've got my code[1] set up as follows:

MU-插件/ sample.php:

mu-plugins/sample.php:

<?php
/*
Plugin Name: Sample Plugin
*/
if (!class_exists("SamplePlugin")) {
  class SamplePlugin {
    function SamplePlugin() {}
    function addHeaderCode() {
      echo '<link type="text/css" rel="stylesheet" href="'.get_bloginfo('wpurl').
             '/wp-content/mu-plugins/sample/sample.css" />\n';
      wp_enqueue_script('sample-ajax', get_bloginfo('wpurl') .
             '/wp-content/mu-plugins/sample/sample-ajax.js.php',
             array('jquery'), '1.0');

    }
    // adds the link to post content.
    function addLink($content = '') {
        $content .= "<span class='foobar clicked'><a href='#'>click</a></span>";
        return $content;
    }
    function doAjax() { //
        echo "<a href='#'>AJAX!</a>";
    } 
  }
}
if (class_exists("SamplePlugin")) {
  $sample_plugin = new SamplePlugin();
}
if (isset($sample_plugin)) {
  add_action('wp_head',array(&$sample_plugin,'addHeaderCode'),1);
  add_filter('the_content', array(&$sample_plugin, 'addLink'));
}

MU-插件/采样/取样ajax.js.php:

mu-plugins/sample/sample-ajax.js.php:

<?php
if (!function_exists('add_action')) {
    require_once("../../../wp-config.php");
}
?>
jQuery(document).ready(function(){
    jQuery(".foobar").bind("click", function() {
        var aref = this;
        jQuery(this).toggleClass('clicked');
        jQuery.ajax({
          url: "http://mysite/wp-content/mu-plugins/sample/sample-ajax.php",
          success: function(value) {
            jQuery(aref).html(value);
          }
        });
    });
});

MU-插件/采样/取样ajax.php:

mu-plugins/sample/sample-ajax.php:

<?php
if (!function_exists('add_action')) {
  require_once("../../../wp-config.php");
}
if (isset($sample_plugin)) {
  $sample_plugin->doAjax();
} else {
  echo "unset";
}
?>

[1]注:下面的教程让我这么远,但我难倒了这一点。 <一href="http://www.devlounge.net/articles/using-ajax-with-your-word$p$pss-plugin">http://www.devlounge.net/articles/using-ajax-with-your-word$p$pss-plugin

[1] Note: The following tutorial got me this far, but I'm stumped at this point. http://www.devlounge.net/articles/using-ajax-with-your-wordpress-plugin

推荐答案

TheDeadMedic是不完全正确。字preSS已建成的Ajax功能。请将您的Ajax请求使用POST的说法行动,以/wp-admin/admin-ajax.php:

TheDeadMedic is not quite right. WordPress has built in AJAX capabilities. Send your ajax request to /wp-admin/admin-ajax.php using POST with the argument 'action':

jQuery(document).ready(function(){
    jQuery(".foobar").bind("click", function() {
        jQuery(this).toggleClass('clicked');
        jQuery.ajax({
          type:'POST',
          data:{action:'my_unique_action'},
          url: "http://mysite/wp-admin/admin-ajax.php",
          success: function(value) {
            jQuery(this).html(value);
          }
        });
    });
});

然后把它挂在插件这样的,如果你只希望它的工作为已登录的用户:

Then hook it in the plugin like this if you only want it to work for logged in users:

add_action('wp_ajax_my_unique_action',array($sample_plugin,'doAjax'));

或钩像这样只为工作不登录的用户:

or hook it like this to work only for non-logged in users:

add_action('wp_ajax_nopriv_my_unique_action',array($sample_plugin,'doAjax'));

使用两个,如果你想让它工作的每一个人。

Use both if you want it to work for everybody.

管理-ajax.php使用了一些动作名称已经,所以一定要确保你通过文件,不要使用同一个动作的名称,否则你会意外地尝试做的事情一样删除评论等。

admin-ajax.php uses some action names already, so make sure you look through the file and don't use the same action names, or else you'll accidentally try to do things like delete comments, etc.

修改

对不起,我不太明白的问题。我还以为你问如何做一个Ajax请求。无论如何,两件事情我想尝试:

Sorry, I didn't quite understand the question. I thought you were asking how to do an ajax request. Anyway, two things I'd try:

首先,有你的函数echo只是没有 A 标签字AJAX。接下来,尝试改变你的Ajax调用因此它既有成功和完整的回调:

First, have your function echo just the word AJAX without the a tag. Next, try changing your ajax call so it has both a success and a complete callback:

jQuery(document).ready(function(){
    jQuery(".foobar").bind("click", function() {
        var val = '';
        jQuery(this).toggleClass('clicked');
        jQuery.ajax({
          type:'POST',
          data:{action:'my_unique_action'},
          url: "http://mysite/wp-admin/admin-ajax.php",
          success: function(value) {
            val = value;
          },
          complete: function(){
            jQuery(this).html(val);
          }
        });
    });
});

这篇关于字preSS:如何调用与Ajax调用插件功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆