How to Enable Google Docs Shortcode for PDF Documents in WordPress

Home » Snippets » How to Enable Google Docs Shortcode for PDF Documents in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing 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:

  1. The wpturbo_enable_google_docs_shortcode() function is defined. This is the function that will handle the processing of the [google_docs] shortcode.

  2. Inside the function, the extract() function is used to extract the attributes passed to the shortcode. The shortcode supports two attributes: url and embedded. The extract() function creates variables from the attributes, making them easily accessible within the function. The shortcode_atts() function is used to set default values for the attributes, in this case, an empty url and a default value of 'true' for embedded.

  3. 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.

  4. The $shortcode variable is initialized with the opening [googlepdf file=""] shortcode. The file attribute is set to the value of the url attribute passed to the shortcode.

  5. If the embedded attribute is set to 'true', the $shortcode variable is appended with the embedded="true" attribute. This allows you to specify whether the PDF should be embedded within your content or displayed as a link.

  6. Finally, the closing ] of the <shortcode> is added to the $shortcode variable.

  7. 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.

  8. The last line of the code snippet adds the wpturbo_enable_google_docs_shortcode() function as a callback to the add_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.

Register an account to save your snippets or go Pro to get more features.