2014-10-17 74 views
1

我正在使用Wp商店定位器 plugin.And我已根據我的需要修改它,但我堅持重定向。如何重定向基於URL

基本上這個插件短code.So工作在列舉我URL的時間是這樣的:localhost/wordpress/?page_id=391

現在有一個環節是重定向我http://localhost/wordpress/?wpsl_id=3

這裏是代碼爲:

add_action("template_redirect", 'my_theme_redirect'); 

    function my_theme_redirect() { 

      $dpath = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ; 
     $templatefilename = 'single-store.php'; 
     if (strpos($dpath,'wpsl_id') !== false){  
       $return_template = get_template_directory() .'/' .$templatefilename ; 
       //$nurl = get_permalink($return_template); 
       //echo $return_template; exit; 
       wp_redirect($return_template); 

       exit;    
      } 
    } 

這段代碼沒有進行重定向我any-where.It停留在同一頁面本地主機/ WordPress的/?PAGE_ID = 391可能是什麼問題呢?

任何人都可以建議我如何在URL中檢測到wpsl_id時指向URL?我想將它們重定向到插件的模板文件夾,我爲其添加了一個文件。

EDITED

我上面的代碼中添加的插件文件正上方的短代碼。希望我與這個問題無關:)

+0

您的'wp_redirect()'函數重定向到頁面模板,而不是有效的站點URL ... – rnevius 2014-10-17 10:45:07

+0

您必須使用'template_include'過濾器鉤子而不是'template_redirect'。 http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include – 2014-10-17 10:48:46

+0

@rnevius抱歉,我沒有得到你。我錯過了什麼?或者我在哪裏犯錯? – Twix 2014-10-17 11:07:27

回答

0

模板名稱看起來不錯。在你打電話之前,你只需要註冊模板。

添加此過濾器,這將你的行動之前執行:

的add_filter( 'template_include', '單店');

+0

您正在尋找非常新的Wordpress。請在回答之前給予有幫助的答案或好好搜索,否則您會降低讚譽。 – Twix 2014-10-21 11:22:36

+0

http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include – 2014-10-22 18:05:23

+0

老兄第二個參數必須是一個函數,而不是頁面名稱。感謝您的時間。我得到了解決方案, – Twix 2014-10-26 06:19:42