0

I'm a newbie to WordPress plugin development. I'm creating a plugin that will fetch a image from some APIs such to flickr and store its values such as its URL in the database. And that particular image will be displayed to the end user along with the content.

Now, I'm trying to create a feature in my plugin which lets the user to see the preview of the image in the admin post window once it is published (in the edit post window).

But I couldn't able to identify any hooks for making that preview in the admin edit post window.

Is there any hooks to add a HTML image tag in the admin edit post window? Any help on this would be greatly appreciated.

1 Answer 1

1

This will work. It will add a meta box below the content editor window which outputs anything you put in the get_preview_image() function. I've included the $post argument so you can grab info from the post.

If you need this to be on a custom post type instead of posts, edit where it says 'post' in the image_preview_add_meta_box() function.

add_action( 'some_image_preview', 'get_preview_image', 10, 1 );

function image_preview_add_meta_box() {
    add_meta_box(
        'image_preview-image-preview',
        __( 'Image Preview', 'image_preview' ),
        'image_preview_html',
        'post',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'image_preview_add_meta_box' );

function image_preview_html( $post ) {
    wp_nonce_field( '_image_preview_nonce', 'image_preview_nonce' );
    do_action( 'some_image_preview', $post );
}

function get_preview_image( $post ) {
    echo 'This is cool!';
}
2
  • Thanks for your solution. I did it myself using add_meta_boxes, is there a way to get it on the side like somewhere under format box?
    – Stranger
    Feb 26, 2017 at 12:30
  • 1
    Replace the 'normal' with 'side' in the image_preview_add_meta_box() function :) If you want it below format, replace 'high' with 'default'. Feb 26, 2017 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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