user1556571
user1556571

Reputation:

Wordpress editor shortcode for custom html ouput

I need help creating Wordpress function to make shortcode [youtube-id=ID] in post editor output the following html code with ID value included:

<iframe width="420" height="315" src="https://www.youtube.com/embed/ID?rel=0" frameborder="0" allowfullscreen></iframe>

Upvotes: 3

Views: 52

Answers (2)

Igor Yavych
Igor Yavych

Reputation: 4228

Try this

function my_amazing_shortcode_handler($args)
{
    $args=shortcode_atts(array('id' => 'some_default_id'), $args);
    return '<iframe width="420" height="315" src="https://www.youtube.com/embed/' . $args['id'] . '?rel=0" frameborder="0" allowfullscreen></iframe>';
}

add_shortcode('youtube_iframe', 'my_amazing_shortcode_handler');

Use like [youtube_iframe id='some_id']

For reference: add_shortcode, shortcode_atts

Upvotes: 0

Moishy
Moishy

Reputation: 3648

in your functions.php

function YouTubeID($atts, $content = null) {
    extract(shortcode_atts(array('id'=>''), $atts));
    return '<iframe width="420" height="315" src="https://www.youtube.com/embed/'.$id.'?rel=0" frameborder="0" allowfullscreen></iframe>';
}
add_shortcode('youtube', 'YouTubeID');

wherever you want the video the shortcode will be:

[youtube id="THE_ID"]

Upvotes: 1

Related Questions