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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝