Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress: how to call a plugin function with an ajax call?
    primarykey
    data
    text
    <p>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. </p> <p>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?</p> <p>(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.)</p> <p>I've got my code[1] set up as follows:</p> <p>mu-plugins/sample.php:</p> <pre><code>&lt;?php /* Plugin Name: Sample Plugin */ if (!class_exists("SamplePlugin")) { class SamplePlugin { function SamplePlugin() {} function addHeaderCode() { echo '&lt;link type="text/css" rel="stylesheet" href="'.get_bloginfo('wpurl'). '/wp-content/mu-plugins/sample/sample.css" /&gt;\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 .= "&lt;span class='foobar clicked'&gt;&lt;a href='#'&gt;click&lt;/a&gt;&lt;/span&gt;"; return $content; } function doAjax() { // echo "&lt;a href='#'&gt;AJAX!&lt;/a&gt;"; } } } if (class_exists("SamplePlugin")) { $sample_plugin = new SamplePlugin(); } if (isset($sample_plugin)) { add_action('wp_head',array(&amp;$sample_plugin,'addHeaderCode'),1); add_filter('the_content', array(&amp;$sample_plugin, 'addLink')); } </code></pre> <p>mu-plugins/sample/sample-ajax.js.php:</p> <pre><code>&lt;?php if (!function_exists('add_action')) { require_once("../../../wp-config.php"); } ?&gt; 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); } }); }); }); </code></pre> <p>mu-plugins/sample/sample-ajax.php:</p> <pre><code>&lt;?php if (!function_exists('add_action')) { require_once("../../../wp-config.php"); } if (isset($sample_plugin)) { $sample_plugin-&gt;doAjax(); } else { echo "unset"; } ?&gt; </code></pre> <p><strong>[1] Note:</strong> The following tutorial got me this far, but I'm stumped at this point. <a href="http://www.devlounge.net/articles/using-ajax-with-your-wordpress-plugin" rel="noreferrer">http://www.devlounge.net/articles/using-ajax-with-your-wordpress-plugin</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload