Add Snippet To Project
Are you tired of the hassle of manually embedding PDF documents into your WordPress website? If so, we have a solution for you. By enabling the Google Docs shortcode for PDF documents, you can easily embed and display your PDF files directly on your site. In this article, we will guide you through the process of enabling the Google Docs shortcode and show you how to effortlessly showcase your PDFs on your WordPress website. Say goodbye to the frustration of clunky PDF embeds and say hello to a seamless user experience. Let’s dive in!
function wpturbo_enable_google_docs_shortcode($atts, $content = null) {
extract(shortcode_atts(array(
'url' => '',
'embedded' => 'true',
), $atts));
if (empty($url)) {
return '';
}
$shortcode = '[googlepdf file="' . $url . '" ';
if ($embedded === 'true') {
$shortcode .= 'embedded="true"';
}
$shortcode .= ']';
return do_shortcode($shortcode);
}
add_shortcode('google_docs', 'wpturbo_enable_google_docs_shortcode');
The code snippet provided enables the use of a custom shortcode in WordPress called [google_docs]
. This shortcode allows you to easily embed PDF documents from Google Docs into your WordPress content.
Let’s break down how the code works:
-
The
wpturbo_enable_google_docs_shortcode()
function is defined. This is the function that will handle the processing of the[google_docs]
shortcode. -
Inside the function, the
extract()
function is used to extract the attributes passed to the shortcode. The shortcode supports two attributes:url
andembedded
. Theextract()
function creates variables from the attributes, making them easily accessible within the function. Theshortcode_atts()
function is used to set default values for the attributes, in this case, an emptyurl
and a default value of'true'
forembedded
. -
Next, the function checks if the
url
attribute is empty. If it is, the function returns an empty string and stops processing. This ensures that a valid URL is provided for the PDF document. -
The
$shortcode
variable is initialized with the opening[googlepdf file=""]
shortcode. Thefile
attribute is set to the value of theurl
attribute passed to the shortcode. -
If the
embedded
attribute is set to'true'
, the$shortcode
variable is appended with theembedded="true"
attribute. This allows you to specify whether the PDF should be embedded within your content or displayed as a link. -
Finally, the closing
]
of the<shortcode>
is added to the$shortcode
variable. -
The
do_shortcode()
function is then called with the$shortcode
variable as the argument. This function processes the shortcode and returns the resulting HTML code. -
The last line of the code snippet adds the
wpturbo_enable_google_docs_shortcode()
function as a callback to theadd_shortcode()
function. This registers the[google_docs]
shortcode in WordPress, allowing you to use it in your content.
With this code in place, you can now use the [google_docs]
shortcode in your WordPress content by providing the url
attribute with the URL of the PDF document you want to embed.
For example:
[google_docs url="https://example.com/document.pdf" embedded="true"]
This will embed the PDF document from the provided URL within your content.