Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A custom shortcode i made is being forced to the top of the widget outside of the widget container. Any ideas why? This is my code...

function nktmediaplayer_func($atts) {
 extract(shortcode_atts(array(
    'id' => rand(1, 900),
    'language' => 'en',
    'playlist' => 'no',
    'media' => '3381',
    'height' => '480',
    'width' => '640',
    'style' => 'single'
    ), $atts));
    ?>

    <div id="player_<?php echo $id; ?>" class="video_player"><a href="http://www.adobe.com/products/flashplayer/">Get the Flash Player</a> to see this player.</div>

    <script type="text/javascript" src="http://kadampa.org/embed/apps/jwplayer.js"></script> 
    <script type="text/javascript"> 
        jwplayer("player_<?php echo $id; ?>").setup({
            flashplayer: "http://kadampa.org/embed/apps/player.swf",
            playlistfile: "http://kadampa.org/<?php echo $language; ?>/api/video/<?php if ( 'playlist' == 'yes' ) echo 'playlist/'; ?><?php echo $media; ?>/desc",
            height: "<?php echo $height; ?>",
            width: "<?php echo $width; ?>",
            config: "http://kadampa.org/embed/config/<?php echo $style; ?>.xml"
        });
    </script>

    <?php    
}
add_shortcode('nkt_mediaplayer', 'nktmediaplayer_func', 10);
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

For shortcodes you have to return the output for it to be written out where the shortcode appears.

Either turn your HTML into a PHP string rather than breaking out of the PHP tags or you can use PHPs output buffering methods like so:

ob_start();

?>

    <div id="player_<?php echo $id; ?>" class="video_player"><a href="http://www.adobe.com/products/flashplayer/">Get the Flash Player</a> to see this player.</div>

    <script type="text/javascript" src="http://kadampa.org/embed/apps/jwplayer.js"></script> 
    <script type="text/javascript"> 
        jwplayer("player_<?php echo $id; ?>").setup({
            flashplayer: "http://kadampa.org/embed/apps/player.swf",
            playlistfile: "http://kadampa.org/<?php echo $language; ?>/api/video/<?php if ( 'playlist' == 'yes' ) echo 'playlist/'; ?><?php echo $media; ?>/desc",
            height: "<?php echo $height; ?>",
            width: "<?php echo $width; ?>",
            config: "http://kadampa.org/embed/config/<?php echo $style; ?>.xml"
        });
    </script>

<?php

$output = ob_get_contents();
ob_end_clean();

return $output; 
share|improve this answer
    
instead of the last 3 lines you could just use return ob_get_clean() :) –  onetrickpony Jan 11 '11 at 19:25
    
Cool. Didn't know ob_end_clean() returns the output aswell, thanks for the tip –  sanchothefat Jan 12 '11 at 12:35
    
Thanks for this, very helpful. –  Noel Tock Feb 5 '11 at 16:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.