0

Goodafternoon,

I use the following code to add an iframe around the PDF files that are added to a post in WordPress. The only problem with this feature is that the iframe will be also visible in the visual editor of WordPress, so currently it takes a largely space of the visual editor, which makes it difficult to edit other content as well.

The code below will add an iframe around the PDF file inside the content, but also inside the visual editor of WordPress:

function embed_pdf_files($html, $id) {
    $attachment = get_post($id); //fetching attachment by $id passed through

    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if ($mime_type == 'application/pdf') { //checking mime-type
        $src = wp_get_attachment_url( $id );

        //change the size to your liking
        $html = '<iframe width="100%" height="1000" src="' . $src . '"></iframe>';
        return $html; // return new $html    
    }
        return $html;
}
add_filter('media_send_to_editor', 'embed_pdf_files', 20, 3);

Then I came up with the following snippet of code, that will get the URL of the PDF and set it in an iframe, only this will be done at the attachment page I think and not to the single content page of that post.

function pdf_add_link( $content, $id ) {
    global $post;

    $attachment = get_post($id); //fetching attachment by $id passed through

    $mime_type = $attachment->post_mime_type; //getting the mime-type
    if ($mime_type == 'application/pdf') { //checking mime-type
        $src = wp_get_attachment_url( $id );

        $content = '<iframe width="100%" height="1000" src="' . $src . '"></iframe>';           

        return $content; // return new $html    
    }

    return $content;

}

add_filter( 'the_content' , 'pdf_add_link' );

So, I was wondering how I can use it on a different way, that will get the PDF inside the content and wrap it inside an iframe.

I look out to your response. Casper

  • why not use css to limit the height of iframes inside the visual editor? – Milo Dec 6 '14 at 16:25
  • That’s a possible solution, but I think that is for the client confusing.. I will think about that. Thank you. – Casper Dec 6 '14 at 17:22
  • 1
    What about a shortcode? – birgire Dec 6 '14 at 17:35
  • I think this will also be confusing. The client should paste the pdf always in a shortcode and after some time they maybe forget it to use. But it is also a possibility. But if there is a option that it will automatically add a shortcode between the PDF it would also be nice! – Casper Dec 6 '14 at 17:37
  • 1
    You can always try to add some UI spice to your shortcodes ;-) – birgire Dec 6 '14 at 20:35

Your Answer

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

Browse other questions tagged or ask your own question.