How to use wpLink without editor?

There is not ethical way of doing this. But still there is a way to do this. WordPress wrote wpLink script keeping in mind that editor is there but still WordPress handle when editor is not there (Good Thing)

Consider this example and assume that we are using it on front-end in footer.

First enqueue the essential style and scripts.

function enqueue_scripts_209490() {
    wp_enqueue_script('wplink');
    wp_enqueue_style( 'editor-buttons' );
}
add_action('wp_enqueue_scripts', 'enqueue_scripts_209490');

Now hook this function in footer Read the inline comments

function display_wplink_html_209490() {
    //Our textarea, click to open the link edior and insert the link in same editor
    echo '<textarea id="example_209490"></textarea>';

    // Require the core editor class so we can call wp_link_dialog function to print the HTML.
    // Luckly it is public static method ;)
    require_once 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 */
        var ajaxurl = "<?php echo admin_url( 'admin-ajax.php'); ?>";
        jQuery(document).ready(function (){
            jQuery('#example_209490').click(function (){
                wpLink.open('example_209490'); /* Bind to open link editor! */
            });
        })
    </script><?php
}
add_action('wp_footer', 'display_wplink_html_209490');

Note: It will not work when user is not logged-in because of js error
setUserSetting is not defined and no AJAX response when user not
logged-in.

Leave a Comment

tech