How to create a shortcode for embedding pdf in iframe?

In this tutorial we’ll learn how to create a shortcode for embedding pdf in iframe. We can use a google docs pdf viewer to embed a pdf on a wordpress site. Here is the function which will enable you to achieve that.

Copy the following lines of code at the end of your functions.php.

function pdf_function($attr, $url) {


       'width' => '640',

       'height' => '480'

   ), $attr));

   return '<iframe src="' . $url . '&embedded=true" style="width:' .$width. '; height:' .$height. ';">Your browser does not support iframes</iframe>';


add_shortcode('pdf', 'pdf_function');

You can find functions.php here.

woocommerceNow open any of your page and enter the shortcode like this.

[pdf width=”520px” height=”700px”][/pdf]

Save this page.

Now, go to front end and open the page in which you’ve placed the shortcode and you’ll see the pdf in pdf viewer.




I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

Leave a Comment

Your email address will not be published. Required fields are marked *