【问题标题】:Redirect URL based on URL query string根据 URL 查询字符串重定向 URL
【发布时间】:2016-09-10 08:12:10
【问题描述】:

我想根据自定义帖子类型的 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);

【问题讨论】:

    标签: wordpress redirect custom-post-type


    【解决方案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
            )
    
    )
    

    希望它能解决你的问题。随意根据您的需要和希望改进功能。

    【讨论】:

    • 谢谢,但它不会带我进入仪表板内部。此外,使用 admin_url() 代替 home_url(/wp-admin/) 是否会更好。无论如何,现在都不适合我。它会将我带到主页。
    • 是的,您可以使用 admin_url() 没关系,如果您需要检查用户何时登录以将他直接带到仪表板,您还需要将 is_user_logged_in() 添加到您的 if 语句中。试一试
    • 对不起,这是我的错误。我有另一个较早触发的条件,因此此功能不适用。谢谢。我接受这个答案。
    猜你喜欢
    • 2012-10-15
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多