Add a link to the featured Image


Hello, I want to add a custom field to the WP editor. With that field I want to add a link to the featured image of that post or page.

I am new to custom fields. Is the Beans Fields API the right tool for that?

I would be happy, if you can share aan example code for that.

thanks Jochen


Hey Jochen,

Yes that is perfectly possible. Here is an example snippet for your:

// Register the post meta.
add_action( 'admin_init', 'example_register_post_meta' );

function example_register_post_meta() {
 $fields = array(
    array(
      'id'      => 'example_featured_image_url',
      'type'    => 'text',
    ),
  );

  beans_register_post_meta( $fields, array( 'post' ), 'example', array(
   'title'    => __( 'Featured Image Link', 'example-domain' ),
    'context'  => 'side',
   'priority' => 'default',
  ) );
}

// Retrieve the value and output in the post body.
add_action( 'beans_post_body_prepend_markup', 'example_image_post_meta_output' );

function example_image_post_meta_output() {
 $image_url = beans_get_post_meta( 'example_featured_image_url' );

 // Stop here if not set.
  if ( empty( $image_url ) ) {
    return;
 }

 ?><img class="uk-margin-bottom" src="<?php echo esc_url( $image_url ); ?>"><?php
}

You can find more about using the Beans Fields API in this article.

Have fun,


Very cool. Thank you for that easy solution.

Here is my altered version. Maybe somebody needs it. I wanted the new URL as anchor for the existing featured image. here is it:

// Add a link to the featured Image
// Register the post meta.
add_action( 'admin_init', 'example_register_post_meta' );

function example_register_post_meta() {
    $fields = array(
        array(
            'id'      => 'example_featured_image_link',
            'type'    => 'text',
        ),
    );

    beans_register_post_meta( $fields, array( 'post' ), 'example', array(
        'title'    => __( 'Featured Image Link', 'example-domain' ),
        'context'  => 'side',
        'priority' => 'default',
    ) );
}

// Retrieve the value and output in the post body.
add_action( 'the_post', 'example_image_post_meta_output' );

function example_image_post_meta_output() {
    $image_url = beans_get_post_meta( 'example_featured_image_link' );

    // Stop here if not set or not a single post.
    if ( empty( $image_url ) || !is_single() ) {
        return;
    }
    beans_wrap_markup( 'beans_post_image_item_wrap', 'featured_image_link', 'a', 
     array( 'href' => $image_url, 'target' => '_blank', 'rel' => 'nofollow', 'title' => 'Get it' )
    );
}

Hey Jochen,

Thanks for sharing, I would advise to use the_post action for example_image_post_meta_output. It is fine to beans_post_body_prepend_markup since it fires before beans_post_image_item_wrap which your are wrapping but since your are not really "prepending" anything to the post body, what your really want is to attached your callback to an action which fires in the loop (to get the correct post id) and before the content displays. the_post is the perfect action since it fires right after setup_postdata() is done πŸ™‚

Have fun,


Write a reply

Login or register to write a reply, it's free!