WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Trying to add Javascript via Shortcode

[Resolved] Trying to add Javascript via Shortcode

  • deziiner
    Member

    @deziiner

    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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • David Gwyer
    Participant

    @dgwyer

    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. 🙂

    deziiner
    Member

    @deziiner

    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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Trying to add Javascript via Shortcode’ is closed to new replies.