2015-12-03 60 views
0

我想追加一個PHP查詢到我的網站鏈接到贊助商網站使用the_content過濾器和preg_replace()結束。我已經在regexr.com上測試了我的正則表達式,它可以工作。我也使用print_r()來測試函數被調用,但由於某種原因鏈接在實踐中沒有被改變。這裏是使用preg_replace()和wordpress過濾器the_content

add_filter('the_content', 'linkAppend'); 

function linkAppend($content) { 
    global $referalString; 

    preg_replace('/\/\/(www|launch)?\.?(solarwinds\.com)\/[^"]*/g','$&?cmp='.$referalString, $content); 
    return $content; 
} 

我有麻煩代碼如果有人可以點我在正確的方向,還是讓我知道我在哪裏出了問題,我將不勝感激。

回答

1

原來我有多個問題,它指出我沒有將preg_replace()的輸出分配給$content,然後我發現並解決了正則表達式上的全局標誌問題,在php中無效而且我沒有考慮.com之後沒有/的鏈接。最終修復看起來像這樣:

add_filter('the_content', 'linkAppend'); 

function linkAppend($content) { 
    global $referalString; 
    $content = preg_replace('/\/\/(www|launch)?\.?(solarwinds\.com)\/?[^"]*/m','$0?cmp='.$referalString, $content); 
    return $content; 
} 
相關問題