How to attach Links into page/post or custom post type?

This is a simple and quick Metabox + jQuery + Plain Text solution.

If you’d like to split the dropdown into categories, then check the documentation for get_bookmarks.
This may also be useful: add_meta_box.

This is the result:
snippet result

And the code:

add_action( 'add_meta_boxes', 'wpse_42440_add_custom_box' );
add_action( 'admin_head', 'wpse_42440_script_direct_print' );

function wpse_42440_add_custom_box() 
{
    add_meta_box(
        'wpse_42440_sectionid',
        __( 'Blogroll' ), 
        'wpse_42440_blogroll_box',
        'page'
    );
    add_meta_box(
        'wpse_42440_sectionid',
        __( 'Blogroll' ), 
        'wpse_42440_blogroll_box',
        'post'
    );
}


function wpse_42440_blogroll_box() 
{
    $bookmarks = get_bookmarks( array(
                    'orderby'        => 'name',
                    'order'          => 'ASC'
                              ));

    echo '<div class="element" id="container-1">
        <select name="bookmarks_dropdown" id="bookmarks_dropdown">
        <option value="">- Select -</option>';

    /**
     * Values contained in each $link
     * link_id, link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_updated, link_rel, link_notes, link_rss
     */
    foreach($bookmarks as $link) {
          echo '<option value="'.$link->link_url.'">'.$link->link_name.'</option>';
    }

    echo '</select>
    <input type="checkbox" name="colores" id="new-window" value="">New window?
    <br /><br />Plain link:<br /><input type="text" name="url1" id="plain-link" size="33" value="">
    <br />Full link:
    <br /><input type="text" name="url2" id="full-link" size="33" value="">
    </div>';
}


function wpse_42440_script_direct_print() 
{
    global $pagenow;
    $post_types = array( 'post-new.php', 'post.php'); 

    // Not our types. Quit.
    if ( !in_array( $pagenow, $post_types ) )
        return;

    ?>
        <script type="text/javascript">
        jQuery(document).ready(function($) 
        {    
            $('#bookmarks_dropdown').on('change', function()
            {       
                // define vars
                var target_window = $('#new-window').attr('checked') ? ' target="_blank"' : '';
                var full_link = '<a href="'+$(this).val()+'"'+target_window+'>'+$('#bookmarks_dropdown option:selected').text()+'</a>';

                // fill input text fields
                $('#plain-link').val($(this).val());
                $('#full-link').val(full_link);
            });
        });
        </script>
    <?php
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)