How to save post change url youtube link?

In functions.php i using this to change width url from youtube:

add_action('save_post', 'set_url_youtube');
function set_url_youtube() {
    $post = get_post($post_id); 
    $content = $post->post_content;
    preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch?v=([-|~_0-9A-Za-z]+)&?.*?)#i', $content, $urls, PREG_SET_ORDER);
    if(is_array($urls)) {
       foreach ($urls as $url)
       $videos_url[] = $url[0];
    }

    if (is_array($videos_url)) {
        $videos_url = array_unique($videos_url);
        rsort($videos_url);
    }   

    if($videos_url) {
        foreach ($videos_url as $video_url) {
            $content = str_replace($video_url, $video_url.'&w=550', $content);
        }
        remove_action( 'save_post', 'set_url_youtube' );
        wp_update_post( array('ID' => $post_id, 'post_content' => $content) );
        add_action( 'save_post', 'set_url_youtube' );
    }
}
?>

But when save post is, url http://www.youtube.com/watch?v=KTRPVo0d90w not change to http://www.youtube.com/watch?v=KTRPVo0d90w&w=550
How to fix it ?

Related posts

1 comment

  1. I think you want to change default video width.
    You can change default embed video width and height by embed_defaults filter.

    add_filter('embed_defaults', 'ravs_embed_defaults');
    
    function ravs_embed_defaults($embed_size) {
        if (is_single()) { // Conditionally set max height and width
            $embed_size['width'] = 640;
            $embed_size['height'] = 600;
        } else {           // Default values
            $embed_size['width'] = 460;
            $embed_size['height'] = 600;
        }
        return $embed_size; // Return new size
    }
    

Comments are closed.