2016-09-10 105 views
1

我想根據URL查詢字符串爲自定義帖子類型重定向一個URL。基於URL查詢字符串的重定向URL

實例網址:http://example.com/custom_post_type/post-name?et_fb=1

我想檢查URL的查詢字符串et_fb = 1如果這樣沒有自動跳轉別的重定向到管理頁面。我試着用下面的功能,但它並不需要我來管理URL

function redirect_cpt_to_admin() { 
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 
    $redirect = strpos($url,'?et_fb') == false; 

    global $wp_query; 
    if (is_singular('my_cpt') && $redirect){ 
     $admin_url = admin_url(); 
     wp_redirect(esc_url_raw($admin_url), 301); 
     exit(); 
} 
} 
add_action ('template_redirect', 'redirect_cpt_to_admin', 20); 

回答

1

試一下這個功能:

function my_cpt_template_redirect() 
{ 
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 
    $param = 'et_fb'; 
    $redirect = strpos($url,$param); 

    $obj = get_post_type_object('YOURCUSTOMPOSTYPE FOR EXAMPLE POST'); 

    if($obj->labels->singular_name == "YOURCUSTOMPOSTYPE_SINGULAR_NAME" && $redirect === true) 
    { 
     wp_redirect(admin_url()); 
     exit(); 
    } 
} 
add_action('template_redirect', 'my_cpt_template_redirect'); 

假設我們有柱式「認證」,我們可以這樣做:

$obj = get_post_type_object('certification'); 

基本上,如果你的print_r($ OBJ),你會看到下面的結果:

stdClass Object 
(
    [labels] => stdClass Object 
     (
      [name] => Certification 
      [singular_name] => Certification 
      [add_new] => Add New 
      [add_new_item] => Add New Certification 
      [edit_item] => Edit Certification 
      [new_item] => New Page 
      [view_item] => View Certification 
      [search_items] => Search Certification 
      [not_found] => Not found 
      [not_found_in_trash] => Not found in Trash 
      [parent_item_colon] => Parent Certification: 
      [all_items] => All Certifications 
      [menu_name] => Certifications 
      [update_item] => Update Certification 
      [name_admin_bar] => Certification 
     ) 

    [description] => Certifications 
    [public] => 1 
    [hierarchical] => 1 
    [exclude_from_search] => 
    [publicly_queryable] => 1 
    [show_ui] => 1 
    [show_in_menu] => 
    [show_in_nav_menus] => 1 
    [show_in_admin_bar] => 1 
    [menu_position] => 5 
    [menu_icon] => dashicons-welcome-widgets-menus 
    [capability_type] => post 
    [map_meta_cap] => 1 
    [register_meta_box_cb] => 
    [taxonomies] => Array 
     (
      [0] => objective 
     ) 

    [has_archive] => 1 
    [rewrite] => Array 
     (
      [slug] => certification 
      [with_front] => 1 
      [pages] => 1 
      [feeds] => 1 
      [ep_mask] => 1 
     ) 

    [query_var] => certification 
    [can_export] => 1 
    [delete_with_user] => 
    [_builtin] => 
    [_edit_link] => post.php?post=%d 
    [label] => Certification 
    [name] => certification 
    [cap] => stdClass Object 
     (
      [edit_post] => edit_post 
      [read_post] => read_post 
      [delete_post] => delete_post 
      [edit_posts] => edit_posts 
      [edit_others_posts] => edit_others_posts 
      [publish_posts] => publish_posts 
      [read_private_posts] => read_private_posts 
      [read] => read 
      [delete_posts] => delete_posts 
      [delete_private_posts] => delete_private_posts 
      [delete_published_posts] => delete_published_posts 
      [delete_others_posts] => delete_others_posts 
      [edit_private_posts] => edit_private_posts 
      [edit_published_posts] => edit_published_posts 
      [create_posts] => edit_posts 
     ) 

) 

我希望它能解決你的問題。隨意改進功能,儘可能多的,你需要和想要的。

+0

謝謝,但它不會把我帶到儀表板內。另外,這應該更好地使用admin_url()而不是home_url(/ wp-admin /)。無論如何,現在不適合我。它把我帶到了主頁。 – Ayanize

+0

是的,你可以用戶admin_url()沒關係,如果你需要檢查用戶登錄時直接帶他到儀表板,你還需要添加is_user_logged_in()到你的if語句中。試試 – Majid

+0

同時改變一下代碼現在你可以試試看 – Majid

相關問題