2

I need to create or use an existing 3rd party plugin to attach any links (one or more) to a page, post or post-type from the WP link manager:
http://example.com/wp-admin/link-manager.php

I need a metabox with a dropdown or multiple select form element, and I have to link that Blogroll-Link with postmeta or something similar.

Is there any plugin, snippet or solution for my task?

1
  • What do you mean by "postmeta or stg"?
    – brasofilo
    Oct 18 '12 at 1:35
1

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.