Skip to content

Instantly share code, notes, and snippets.

@wp-kitten
Created February 28, 2017 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wp-kitten/c647cda5ddacc5b27f1db20a3a476ae2 to your computer and use it in GitHub Desktop.
Save wp-kitten/c647cda5ddacc5b27f1db20a3a476ae2 to your computer and use it in GitHub Desktop.
Add WordPress Internal Link Popup
/*
* Load the scripts needed so we can display the Internal Link Popup
*/
add_action( 'admin_enqueue_scripts', 'loadScripts', 800 );
function loadScripts( $hook ) {
if( is_admin()) {
wp_enqueue_script( 'wplink' );
wp_enqueue_style( 'editor-buttons' );
}
}
/*
* Include the javascript and PHP file needed to display the Internal Link Popup
*/
add_action( 'admin_footer', 'adminFooterScripts' );
function adminFooterScripts() {
if( is_admin())
{
// Require the core editor class so we can call wp_link_dialog function to print the HTML.
// Luckily it is public static method, so no need to have a hidden wp-editor
require_once( trailingslashit( ABSPATH ) . "wp-includes/class-wp-editor.php" );
_WP_Editors::wp_link_dialog(); ?>
<script type="text/javascript">
/* We need ajaxurl to send ajax to retrive links. This script can also be localized */
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php'); ?>";
jQuery(function ($){
/* THE_ID_OF_THE_ELEMENT_CLICKED => usually a text area or input text */
$('#THE_ID_OF_THE_ELEMENT_CLICKED').click(function (){
wpLink.open('THE_ID_OF_THE_ELEMENT_CLICKED'); /* Bind to open the link editor */
});
})
</script><?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment