WordPress Plugin: Youtube Shortcode

WordPress

F13 Youtube Shortcode

0 from 0 ratings

Description:

Using F13 Youtube Shortcode you can simply embed a Youtube video into your WordPress powered website using shortcode.

Features include:

  • Simply install and add the shortcode
  • All you need is the Youtube video id (the last section of the URL)
  • Optionally set the autoplay attribute to true for the video to play when the page loads

Arbitrary section

Downloads: 419

Tags:
  • embed
  • shortcode
  • video
  • youtube

GitHub

Do you want to embed a youtube video into a page on your WordPress blog without having to find the embed code, just use shortcode.
Forks: 0
Stars: 0
Open issues: 0
Latest tag: None
git clone https://github.com/f13dev/wp-youtube-shortcode.git

Introduction

Do you want to embed a Youtube video into a page or blog post on a WordPress powered website? Can’t remember the Youtube embed code? Why not embed videos with shortcode.

Simply add the ‘youtube’ shortcode to the desired location on a page or blog post, enter the video ID under the video attribute and optionally set the autoplay attribute to true.

Example

[youtube video=”ug-CyGXMabg” autoplay=”false”]

Installation

  1. Upload the plugin files to the `/wp-content/plugins/plugin-name` directory, or install the plugin through the WordPress plugins screen directly.
  2. Activate the plugin through the ‘Plugins’ screen in WordPress
  3. Add the shortcode [youtube video=”video_id”], or [youtube video=”video_id” autoplay=”true”] to the desired location on your blog.

Code

<?php
/*
Plugin Name: F13 Youtube Shortcode
Plugin URI: http://f13dev.com/wordpress-plugin-youtube-shortcode/
Description: Embed a Youtube video into your blog with shortcode
Version: 1.0
Author: Jim Valentine - f13dev
Author URI: http://f13dev.com
Text Domain: f13-youtube-shortcode
License: GPLv3
*/

/*
Copyright 2016 James Valentine - f13dev (jv@f13dev.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_shortcode( 'youtube', 'f13_youtube_shortcode');

/**
* Function to handle the shortcode
* @param  Array  $atts    The attributes set in the shortcode
* @param  [type] $content [description]
* @return String          The response of the shortcode
*/
function f13_youtube_shortcode( $atts, $content = null )
{
        // Get the attributes
        extract( shortcode_atts ( array (
        'video' => '', // Default video_id of null
        'autoplay' => '' // Default autoplay of 0
    ), $atts ));

    // Check if a youtube video id has been entered
    if ($video == '')
    {
        // If no postcode has been entered, allert the user
        $string = 'The \'video\' attribute is required.';
    }
    else
    {
        // Check if autoplay is 'true', if so set it to 1.
        if ($autoplay == 'true')
        {
            $autoplay = '1';
        }
        // If autoplay is not 'true', set autoplay to 0.
        else
        {
            $autoplay = '0';
        }
        // Create the video iframe
        $string = '
        <iframe id="ytplayer" type="text/html" width="640" height="390"
        src="https://www.youtube.com/embed/' . $video . '?autoplay=' . $autoplay . '"
        frameborder="0">
        </iframe>';
    }
    // Return the newly created string
    return $string;
}

Screenshot

An example of the youtube shortcode in use on a blog post.

An example of the youtube shortcode in use on a blog post.

Leave a Reply