dongzai2952 2016-03-07 09:48
浏览 92
已采纳

wordpress管理中的纯文本(HTML)

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?

<?php
function get_likes($url) {
    $json_string = file_get_contents('https://api.facebook.com/method/links.getStats?urls=' . $url . '&format=json');
    $json = json_decode($json_string, true);
    if(isset($json[0]['total_count'])){
            return intval( $json[0]['total_count'] );
    } else { return 0;}
}

function get_tweets($url) {
    $json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
    $json = json_decode($json_string, true);
    if(isset($json['count'])){
        return intval( $json['count'] );
    } else {return 0;}
}

function get_plusones($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
    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);
    if(isset($json[0]['result']['metadata']['globalCounts']['count'])){
        return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
    } else {return 0;}
}
function get_stumble($url) {
    $json_string = file_get_contents('http://www.stumbleupon.com/services/1.01/badge.getinfo?url='.$url);
    $json = json_decode($json_string, true);
    if (isset($json['result']['views'])) {
        return intval($json['result']['views']);
    } else {return 0;}
}


if(isset($_GET["thisurl"])){
    $thisUrl=$_GET["thisurl"];
    $firstpart = substr("$thisUrl", 0, 22);
    // Change http://medialoot.com to your own domain!
    if ($firstpart == 'http://mdbootstrap.com') {
        $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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集