Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I recently changed the theme on my website pocketlife.com.br , and now all my videos, which were used on the posts on this format: [youtube id="only the id here" width="600" height="350"] are not understood, and inside the posts like this one for example http://pocketlife.com.br/2014/03/cut-the-rope-2-chega-finalmente-ao-android/ , the following message is shown where the video is supposed to be: "Please enter the url to a YouTube video." . How can I make it so I don't need to go into all the posts I have that has any video inside and edit in a way that wordpress will embed it?

Thanks!

share|improve this question
    
Why not just use the built-in youtube embedding instead of using a custom shortcode? See Embeds. –  t31os Apr 1 at 11:13

1 Answer 1

up vote 1 down vote accepted

You can for example:

  • Copy the YouTube shortcode from the old theme to your new theme.

    Locate the line (most likely in the functions.php file):

    add_shortcode( 'youtube', 'some_function' );
    

    and copy this line and the some_function() to your functions.php in your new
    theme or better yet - create a new plugin file including:

    /*Plugin Name: YouTube embed shortcode from old theme */
    
    add_shortcode( 'youtube', 'some_function' );
    
    some_function( $args = array(), $content = ''){ /* ...code... */}
    
  • Add a YouTube embed plugin with the same parameters.

  • Create your own plugin to handle this.

    Here's a poor man's version (untested):

    /*Plugin Name: YouTube embed shortcode - poor man's version */
    
    add_shortcode( 'youtube', 'ytb_sc' );
    
    if( ! function_exists( 'ytb_sc' ) ):
    
        function ytb_sc( $atts = array(), $content = '' )
        {
           $atts = shortcode_atts( array(
               'id'      => '',             // default id
               'width'   => 640,            // default width (px)
               'height'  => 480,            // default height (px)
           ), $atts, 'ytb_sc' );
    
             // Sanitize input:
             $id      = esc_attr( $atts['id'] );
             $width   = (int) $atts['width'];
             $height  = (int) $atts['height'];
    
            if( ! empty( $id ) ) {
                $sc = sprintf( '[embed width="%d" height="%d"]%s%s[/embed]',
                               $width,
                               $height,
                               'https://www.youtube.com/watch?v=',
                               $id );
            } else {
                return __( 'Missing YouTube ID!' );
            }
    
            // Output
            return $GLOBALS['wp_embed']->run_shortcode($sc);
        }
    
    endif;
    

Hope this helps!

share|improve this answer
    
unfortunately it didn't work =[ ... I think my theme (Explicit) has some code blocking any kind of change to it's natural settings for youtube for some reason. It's even blocking the plugin jetpack from working. I'll just have to change it one by one! =/ Thanks a lot for your answer! –  Carlos Ferreira Apr 4 at 18:44
    
ok, what method did you try? –  birgire Apr 4 at 18:47
    
oh and this is the code i found on functions.php on my old theme (LeetPress) add_shortcode('youtube', 'shortcode_youtube'); function shortcode_youtube($atts) { $atts = shortcode_atts( array( 'id' => '', 'width' => 600, 'height' => 360 ), $atts); return '<div class="video-shortcode"><iframe title="YouTube video player" width="' . $atts['width'] . '" height="' . $atts['height'] . '" src="youtube.com/embed/'; . $atts['id'] . '" frameborder="0" allowfullscreen></iframe></div>'; } –  Carlos Ferreira Apr 4 at 18:47
    
I tried creating a plugin with the code you gave me, tried to create a plugin with the code of my old theme, tried installing TinyMCE, hmmm and installing many other plugins for youtube embed –  Carlos Ferreira Apr 4 at 18:48
    
my website keeps on showing the message "Please enter the url to a YouTube video." where the videos should be. =[ –  Carlos Ferreira Apr 4 at 18:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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