I am writing a plugin and I want to create a list of posts/and pages the user can choose from that is exactly like the list you see when you click the ‘link’ button in the editor and twirl down ‘Or link to existing content’- does anyone know where I can find/how to call the function that generates that?
(I have asked this in the wordpress.stackexchange but I never seem to get anywhere posting there 🙁 )
Thanks Hobo! Actually I just found my own solution and was just about to post it when I saw your answer!
Anyway, I’ll post my solution here as it may be useful for someone 😉
So this is the function I made (basically I nicked it from class_wp_editor.php- its virtually the same as the ‘wp_link_query’ in there…)
}
I made it have a massive number of posts so that all of them are returned, I then iterate over the results array and stuff them into a table.
then I found a fab bit of code on code canyon that filters the table as you type in a search box- just like in the back end. This is here http://codecanyon.net/item/advanced-tables/53366
I am not sure if thats the ‘right’ way to do it since obviously I am repeating code- probably you can just run that function with your own args, and then just deal with the results from your own function…I’ll try it out and post some streamlined code…
That list is populated using a call to
admin-ajax.php
with a parameter ofwp-link-ajax
. That in turn calls_WP_Editors::wp_link_query()
(inwp-includes/class-wp-editor.php
).Looking at the code, it only seems to accept a page number and/or a search term – you can’t change (for example) the number of posts returned (there are no filter hooks called). Not sure if that’ll be a problem for you.
To answer your “how can I find the function that generated that”, I guess there are multiple ways, but I used the Tamper Data Firefox plugin. I watched the calls the browser was making to the server (without tampering with anything) and seeing which call updated the list.
As a 2014 update, there are now two useful filters for this purpose, since WordPress 3.7.0.