I’m coding a widget and I’d like the user to be able to pick a link like you can when editing a regular posts or page (when you click on the little link icon and you get the AJAX search functionality in the pop up). Does anyone know how I get this working? I’ve got a HTML button which I would like to attach and click even to, and a field for the value to go in.
In class-wp-editor.php I found a few interesting things, and wondered if I might need these files..?
wp_enqueue_script('wp-fullscreen');
wp_enqueue_script('wplink');
On calling fullscreen.link();
,like the file mentioned above, I get this error:
Uncaught ReferenceError: wpActiveEditor is not defined
..and I’m stumped for now, because the JS that references that variable looks crazy to me.
Care to point me in the right direction? I’d love to get this working, it will make a killer user interface for my widgets!
——edit——-
Not that much code so far, apart from the script inclusions that I have previously stated;
<label for="<?php echo $this->get_field_name('link'); ?>">Link URL (including http://) : </label>
<input type="text" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" value="<?php if(isset($link)) echo esc_attr($link); ?>" class="widefat" />
<button class="secondary" id="choose_link">Link Browser</button>
..the part of the JS which is supposed to trigger the link script to open;
linkBrowserButton.on("click", function(e){
e.preventDefault();
fullscreen.link();
});
I invoke the link dialogue within the metabox class I use for development. Its a tad hacky but can be done, until something more robust is developed.
You can invoke the link box by first enqueing the required js, and then interacting with the wp-link js files methods.
Make sure you have enqueued wp-link
1 /
wp_enqueue_script( 'wp-link' );
2 / Set up your ui. I usually use a button to invoke the link dialogue from and a textfield to handle the links URL.
3 / Invoke link dialogue
4 // enqueue scripts. Add the following to your functions.php file, and adjust the file names / paths to suit.
Should about do it. I use the same approach in my metabox class and it seems to work OK.
First, I would build it similarly to the link functionality in WordPress: An input text field, results, select functionality and submit (add link) button.
Ajax – this would fire when text is entered into the input, returning set of results based on the search term. Take a look at what we did with our quicksearch plugin, WP Jarvis. You just need to set up the ajax call to target ajaxurl (admin-ajax.php) and set an action hook in your php to execute the query and echo the results in json format. You would want the results to include title, post-type and permalink for each result. Read more about ajax in plugins.
Finally, selecting the result you’re interested in would grab the permalink from the json object and insert into the widget field.
I know this isn’t a complete answer, but I hope this helps.