将twitter feed整合到wordpress的短代码中

时间:2015-08-19 14:40:59

标签: php wordpress twitter shortcode

我试图为wordpress创建一个简单的短代码,其中包含以下变量: - 用户名 - no_of_tweets

所以用户可以写[twitter_feed username =" USERNAME" no_of_tweets =" 5"]

它显示了5条最新推文的列表。我一直在为开发人员使用一个名为Twitter Feed的插件 - 它完成所有oAuth的东西,而且你只想编写代码来输出一些前端html。

除了一个令人讨厌的故障之外我还能工作 - 我无法让no_of推文发挥作用。

此代码有效,但不允许用户指定no_of_tweets变量:

    extract( shortcode_atts(
        array(
            'username' => 'skizzar_sites',
            'no_of_tweets' => '3',
        ), $atts )
    );

    // Code
$tweets = getTweets(3, $username);
...

如果我将代码更改为以下内容(即更改$ tweets变量中的" 3"代码停止工作:

extract( shortcode_atts(
    array(
        'username' => 'skizzar_sites',
        'no_of_tweets' => '5',
    ), $atts )
);

// Code
$tweets = getTweets($no_of_tweets, $username);

为什么这可能无法正常工作?

请参阅下面的完整代码:

<?php 
// Add Shortcode
function skizzar_twitter_feed( $atts ) {

// Attributes
extract( shortcode_atts(
    array(
        'username' => 'skizzar_sites',
        'no_of_tweets' => '5',
    ), $atts )
);

// Code
$tweets = getTweets($no_of_tweets, $username);//change number up to 20 for number of tweets
if(is_array($tweets)){

// to use with intents
echo '<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>';

foreach($tweets as $tweet){

if($tweet['text']){
    $the_tweet = $tweet['text'];

    if(is_array($tweet['entities']['user_mentions'])){
        foreach($tweet['entities']['user_mentions'] as $key => $user_mention){
            $the_tweet = preg_replace(
                '/@'.$user_mention['screen_name'].'/i',
                '<a href="http://www.twitter.com/'.$user_mention['screen_name'].'" target="_blank">@'.$user_mention['screen_name'].'</a>',
                $the_tweet);
        }
    }


    if(is_array($tweet['entities']['hashtags'])){
        foreach($tweet['entities']['hashtags'] as $key => $hashtag){
            $the_tweet = preg_replace(
                '/#'.$hashtag['text'].'/i',
                '<a href="https://twitter.com/search?q=%23'.$hashtag['text'].'&src=hash" target="_blank">#'.$hashtag['text'].'</a>',
                $the_tweet);
        }
    }


    if(is_array($tweet['entities']['urls'])){
        foreach($tweet['entities']['urls'] as $key => $link){
            $the_tweet = preg_replace(
                '`'.$link['url'].'`',
                '<a href="'.$link['url'].'" target="_blank">'.$link['url'].'</a>',
                $the_tweet);
        }
    }

    echo $the_tweet;

    echo '
    <ul class="twitter_intents"> 
        <li><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'"><i class="fa fa-reply"></i></a></li>
        <li><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'"><i class="fa fa-retweet"></i></a></li>
        <li><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'"><i class="fa fa-star"></i></a></li>
    </ul>';



    echo '
    <p class="timestamp">
        <a href="https://twitter.com/'.$username.'/status/'.$tweet['id_str'].'" target="_blank">
            '.date('h:i A M d',strtotime($tweet['created_at']. '- 8 hours')).'
        </a>
    </p>';// -8 GMT for Pacific Standard Time
} else {
    echo '
    <br /><br />
    <a href="http://twitter.com/'.$username.'" target="_blank">Click here to read '.$username.'\'S Twitter feed</a>';
}
}
}
}
add_shortcode( 'twitter_feed', 'skizzar_twitter_feed' ); 

这是getTweets函数代码:

/* implement getTweets */
function getTweets($username = false, $count = 20, $options = false) {

  $config['key'] = get_option('tdf_consumer_key');
  $config['secret'] = get_option('tdf_consumer_secret');
  $config['token'] = get_option('tdf_access_token');
  $config['token_secret'] = get_option('tdf_access_token_secret');
  $config['screenname'] = get_option('tdf_user_timeline');
  $config['cache_expire'] = intval(get_option('tdf_cache_expire'));
  if ($config['cache_expire'] < 1) $config['cache_expire'] = 3600;
  $config['directory'] = plugin_dir_path(__FILE__);

  $obj = new StormTwitter($config);
  $res = $obj->getTweets($username, $count, $options);
  update_option('tdf_last_error',$obj->st_last_error);
  return $res;

}

0 个答案:

没有答案