2016-03-07 09:48 阅读 84


I am developing my custom theme and I noticed that some of the links result in showing me plain text HTML instead of normal webpage. I tracked down the issue and found out that this happens when I include one custom php file inside my functions.php. I found that code on one of the tutorial on how to create social share buttons. If I comment include out everything works like a charm. I tried to investigate the file but I couldn't find anything wrong with it, could you please have a look what might be wrong?

function get_likes($url) {
    $json_string = file_get_contents('' . $url . '&format=json');
    $json = json_decode($json_string, true);
            return intval( $json[0]['total_count'] );
    } else { return 0;}

function get_tweets($url) {
    $json_string = file_get_contents('' . $url);
    $json = json_decode($json_string, true);
        return intval( $json['count'] );
    } else {return 0;}

function get_plusones($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    $curl_results = curl_exec ($curl);
    curl_close ($curl);

    $json = json_decode($curl_results, true);
        return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
    } else {return 0;}
function get_stumble($url) {
    $json_string = file_get_contents(''.$url);
    $json = json_decode($json_string, true);
    if (isset($json['result']['views'])) {
        return intval($json['result']['views']);
    } else {return 0;}

    $firstpart = substr("$thisUrl", 0, 22);
    // Change to your own domain!
    if ($firstpart == '') {
        $data = "{";
        $data .= '"facebook": '. json_encode(get_likes($thisUrl)) . ", ";
        $data .= '"twitter": ' . json_encode(get_tweets($thisUrl)) . ", ";
        $data .= '"gplus": ' . json_encode(get_plusones($thisUrl)) . ", ";
        $data .= '"stumble": ' . json_encode(get_stumble($thisUrl)) . "}";
    } else {
        //throw error
        $data = 'ERROR - you are trying to use this script for something outside of the allowed domain';

} else {
    $data = '';

    header('Content-Type: application/json');
    echo $data;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongtiannan0367 dongtiannan0367 2016-03-07 10:05

    You are echoing the contents of $data – I guess thats also what you are seeing if I understood that correctly.

    If the code is included in your functions.php like this, it probably gets executed as soon as the functions.php file is loaded, which might be too late or too early.

    To be able to control when the code executes, you should have a look into WordPress Hooks and hook your code into this mechanism.

    If you can tell me more about what exactly you are trying to do, I might be able to give a more detailed answer.

    Just as a sidenote: Take care not to cross over into Plugin territory with your theme. As soon as you are trying to do anything more than modifying the look of something, it doesn't belong into functions.php anymore but a separate plugin.

    点赞 评论 复制链接分享