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.

I have a function which is used to display a video, I want this video to be shown after the post, It should display the video when the post ends.

I have added filter to it, but then too, the video is shown before the post.

Here is the filter which I am using:

function append_the_video($content) {
            global $post;
            $content .= youtube_video();
            return $content;
            }
    add_filter('the_content', 'append_the_video');

edited entire function

<?php
function youtube_video()
{
?>
<div class="post">

<?php 

$newvar = urlencode(get_the_title());
include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('http://gdata.youtube.com/feeds/base/videos?q='.$newvar.'&client=ytapi-youtube-search&v=2');
    $maxitems = get_option('novideos');

    $items =is_array($rss->items) ? array_slice($rss->items, 0, $maxitems) : '' ;
                            ?>
<ul>
<?php if (empty($items)) {echo "No current video uploads by ".get_the_title();}
else foreach ( $items as $item ) : ?>
<?php
$youtubeid = strchr($item['link'],'='); 
$youtubeid = substr($youtubeid,1);
?>

<p>Latest Videos of <b><?php the_title(); ?>:</b></p>
<br>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="320" height="265">
<param name="movie" value="http://www.youtube.com/v/<?php echo $youtubeid ?>&hl=en&fs=1" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/<?php echo $youtubeid ?>&hl=en&fs=1" width="320" height="265">
<!--<![endif]-->
<p><a href="http://www.youtube.com/v/<?php echo $youtubeid ?>">View movie&raquo;</a></p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php

}
function append_the_video($content) {
        global $post;
        $content .= youtube_video();
        return $content;
        }
add_filter('the_content', 'append_the_video');

?>
share|improve this question
    
are you sure your youtube_video() function is returning a string and not printing the video? –  MZAweb May 26 '11 at 5:50
    
@Daniel Dvorkin yaa you are right, its directly printing the value, its not returning the value, so how can I append it to the post end –  ntechi May 26 '11 at 6:03

2 Answers 2

up vote 1 down vote accepted

2 options:

1) Edit your youtube_video() function so it return a string instead of printing the code

2) The easiest way... if you can edit your theme, place the youtube_video() call after showing your post content

Edit:

<?php
function youtube_video()
{

$ret = "";

$ret .= "<div class='post'>";


$newvar = urlencode(get_the_title());
include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('http://gdata.youtube.com/feeds/base/videos?q='.$newvar.'&client=ytapi-youtube-search&v=2');
    $maxitems = get_option('novideos');

    $items =is_array($rss->items) ? array_slice($rss->items, 0, $maxitems) : '' ;

$ret .= "<ul>";

if (empty($items)) {$ret .= " 'No current video uploads by '.get_the_title()";}
else foreach ( $items as $item ) :
$youtubeid = strchr($item['link'],'='); 
$youtubeid = substr($youtubeid,1);

$ret .= "<p>Latest Videos of <b>" . get_the_title() . ":</b></p>";
$ret .= "<br>";
$ret .= "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='320' height='265'>";
$ret .= "<param name='movie' value='http://www.youtube.com/v/" . $youtubeid ."&hl=en&fs=1' />";
$ret .= "<!--[if !IE]>-->";
$ret .= "<object type='application/x-shockwave-flash' data='http://www.youtube.com/v/" . $youtubeid ."&hl=en&fs=1' width='320' height='265'>";
$ret .= "<!--<![endif]-->";
$ret .= "<p><a href='http://www.youtube.com/v/" . $youtubeid ."'>View movie&raquo;</a></p>";
$ret .= "<!--[if !IE]>-->";
$ret .= "</object>";
$ret .= "<!--<![endif]-->";
$ret .= "</object>";
$ret .= "</li>";

endforeach; 
$ret .= "</ul>";
$ret .= "</div>";

return $ret;
}

?>
share|improve this answer
    
Daniel Dvorkin how do I make my function to return the value instead of printing? –  ntechi May 26 '11 at 7:29
    
I edited my answer with the code –  MZAweb May 26 '11 at 9:38
    
really thanks it is working –  ntechi May 26 '11 at 9:51

Try containing the content and the video inside different div's:

function append_the_video($content) {
        global $post;
        return '<div class="content_inner">'.$content.'</div><div class="content_video">'. youtube_video().'</div>';
}
add_filter('the_content', 'append_the_video');
share|improve this answer
    
the code which you gave doesn't helps me, because my function is not returning any value, its directly printing it, –  ntechi May 26 '11 at 6:15
    
sorry there was an = sign in the return, i removed it, and post your youtube_video() function code so will see just how to change it to return value instead of echoing it. –  Bainternet May 26 '11 at 6:36
    
check my question –  ntechi May 26 '11 at 6:54
    
This is exactly what I was looking for. Is there a simple way to add an argument so it only appends the 'youtube' function to the_content on a specific post_type? –  shawn Jun 4 '11 at 4:13

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.