Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Twitter RSS - Latest Tweet > Multisite

  • SOLVED

Hi All,

I have added a custom Twitter feed that will show a users latest post within the sidebar.

<strong>This works great for a standard Wordpress site (no problems at all) however breaks if tried on a multisite.</strong>

Can anybody show me where I am going wrong to get this working on a multisite?

Lee



Sidebar.php

<?php if($options = get_option('sample_theme_options')) {
if($options['twitter']!=false) {

echo '<div class="twit"><h1>TWITTER<h1></div>';
echo '<h4 class="tweet"><strong>@example: </strong> ';

$doc = new DOMDocument();
# load the RSS -- replace 'lylo' with your user of choice
if($doc->load('http://twitter.com/statuses/user_timeline/example.rss')) {
echo '';
# number of <li> elements to display. 20 is the maximum
$max_tweets = 1;
$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
# fetch the title from the RSS feed.
# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;

# the title of each tweet starts with "username: " which I want to remove
$tweet = substr($tweet, stripos($tweet, ':') + 1);

# OPTIONAL: turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
'<a href="$1">$1</a>', $tweet);

# OPTIONAL: turn @replies into links
$tweet = preg_replace("/@([0-9a-zA-Z]+)/",
"<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);
echo "". $tweet . "";

echo '</h4>';

if($i++ >= $max_tweets) break;
}
echo ' ';
}
}
else {
echo '';
}
} ?>



Working code on normal site with theme option settings :



<?php if($options = get_option('sample_theme_options')) {

if($options['twitter']!=false) {



echo '<div class="twit"><h1>BRITISH COUNCIL TWITTER<h1></div>';

echo '<h4 class="tweet">@';

echo $options['twitter'];

echo ': ';



$doc = new DOMDocument();

# load the RSS -- replace 'lylo' with your user of choice

if($doc->load('http://twitter.com/statuses/user_timeline/'. $options['twitter'] .'.rss')) {

echo '';

# number of <li> elements to display. 20 is the maximum

$max_tweets = 1;

$i = 1;

foreach ($doc->getElementsByTagName('item') as $node) {

# fetch the title from the RSS feed.

# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)

$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;



# the title of each tweet starts with "username: " which I want to remove

$tweet = substr($tweet, stripos($tweet, ':') + 1);



# OPTIONAL: turn URLs into links

$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',

'<a href="$1">$1</a>', $tweet);



# OPTIONAL: turn @replies into links

$tweet = preg_replace("/@([0-9a-zA-Z]+)/",

"<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);

echo "". $tweet . "";



echo '</h4>';



if($i++ >= $max_tweets) break;

}

echo ' ';

}

}

else {

echo '';

}

} ?>

Answers (3)

2011-12-23

Hai Bui answers:

Hi,

Can you be more specific? What does it show if tried on a multisite?


leannekera comments:

It does not load at all. Shows nothing.


Hai Bui comments:

Are you sure the top 2 conditions are true? If you are not sure, try echoing in else statements to check.



<?php if($options = get_option('sample_theme_options')) {

if($options['twitter']!=false) {



echo '<div class="twit"><h1>TWITTER<h1></div>';

echo '<h4 class="tweet">@example: ';



$doc = new DOMDocument();

# load the RSS -- replace 'lylo' with your user of choice

if($doc->load('http://twitter.com/statuses/user_timeline/example.rss')) {

echo '';

# number of <li> elements to display. 20 is the maximum

$max_tweets = 1;

$i = 1;

foreach ($doc->getElementsByTagName('item') as $node) {

# fetch the title from the RSS feed.

# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)

$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;



# the title of each tweet starts with "username: " which I want to remove

$tweet = substr($tweet, stripos($tweet, ':') + 1);



# OPTIONAL: turn URLs into links

$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',

'<a href="$1">$1</a>', $tweet);



# OPTIONAL: turn @replies into links

$tweet = preg_replace("/@([0-9a-zA-Z]+)/",

"<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);

echo "". $tweet . "";



echo '</h4>';



if($i++ >= $max_tweets) break;

}

echo ' ';

}

}

else {

echo '1';

}

}
else {

echo '2';

}
?>


leannekera comments:

Hai Bui,

I have no idea what you changed but after implementing the theme options to your script its working.

Huge think you!

2011-12-23

Arnav Joy answers:

please check this link

http://wpsmith.net/my-plugins/enhanced-latest-tweets-widget/


leannekera comments:

Sorry this script will be interacting with a custom theme options. I cant use a widget.


Arnav Joy comments:

i think your problem is , you are using get_option function at very top which is used in single site .


Arnav Joy comments:

how many sites do you have as multisites??


leannekera comments:

The script will be working along site my theme options where a user can submit their twitter name. Get_option works fine on its own to display the users twitter name so thats not the issue, the problem seems to be the twitter feed on its own.

(I tried to dumb it down a bit to explain better) The full script to be used is so :


<?php if($options = get_option('sample_theme_options')) {
if($options['twitter']!=false) {

echo '<div class="twit"><h1>BRITISH COUNCIL TWITTER<h1></div>';
echo '<h4 class="tweet"><strong>@';
echo $options['twitter'];
echo ': </strong> ';

$doc = new DOMDocument();
# load the RSS -- replace 'lylo' with your user of choice
if($doc->load('http://twitter.com/statuses/user_timeline/'. $options['twitter'] .'.rss')) {
echo '';
# number of <li> elements to display. 20 is the maximum
$max_tweets = 1;
$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
# fetch the title from the RSS feed.
# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;

# the title of each tweet starts with "username: " which I want to remove
$tweet = substr($tweet, stripos($tweet, ':') + 1);

# OPTIONAL: turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
'<a href="$1">$1</a>', $tweet);

# OPTIONAL: turn @replies into links
$tweet = preg_replace("/@([0-9a-zA-Z]+)/",
"<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);
echo "". $tweet . "";

echo '</h4>';

if($i++ >= $max_tweets) break;
}
echo ' ';
}
}
else {
echo '';
}
} ?>


Arnav Joy comments:

define this function in functions.php

function get_ms_option($blogID, $option_name) {
global $wpdb;

$select_statement = "SELECT *
FROM `".DB_NAME."`.`".$wpdb->get_blog_prefix($blogID)."options`
WHERE `option_name` LIKE '".$option_name."'";
$sql = $wpdb->prepare($select_statement);
$option_value = $wpdb->get_results( $sql, ARRAY_A );
return $option_value[0]['option_value'];
}


and now call this function in sidebar as

$numMultiSites = 100;

for($i=1;$i<=$numMultiSites;$i++){

if($options = get_ms_option($i,'sample_theme_options')){

//place everything else here

}

}

or copy this entire code in sidebar.php

<?php

for($i=1;$i<=$numMultiSites;$i++){

if($options = get_ms_option($i,'sample_theme_options')){

if($options['twitter']!=false) {



echo '<div class="twit"><h1>TWITTER<h1></div>';

echo '<h4 class="tweet">@example: ';



$doc = new DOMDocument();

# load the RSS -- replace 'lylo' with your user of choice

if($doc->load('http://twitter.com/statuses/user_timeline/example.rss')) {

echo '';

# number of <li> elements to display. 20 is the maximum

$max_tweets = 1;

$i = 1;

foreach ($doc->getElementsByTagName('item') as $node) {

# fetch the title from the RSS feed.

# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)

$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;



# the title of each tweet starts with "username: " which I want to remove

$tweet = substr($tweet, stripos($tweet, ':') + 1);



# OPTIONAL: turn URLs into links

$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',

'<a href="$1">$1</a>', $tweet);



# OPTIONAL: turn @replies into links

$tweet = preg_replace("/@([0-9a-zA-Z]+)/",

"<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);

echo "". $tweet . "";



echo '</h4>';



if($i++ >= $max_tweets) break;

}

echo ' ';

}

}

else {

echo '';

}

}

}



?>

2011-12-23

Kannan C answers:

get_option is blog option not a network option. Not sure but try
switch_to_blog($blog_id)
before your top if condition