How to Create oEmbed-Based Shortcodes in WordPress

Home » Snippets » How to Create oEmbed-Based Shortcodes in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of manually embedding content from external websites into your WordPress posts? If so, then you’ll be thrilled to learn about oEmbed and how it can make your life easier. With oEmbed, you can simply paste a URL from supported websites and WordPress will automatically generate the embedded content for you. But what if you want more control over how the content is displayed? That’s where oEmbed-based shortcodes come in. In this article, we’ll show you how to create custom shortcodes that leverage the power of oEmbed to embed external content exactly the way you want it. Let’s dive in and explore this handy technique for enhancing your WordPress website.

					function wpturbo_oembed_shortcode($atts, $content = null) {
        'url' => ''
    ), $atts));
    // Embed the oEmbed content
    $embed = wp_oembed_get($url);
    // Return the embedded content
    return $embed;
add_shortcode('my_oembed_shortcode', 'wpturbo_oembed_shortcode');

The code snippet above demonstrates how to create custom oEmbed-based shortcodes in WordPress. oEmbed is a feature in WordPress that allows users to easily embed content from supported providers (such as YouTube, Twitter, Vimeo, etc.) just by pasting the URL of the content.

To create an oEmbed-based shortcode, we start by defining a new function called wpturbo_oembed_shortcode. This function takes two parameters: $atts and $content. $atts represents the attributes passed in the shortcode, and $content represents any content enclosed within the shortcode.

Inside the function, we use the extract function to extract the attribute value(s) passed into the shortcode. In this case, we are expecting a single attribute called url. We set a default value of an empty string in case the attribute is not provided.

The next step is to use the wp_oembed_get function to fetch the oEmbed content based on the provided URL. This function takes the URL as its parameter and returns the embedded content in HTML format.

Finally, we return the embedded content from the function. This means that when the shortcode is used in a post or a page, the embedded content will be outputted instead of the shortcode itself.

To register the shortcode, we use the add_shortcode function. This function takes two parameters: the shortcode name (in this case, my_oembed_shortcode) and the callback function that will be responsible for handling the shortcode (in this case, wpturbo_oembed_shortcode).

By adding this code to the WordPress theme’s functions.php file or creating a custom plugin, users can now use the [my_oembed_shortcode] shortcode in their posts or pages and specify the desired URL attribute. The shortcode will automatically fetch and embed the content from the provided URL using the WordPress oEmbed functionality.

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