Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how can I over-ride wp-link-ajax WordPress



I have been stuck on this for a while.

I have a js script on admin side that takes care of populating "'#wp-link-wrap #most-recent-results ul'" with custom <li> </li> stuff.

then when I click on "insert link" the windows popup and show me all my custom <li> are inside of that ul.

but when I scroll the "wp-link-ajax" action over wright my <li> with post and pages witch i don't want.

here is what I found on stackoverflow
That list is populated using a call to admin-ajax.php with a parameter of wp-link-ajax. That in turn calls _WP_Editors::wp_link_query() (in wp-includes/class-wp-editor.php).</blockquote>

But I diden't find a way to prevent it completely I can alter the wp_link_query() but then it returns "no results" and still over write my custom <li>

Here is my Js script

function linksCall(){

type : "post",
data : {postID: MyACF.post_id, links_ref:'links_ref'},
dataType : "text",
url : MyACF.theme_directory+"/ACF_index_load.php",
beforeSend : function(){
$('#wp-link-wrap #most-recent-results ul').hide();
success : function(data){

var parsedData = $.parseHTML(data);
var $data = $( parsedData );

$('#wp-link-wrap #most-recent-results ul').html($data);
$('#wp-link-wrap #most-recent-results ul').show();

error : function(jqXHR, textStatus, errorThrown) {


//call on init

this javascript is called by

function my_acf_field_group_admin_enqueue_scripts(){

wp_enqueue_script( 'custom_admin_ACF_script', get_template_directory_uri() . '/_/js/ACF_admin.js', array('jquery'));

wp_localize_script( 'custom_admin_ACF_script', 'MyACF', array(
'post_id' => $_GET['post'],
'theme_directory' => get_template_directory_uri(),
'ajaxurl' => admin_url('admin-ajax.php')

add_action('acf/input/admin_enqueue_scripts', 'my_acf_field_group_admin_enqueue_scripts');

The $('#wp-link-wrap #most-recent-results ul') gets populated with the good data but then on scroll it trigger a wp-link-ajax action

Answers (3)


Dbranes answers:

To modify the list I would try:

function wpq_link_query_args( $query )
// modify $query here...

return $query;

add_filter( 'wp_link_query_args', 'wpq_link_query_args' );

or to modify the results:

function wpq_link_query( $results, $query )
// modify $results here...

return $results;

add_filter( 'wp_link_query', 'wpq_link_query', 10, 2 );

If you got no results, I would guess you got some echo or print that's corrupting the ajax call.

You can get the current page via <em>$_POST['page']</em> where the <em>$_POST['action']</em> is <em>wp-link-ajax</em> or <em>$query['offset']</em> in the <em>wp_link_query </em> filter.

So I guess I would go with these filters instead, it looks more clean ;-)

l.pirondini comments:

Hi Dbranes, Actualy I have the stuff I want showing in the box, but then when I scroll it does call "wp_link_query_args" and "wp_link_query" I just want to prevent these two actions from happening because at the time they happened my box is already set with what I want.

Dbranes comments:

ok, what about returning a <em>NULL </em> result, with:

add_filter( 'wp_link_query', '__return_null' );

l.pirondini comments:

I just tried that but it replaces the content inside the <ul> with
"<li class="unselectable"><span class="item-title"><em>No matches found.</em></span></li>"

The solution is preventing "wp_ajax_wp-link-ajax" from running at all but i couldn't find a way to achieve that

l.pirondini comments:

i meant "wp_ajax_wp_link_ajax"

Dbranes comments:

ok, so you need to unregister it with:

remove_action( 'wp_ajax_wp-link-query', 'wp_ajax_wp_link_query', 1 );

but the question is: inside what hook this is suitable.

But what about this hack:

add_action( 'admin_init', function(){
if( isset( $_POST['action'] ) && 'wp-link-ajax' == $_POST['action'] )
unset( $_POST['action'] );

} );


l.pirondini comments:

I can see the logic behind this hack but event with that the action still proceed and it still return a <li> No matches found.</li> replacing my li's

l.pirondini comments:

that's the function posing problem i guess

The problem is that making it invalid by unset( $_POST['action']); seems to returns "No matches found."

Dbranes comments:

The ajax call is in the <em>/wp-includes/js/wplink.min.js</em> file:

$.extend( Query.prototype, {
ready: function() {
return ! ( this.querying || this.allLoaded );
ajax: function( callback ) {
var self = this,
query = {
action : 'wp-link-ajax',
page :,
'_ajax_linking_nonce' : inputs.nonce.val()

if ( ) =;

this.querying = true;

$.post( ajaxurl, query, function( r ) {;
self.querying = false;
self.allLoaded = ! r;
callback( r, query );
}, 'json' );

l.pirondini comments:

is it possible to alter it by function.php so it wont be broken on update? That's so frustrating?

Dbranes comments:

Did you try to create a new node and add the result to that one, but keeping the old one hidden, i.e. this one: <em>#wp-link-wrap #most-recent-results ul</em> ?

Maybe it's possible to alter the <em>wpLink</em> object after initialization, I don't know.

It's the <em>process </em>method that's of interest:

process: function( results, params ) {
var list = '', alt = true, classes = '',
firstPage = == 1;

if ( ! results ) {
if ( firstPage ) {
list += '<li class="unselectable"><span class="item-title"><em>' +
wpLinkL10n.noMatchesFound + '</em></span></li>';
} else {
$.each( results, function() {
classes = alt ? 'alternate' : '';
classes += this.title ? '' : ' no-title';
list += classes ? '<li class="' + classes + '">' : '<li>';
list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
list += '<span class="item-title">';
list += this.title ? this.title : wpLinkL10n.noTitle;
list += '</span><span class="item-info">' + + '</span></li>';
alt = ! alt;

this.ul[ firstPage ? 'html' : 'append' ]( list );

Dbranes comments:

ps: I'm just wondering what kind of list you're fetching, and why it can't be constructed into $results in the <em>wp_link_query </em>filter

l.pirondini comments:

Hey! your sugestion <blockquote>"Did you try to create a new node and add the result to that one, but keeping the old one hidden, i.e. this one: #wp-link-wrap #most-recent-results ul ?"</blockquote>

did the trick Thanks!

actualiy i am fetching data from a repeater field inside the current post (which is already fetched with an ajax call to populate a dropdown on each flexible content field), constructing it with "$results in the wp_link_query" seemed over complicated to me as i am putting much less information in, I guess that would have been a cleaner way.

Thanks for your help again any ways.


zebra webdesigns answers:


Can you PM me the login details.

My skype ID: bhuvan530531
Mail: [email protected]

l.pirondini comments:

sorry don't have the right to do so, the website is on my client's host and I am working with his account.

zebra webdesigns comments:

No problem Can you add me in skype and I can come through teamviewer (remote assistance) and take a look on the error

Skype ID: bhuvan530531


S├ębastien | French WordpressDesigner answers:

could you paste here the code of this js script ?

S├ębastien | French WordpressDesigner comments:

have you a line with on.('scroll' anywhere ?

l.pirondini comments:

Actualy I am using the bootstrap affix element so yes some on.('scroll' stuff is happening but not on the specific dialogue element