Creating a shortcode in a plugin that includes JS

I use a lot of embedded tweets on my site. The script always gets stripped out. I added Twitter’s code to my theme, but it doesn’t work with infinite scroll. So I’d like to make a shortcode that will import the needed script from Twitter.

Please take a look at what I’ve got below. I can’t get it to work and I’m not sure what’s wrong. Thanks.

<?php
/*
Plugin Name: Tweet Shortcode
Description: Use [tweet] to insert the Twitter embed javascript so it won't be stripped from your post.
Author: Nate Hill
Version: 0.1
*/

add_shortcode( 'tweet', 'tweets' );
function tweet_code( $atts ) {
$tweet_text = <<<'EOD'
<!-- Tweet JS. Do not edit. -->
<script LANGUAGE="JavaScript"  TYPE="text/javascript" async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<!-- end  Tweet JS tag -->
EOD;
return $tweet_text;
}

Related posts

1 comment

  1. Hook function name should be same. You are using “tweets” function name as a parameter for the add_shortcode() function but actual function name in your code is “tweet_code” so it should be “tweets” as shown in the following code.

    <?php
    add_shortcode( 'tweet', 'tweets' );
    function tweets( $atts ) {
    $tweet_text = <<<'EOD'
    <!-- Tweet JS. Do not edit. -->
    <script LANGUAGE="JavaScript"  TYPE="text/javascript" async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
    <!-- end  Tweet JS tag -->
    EOD;
    return $tweet_text;
    }
    ?>
    

    For more information on add_shortcode function visit this page.

Comments are closed.