0

I'm very new to WordPress. I have managed to create a custom field for embedded content (using carbon fields) and oEmbed. It works by simply using the code:

echo wp_oembed_get($variable);

The $variable is retrieved in a loop and contains the URL to a Twitter post. This post is displayed on my index page with (not intended) rounded borders. How do I remove these borders using the parameters chrome=noborders provided by Twitter?

1 Answer 1

0

Answering my own question:

For a single Twitter post, it is not currently possible to remove borders based on specific parameters for a single Twitter post.

However, I've found two pieces of code that solve the problem, even though I still cannot explain them. The following code (based on this post) will enable parameters to be set for a single Twitter post (inside functions.php):

function my_oembed_fetch_url( $provider, $url, $args ) {
if ( strpos( $provider, 'twitter.com' ) !== false) {
    // List of args for a single Tweet: https://dev.twitter.com/rest/reference/get/statuses/oembed
    if ( isset( $args['lang'] ) ) {
        $provider = add_query_arg( 'lang', $args['lang'], $provider );
    }
    if ( isset( $args['theme'] ) ) {
        $provider = add_query_arg( 'theme', $args['theme'], $provider );
    }
    if ( isset( $args['align'] ) ) {
        $provider = add_query_arg( 'align', $args['align'], $provider );
    }
    if ( isset( $args['hide_thread'] ) ) {
        $provider = add_query_arg( 'hide_thread', $args['hide_thread'], $provider );
    }
    if ( isset( $args['widget_type'] ) ) {
        $provider = add_query_arg( 'widget_type', $args['widget_type'], $provider );
    }
}
return $provider;
}
add_filter('oembed_fetch_url', 'my_oembed_fetch_url', 10, 3 );

Inside template (for example index.php), set your desired parameters:

echo wp_oembed_get($twitterurl, array('theme'=>'light',widget_type'=>'video'));

An alternative code is to set parameters directly inside functions.php, using the following code (based on this post):

    add_filter( 'oembed_fetch_url', function( $provider, $url, $args )
    {
    // Target publish.twitter.com provider
    if( 'publish.twitter.com' === parse_url( $provider, PHP_URL_HOST ) )
    $provider = add_query_arg( 'widget_type', 'video', $provider );

    return $provider;
    }, 99, 3 );

Inside template, simply use:

echo wp_oembed_get($twitterurl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.