Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use oembed_endpoint_url filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 8, 2023
5 minutes read

So, let’s dive into the oembed_endpoint_url filter hook in WordPress. This hook allows you to filter the oEmbed endpoint URL. First, you need to register it using add_filter. You can put this code into the functions.php file of your active theme or in a custom WordPress plugin.

At WePlugins, we always prefer creating a custom WordPress plugin when using hooks. This ensures that nothing breaks when you update your WordPress theme in the future.

In the live examples below, we’ve defined a function weplugins_modify_oembed_endpoint_url_defaults which takes three parameters. We then register it using add_filter. The first parameter oembed_endpoint_url is the name of the hook. The second parameter weplugins_modify_oembed_endpoint_url_defaults is the name of the function to be called. The third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed to the registered function.

Sometimes, you may need to remove a registered hook. You can use remove_filter to remove the oembed_endpoint_url filter.

Parameters

    Below are the three parameters required to use this hook:

  • $url: (string) The URL to the oEmbed endpoint.
  • $permalink: (string) The permalink used for the URL query arg.
  • $format: (string) The requested response format.

Live Example

Below is an example of how you can use this hook:

Example 1: Basic Usage

This example shows the basic usage of the oembed_endpoint_url filter:

    function weplugins_modify_oembed_endpoint_url_defaults($url, $permalink, $format) { 
        // Update the $url variable according to your website requirements and return this variable.
        // You can modify the $url variable conditionally too if you want.
        return $url; 
    }
    // Add the filter
    add_filter("oembed_endpoint_url", "weplugins_modify_oembed_endpoint_url_defaults", 10, 3);
    

Example 2: Conditional URL Modification

This example demonstrates how to conditionally modify the oEmbed endpoint URL:

    function weplugins_modify_oembed_endpoint_url_conditional($url, $permalink, $format) { 
        if ($format === 'json') {
            $url = str_replace('xml', 'json', $url);
        }
        return $url; 
    }
    // Add the filter
    add_filter("oembed_endpoint_url", "weplugins_modify_oembed_endpoint_url_conditional", 10, 3);
    

Example 3: Removing the Hook

To remove a hook callback, use the example below:

    // Remove the filter
    remove_filter("oembed_endpoint_url", "weplugins_modify_oembed_endpoint_url_defaults", 10, 3);
    

Please make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook, please contact us and we’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.