0

Please help me execute a shortcode outside the POST CONTENT in a theme. I have created a plugin as following:

<?php

add_shortcode('jplayer', 'embed_player');

if (!is_admin()) {
    add_filter('widget_text', 'do_shortcode');
}
add_filter('the_excerpt', 'do_shortcode', 11);
add_filter('the_content', 'do_shortcode', 11);
function embed_player($atts, $content = null) {
    extract(shortcode_atts(array(
        'url' => '',
        'autoplay' => '',
        'volume' => '',
        'class' => '',
        'loops' => '',
                    ), $atts));
    if (empty($url)) {
        return '<div style="color:red;font-weight:700;">ERROR!</div>';
    }
    if (empty($volume)) {
        $volume = '80';
    }
    if (empty($class)) {
        $class = "player_container";
    }
    /** Set default container class */
    if (empty($loops)) {
        $loops = "false";
    }
    $parent_title = get_the_title( $post->post_parent );
        /** check if the post has a Post Thumbnail assigned to it*/
    if ( has_post_thumbnail() ) {
    $thumbnail = the_post_thumbnail();
    }
    else {
        $thumbnail = 'http://demo.link/i.jpg';
    }
    
    $player_cont = '<div class="' . $class . '">';
    $player_cont .= '
    <div id="player-1" class="light">
         <div style="display:none;" class="playerData">
            {"name":"'.$parent_title.'","size":{"width":"100%","height":"100%"},"media":{"mp3":"' .$url.'","poster":"'.$thumbnail.'"}}
          </div>
        </div></div>';
    return $player_cont;
}

The above code shows the audio player in the post content/entry, but I want to add this player on the post title as format:

Audio player (here I want to execute the do_shortcode function)

Post title

Post content/entry (here I included a shortcode [jplayer url="http://my.link"] )

To do this, I inserted a shortcode [jplayer url="http://my.link"] in the post content/entry, and execute do_shortcode in my theme like this:

<?php echo do_shortcode('[jplayer url="'.$variable_parameter.']'); ?>

I also tried many codes:

<?php echo do_shortcode('[jplayer url="'.get_post_meta('post->ID', 'url', true).'"]') ?>

and tried with get_post_custom() I'm a wordpress beginner, so I'm facing with many problems but I love designing website. Please correct the code or guide me the right way. Thanks very much.

1 Answer 1

2

I tried the code you provided but it didn't render the player in the content. I think there is some player js dependency. If so please edit the question and let us know which js you are using.

As far as making the shortcode work in theme template; for that you need to pass it through filter. Please use below and it shall work fine:

<?php echo apply_filters('the_content', '[jplayer url="'.get_post_meta('post->ID', 'url', true).'"]') ?>

OR

<?php echo apply_filters('the_content','[jplayer url="'.$variable_parameter.']'); ?>
4
  • Thanks much. The above code works well, just provide a demonstration. My purpose is to execute a shortcode in a theme
    – samdi
    Commented Dec 30, 2015 at 15:51
  • Good to know it worked. But bit confused on second part of your comment. Do you still need any help? Commented Dec 30, 2015 at 15:58
  • I mean I want to execute a shortcode with variable parameters. I just executed the above short code like this [jplayer url="http://link"] but there is a problem here. The link in parameter url is fixed, not changeable, and the above code shows ERROR. The shortcode works well on the post content/entry. Is there any way to execute the shortcode outside the post content? Most important, the shortcode is not executed in the post content
    – samdi
    Commented Dec 31, 2015 at 1:18
  • Did you try the first code snippet from my answer? That dynamically fethces the URL from post meta and passes it to the short code. Commented Dec 31, 2015 at 4:18

Your Answer

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

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