WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trying to add Javascript via Shortcode (3 posts)

  1. deziiner
    Member
    Posted 3 years ago #

    Hi all.

    I'm using jPlayer to add music streaming to my website, and to do so, I'm trying to implement it via WP Shortcode. I want to use the following shortcode to call the jPlayer playlist file:

    [stream playlist="http://example.com/playlist.js"]

    And it should return, with the other called files:

    <script type="text/javascript" src="http://example.com/playlist.js"></script>

    And here's the full shortcode code to show you what I'm doing:

    // ADD SINGLE STREAM SHORT CODE
    // [stream playlist="http://example.com"]
    function getPlayer($atts, $content = null) {
    extract(shortcode_atts(array(
    "playlist" => 'http://example.com/playlist.js',
    ), $atts));
    $showstream = '<style type="text/css">@import url("' . bloginfo('template_directory') . '/css/jplayer.single.doubledance.css");</style>';
    $showstream .= '<script type="text/javascript" src="' . bloginfo('template_directory') . '/js/jquery.jplayer.min.js"></script>';
    $showstream .= '<script type="text/javascript" src="' . $playlist . '"></script>';
    return $showstream;
    }
    add_shortcode("stream", "getPlayer");

    But WordPress strips all javascript of its script tags, so my question is, how do I implement javascript via my shortcode?

    Thanks!

  2. David Gwyer
    Member
    Posted 3 years ago #

    You could take a look at my Plugin called Contemplate:

    http://wordpress.org/extend/plugins/contemplate/

    In the admin options you can define blocks of text/html/javascript etc. and then add it into any posts/pages via a simple shortcode. Plugin has flexible options, see the screenshots etc. and see if it is what you need.

    Hope it helps. :)

  3. deziiner
    Member
    Posted 3 years ago #

    Hey thanks for your help! Turn out I had errors in my code, that's why it wasn't working! Apparently WordPress DOES allow Javascript to be passed through shortcodes.

    Thanks though!

Topic Closed

This topic has been closed to new replies.

About this Topic