Forum Moderators: coopster

Message Too Old, No Replies

PHP Newbie and Wordpress Plugin Problem

wordpress rss plugin problem with the_content

         

AndrewT2FR

6:22 pm on May 16, 2011 (gmt 0)

10+ Year Member



I'm creating simple wordpress plugin for a tutorial on my blog [t2fr.com ] which is mainly just a blog where I try and post everything I learn. But the problem is I cant figure out how to get the RSS links to show up after the_content. Here is the code and any help is great:

/* Version check */
global $wp_version;
$exit_msg='WP-Append-RSS requires WordPress 2.3 or newer.
<a href="http://codex.wordpress.org/Upgrading_WordPress">Please
update!</a>';

if (version_compare($wp_version,"2.3","<"))
{
exit ($exit_msg);
}

require_once(ABSPATH . WPINC . '/feed.php');
require_once(ABSPATH . WPINC . '/post.php');

global $post;

function WP_Get_The_Feed () {

$rss = fetch_feed('http://www.t2fr.com/feed');
if (!is_wp_error( $rss ) ) :

// Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(5);

// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
endif;

if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) :

$theurl = $item->get_permalink();
$thetitle = $item->get_title();
$thefeedstuff = "<li><a href= $theurl>$thetitle</a></li>";

echo $thefeedstuff;

//end the loop.
endforeach;

}

//function specifically for adding the links to the end of the post.
function WP_Append_The_Links($content)
{

return $content.Wp_Get_The_Feed();
}

add_filter('the_content', 'WP_Append_The_Links');

?>

astupidname

8:45 am on May 17, 2011 (gmt 0)

10+ Year Member



Just taking a guess here and assuming there is no problem with the feed, but perhaps the problem is:
return $content.Wp_Get_The_Feed();
I would think that would work to invoke the Wp_Get_The_Feed function and make it do it's echoes (you're saying it does not? Oh, I'm betting the output is being done before the content, see below), but perhaps not and also the fact that you are not actually returning anything from Wp_Get_The_Feed means nothing will be concatenated to $content.
Example, try the following is what's happening I'll bet:
<?php

function nothing(){
echo 'nasty';
}

echo 'Hello '.nothing().' world';

?>


Note how nothing() does not return a value.
Replace:
echo 'nasty';
with:
return 'beautiful';
and everything is golden. So in your Wp_Get_The_Feed function, rather than the echo's you may want to build a string and return it.

p.s. Welcome to webmasterworld, and good luck!

AndrewT2FR

12:11 pm on May 17, 2011 (gmt 0)

10+ Year Member



thank you your exactly right and i did try returning a string but I could only seem to get one link to return, but it is concatenated at the end of the_content atleast, the problem is returning all 5 links to be echoed I fooled around with array and array_push but couldnt come up with anything

astupidname

9:21 am on May 18, 2011 (gmt 0)

10+ Year Member



Sounds to me like you're close now but may be having problems with how the string is constructed or something. Also there may be errors in the function. Give this a shot and see if this works (mainly just some restructuring of the WP_Get_The_Feed function, and I tossed the php alternate syntax in exchange for clarity):

<?php

/* Version check */
global $wp_version;
$exit_msg='WP-Append-RSS requires WordPress 2.3 or newer.
<a href="http://codex.wordpress.org/Upgrading_WordPress">Please
update!</a>';

if (version_compare($wp_version,"2.3","<")) {
exit ($exit_msg);
}

require_once(ABSPATH . WPINC . '/feed.php');
require_once(ABSPATH . WPINC . '/post.php');

global $post;

function WP_Get_The_Feed() {
$str = '<li>No items.</li>';
$rss = fetch_feed('http://www.t2fr.com/feed');
if (!is_wp_error($rss)) {
// Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(5);
if ($maxitems) {
$str = '';
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
// Loop through each feed item and display each item as a hyperlink.
foreach ($rss_items as $item) :
$theurl = $item->get_permalink();
$thetitle = $item->get_title();
$str .= '<li><a href="'.$theurl.'">'.$thetitle.'</a></li>';
}
}
}
return $str;
}

//function specifically for adding the links to the end of the post.
function WP_Append_The_Links($content) {
return $content.Wp_Get_The_Feed();
}

add_filter('the_content', 'WP_Append_The_Links');

?>


If anybody's wondering, how to post formatted code on webmasterworld [webmasterworld.com]
Do not copy formatted code on webmasterworld from IE, use other browser such as Firefox.