2017-04-13 17 views
0

所以有些人直接訪問我的網站(不通過我與Voluum獲得的跟蹤鏈接),因此他們無法點擊鏈接,我看不到他們作爲我的統計數據的一部分。 如何將沒有/?voluumdata=BASE64...網址參數的用戶重定向到跟蹤的網址,併爲每篇博文發佈不同的重定向?如果每個帖子的URL參數爲空,WordPress的重定向

我正在測試和尋找插件/ .htaccess技巧幾個小時,但似乎沒有任何幫助。

編輯:我發現了一個解決方案,我肯定是要工作,但由於某種原因,它沒有:

[insert_php] 

if(empty($_GET['voluumdata'])) 
{ 
    header('Location: REDIRECT_URL'); 
    exit; 
} 
[/insert_php] 

也試過:

[insert_php] 
if(!isset($_GET['voluumdata'])) 
{ 
    header('Location: REDIRECT_URL'); 
    exit; 
} 
[/insert_php] 

兩個剛剛突破網頁加載proccess。

回答

0

不幸的是,我不明白你在你的問題中輸入的代碼的目的是什麼。我的意思是你不清楚你使用標籤[insert_php]的原因。

解決您的問題可以是以下幾點。

function redirect_direct_access() { 
    // You may use the code: 
    // 
    //  global $wp_query 
    // 
    // in order to determine in which pages you should run your 
    // redirection code. If you only check for the token existence 
    // then you will be faced with redirection loop. I don't explain in 
    // depth how to use the $wp_query as it is not part of your question 
    // but you always have the opportunity to check what is the contents 
    // of this variable using the code: 
    // 
    // echo "<pre>"; 
    // print_r($wp_query); 
    // echo "</pre>"; 
    // 
    // This way you will be able to build your specific if statement 
    // for the page you like to test. 

    if ( 
     ! isset($_GET[ 'voluumdata' ]) || 
     empty($_GET[ 'voluumdata' ]) 
    ) { 
     wp_redirect(home_url('/page/to/redirect/')); 
     exit(); 
    } 
} 

add_action('template_redirect', 'redirect_direct_access'); 

你可以找到相關的template_redirect鉤WordPress的文檔中的更多信息。

+0

這聽起來不錯,但如果我需要設置不同的重定向每個帖子的網址?其目的是像Voluum這樣的跟蹤服務只通過他們提供的獨特鏈接跟蹤訪問者,他們沒有選擇使用Google Analytics等js腳本跟蹤訪問者。 '[insert_php]'標籤是與WP插件一起使用的標籤,可讓您直接將PHP代碼注入帖子。 – Ricardo

+0

所以經過一個小小的研究,我幾乎做到了,現在唯一的問題就是頁面檢測。假設我想爲帖子ID 180創建一個「IF」,我會在「!isset」之前添加「is_page(180)&&」嗎?由於某種原因,它不起作用,並在所有頁面上觸發重定向。任何幫助? – Ricardo

+0

那麼你的解決方案與'[insert_php]'簡碼不能工作,因爲你應該在PHP中打印任何字符到瀏覽器之前做任何重定向。這就是爲什麼你不能做任何重定向。現在,如果您嘗試在上面的代碼段中使用我在評論中提供的代碼,您會發現可以獲得訪問者訪問的頁面和/或帖子的ID。試試這個代碼'global $ wp_query;回聲「

"; print_r($wp_query); echo "
」;'你會發現你訪問的給定頁面的信息太多。希望這可以幫助你。最後,如果你對我的回答滿意,請投票 –

0

以防萬一,任何人都會面臨同樣的問題,@Merianos尼科斯給半的答案,我掌握了它轉換成:

function redirect_direct_access() { 

    $post_id = get_the_ID(); 

    if ( 
    $post_id == POST_ID && 
     !isset($_GET[ 'voluumdata' ]) 
    ) { 
     wp_redirect('REDIRECT_URL'); 
     exit(); 
    } 

    if ( 
    $post_id == POST_ID && 
     !isset($_GET[ 'voluumdata' ]) 
    ) { 
     wp_redirect('REDIRECT_URL'); 
     exit(); 
    } 
} 

add_action('template_redirect', 'redirect_direct_access');