Creating a shortcode in a plugin that includes JS

admin2025-06-03  4

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/widgets.js" charset="utf-8"></script>
<!-- end  Tweet JS tag -->
EOD;
return $tweet_text;
}

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/widgets.js" charset="utf-8"></script>
<!-- end  Tweet JS tag -->
EOD;
return $tweet_text;
}
Share Improve this question asked Aug 24, 2013 at 6:09 zutto_shonenzutto_shonen 211 silver badge6 bronze badges 2
  • 2 Possible duplicate of Enqueue Scripts / Styles when shortcode is present – admcfajn Commented Jan 29, 2019 at 18:35
  • except this question was written one year after the "duplicate" you mentioned... – rudtek Commented Feb 8, 2019 at 16:51
Add a comment  | 

1 Answer 1

Reset to default 2

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/widgets.js" charset="utf-8"></script>
<!-- end  Tweet JS tag -->
EOD;
return $tweet_text;
}
?>

For more information on add_shortcode function visit this page.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748964146a315207.html

最新回复(0)