2013-01-07 15 views
2

我使用的WP功能:get_the_author_meta('user_url');顯示WP筆者元網址沒有「HTTP」

當我回顯到瀏覽器,它自動會預先考慮的「http://」的URL。我該如何避免這種情況,以便我的網址能夠正確顯示在用戶設置頁面上輸入的內容。

非常感謝。

回答

4
$author_url = get_the_author_meta('user_url'); // e.g. http://www.example.com 
$to_remove = array('http://', 'https://'); 
foreach ($to_remove as $item) { 
    $author_url = str_replace($item, '', $author_url); // to: www.example.com 
} 
echo $author_url; // now it will not have the http:// part you wish to avoid. 
+0

謝謝!完美工作。在你的代碼中只有一個小的語法錯誤:變量'author_url'的第一個實例需要它前面的'$'。 – sbroways

+0

哦,謝謝!更新! – graemeboy

+0

我在這裏需要一些幫助,爲什麼我們在將數據保存到mysql時不修剪http。任何人都可以建議我從哪個行動文件wp保存數據? –

1

str_replace可能是最有效的方法。

$author_url = str_replace(array('http://', 'https://'), '', get_the_author_meta('user_url')); 

其他URL修改技術。

  • 對於更復雜的字符串替換,匹配你可以看看使用preg_replace
  • 有一個叫做http-build-url()的函數,它包含在php_http.dll擴展中,它可能更適合某些用例。