0

I have this typed.js code snippet, which I want to call from a shortcode:

<script src="https://www.example.com/wp-content/themes/mediso-v1-03/javascript/jquery-2.2.4.min.js"></script>
<script src="https://www.example.com/wp-content/themes/mediso-v1-03/javascript/typed.js"></script>

<script>// <![CDATA[
$(function(){ $(".element-typing").typed({ strings: ["This is my typing text"], typeSpeed: 20 }); });
// ]]></script>
<p class="element-typing"></p>

Here is what I came up with:

// Add Shortcode
function custom_shortcode23() {
    wp_enqueue_script('script-typed-jquery', 'https://www.example.com/wp-content/themes/mediso-v1-03/jquery-2.2.4.min.js', array('jquery'));
    wp_enqueue_script('script-typed', 'https://www.example.comwp-content/themes/mediso-v1-03/typed.js', array('jquery'));
    echo    '<script>// <![CDATA[$(function(){ $(".element-typing").typed({ strings: ["This is my typing text"], typeSpeed: 20 }); });',
    '// ]]></script>',
    '<p class="element-typing"></p>';
}
add_shortcode( 'typed23', 'custom_shortcode23' );

But it is not working. Where am I wrong?

1
  • 3
    A shortcode callback must return a string, not echo anything. Commented Sep 5, 2016 at 8:17

1 Answer 1

6

First of all, it looks like you are trying to enqueue version 2.2.4 of jquery, under the condition that jquery has already been loaded. That doesn't look good.

Secondly, you can't just echo a script in a shortcode. You must add it to another script using wp_add_inline_script. Like this:

function wpse238227_custom_shortcode23() {
    wp_enqueue_script('script-typed', 'https://www.example.comwp-content/themes/mediso-v1-03/typed.js', array('jquery'));
    // script needs to be added
    $script_to_add = 'jQuery(function ($) { $(".element-typing").typed({ strings: ["This is my typing text"], typeSpeed: 20 }); });';
    wp_add_inline_script ('script-typed', $script_to_add);
    // the html needs to be returned
    return "<p class='element-typing'></p>";
}
add_shortcode( 'typed23', 'wpse238227_custom_shortcode23' );

Note that this script will be added to the footer, because by the time WordPress is evaluating the shortcode in the content, the head of the site has already been assembled. You may also want to read more about enqueueing scripts from shortcodes in general.

4
  • We actually don't need the <script> tag with wp_add_inline_script(). Commented Sep 5, 2016 at 9:09
  • 1
    True, and the paragraph tag can't be just there either because it isn't a script. I just copied the whole string too quikcly. Commented Sep 5, 2016 at 9:14
  • We should probably use jQuery(function ($) {...}); too. Commented Sep 5, 2016 at 9:34
  • @birgire Why are you always right? ;-) Commented Sep 5, 2016 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.