0

I've a post with a video, contained inside the content. So, the content of my post is like:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam quam sed imperdiet viverra. In ornare semper nisl, ac sodales metus facilisis ac. Morbi quis erat id magna dapibus consequat. Vestibulum sollicitudin hendrerit nisl sit amet lacinia. Mauris sed sodales quam. Mauris semper, ante quis imperdiet iaculis, neque eros pulvinar eros, id aliquet magna mauris at sapien. Sed sodales, diam venenatis aliquam ornare, nisl lorem accumsan eros, vitae suscipit dolor purus quis odio. Integer quis hendrerit sapien, at pretium neque. Nullam convallis auctor enim et malesuada. Suspendisse id fermentum risus..

[video width="100%" height="360" url="http://www.youtube.com/watch?v=sd1273xA"]"

where the shortcode is displayed like text... I need to display the video...to use do_shortcode.

I'm trying without luck:

function your_prefix_detect_shortcode()
{
    global $post;

    $pattern = get_shortcode_regex();

    if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
            && array_key_exists( 2, $matches )
            && in_array( 'video', $matches[2] ) )
    {
       echo do_shortcode("[$matches[0][0]]");
    }

}
add_action( 'wp', 'your_prefix_detect_shortcode' );
1

In order to get your shortcode to show up, you have to register it first. This is done with add_shortcode(). You can read more about the Shortcode API in the Codex.

Here is an example of a Youtube video-shortcode that accepts 3 parameters; Video ID (from Youtube, the last part of the Youtube url), height and width. This would go into your functions.php file, or in a function plugin.

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

Then use it like this in your posts [video id="sd1273xA"] or [video id="sd1273xA" width="200" height="150"] to specify the width and height.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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