代码之家  ›  专栏  ›  技术社区  ›  Mittul At TechnoBrave

WordPress WooCommerce根据用户角色添加新的自定义菜单和限制

  •  0
  • Mittul At TechnoBrave  · 技术社区  · 5 年前

    我正在使用WordPress和WooCommerce,我关注了这篇文章 https://rudrastyh.com/woocommerce/my-account-menu.html 在我的帐户菜单中添加新菜单项。

    这是我的工作代码。

    function getUserRolesByUserId( $id ) {
        
        if ( !is_user_logged_in() ) { return false; }
    
        $oUser = get_user_by( 'id', $id );
        $aUser = get_object_vars( $oUser );
        $sRoles = $aUser['roles'];
        return $sRoles;
    
    }
    
    function createMenuBasedonUserRole($userId)
    {
        $userRoleIds = getUserRolesByUserId(get_current_user_id());
    
        $urlMenuData = [];
        if(!empty($userRoleIds) && in_array('mindesk_var_account',$userRoleIds)) {
            
            $urlMenuData = [
                'pageName' => "Clients",
                "pageLink" => "clients"
            ];
            
            
     
        } else if(!empty($userRoleIds) && in_array('mindesk_owner_account',$userRoleIds)) {
    
            $urlMenuData = [
                'pageName' => "Children",
                "pageLink" => "children"
            ];  
            
        }
        return $urlMenuData;
    }
    
    /*
     * Step 1. Add Link (Tab) to My Account menu
     */
    add_filter ( 'woocommerce_account_menu_items', 'mindesk_clients_children_link', 40 );
    
    function mindesk_clients_children_link( $menu_links ){
        $urlData = createMenuBasedonUserRole(get_current_user_id());
        
        if(!empty($urlData)){
            $menu_links = array_slice( $menu_links, 0, 5, true ) + array( $urlData['pageLink'] => $urlData['pageName'] ) + array_slice( $menu_links, 5, NULL, true );
        }
        
        return $menu_links;
    
    }
    
    /*
    * Step 2. Register Permalink Endpoint
    */
    add_action( 'init', 'mindesk_add_menu_endpoint' );
    function mindesk_add_menu_endpoint() {  
            
        add_rewrite_endpoint( 'clients', EP_PAGES );
        add_rewrite_endpoint( 'children', EP_PAGES );
    }
    
    /*
    * Step 3. Content for the new page in My Account, woocommerce_account_{ENDPOINT NAME}_endpoint
    */
    add_action( 'woocommerce_account_clients_endpoint', 'mindesk_clients_my_account_endpoint_content' );
    function mindesk_clients_my_account_endpoint_content() {
        require_once(get_template_directory() . '/myaccount/clients.php') ;
    }
    
    add_action( 'woocommerce_account_children_endpoint', 'mindesk_children_my_account_endpoint_content' );
    function mindesk_children_my_account_endpoint_content() {
        require_once(get_template_directory() . '/myaccount/children.php') ;
    }
    /* Step 4
    */
    // Go to Settings > Permalinks and just push "Save Changes" button.
    

    这就是我的新菜单“客户”的显示方式。

    enter image description here

    如您所见,我添加了新菜单,并根据用户角色执行页面 mindesk_var_account 我需要展示 clients mindesk_owner_account 我需要展示 children .

    我已经在上创建了这两个php页面 /wp-content/themes/twentytwentyone/myaccount 它工作得很好。

    然而,我想使用 wp_die 或者,如果具有另一个角色的用户尝试访问其中一个不允许他们访问的页面。

    例如,如果登录用户 mindesk_var_账户 如果他们试图去http://localhost/wordpress/my-account/clients/然后我需要使用 wp_die() 不执行它。

    在这两个新的页面内,然后执行菜单和其他内容。我只想要这样的东西。

    enter image description here

    我尝试使用以下代码。。。

    add_action( 'template_redirect', 'my_account_redirect' );
    function my_account_redirect() {
        if( is_page( 'my-account' ) ) {
            wp_die('fg');
    
        }
    }
    

    但它检查了所有 my-account 页。。我希望只检查内部页面,如 client 儿童 .

    有人能指导我如何做到这一点吗?从现在开始我该怎么做。

    谢谢

    0 回复  |  直到 5 年前
        1
  •  1
  •   LoicTheAztec    5 年前

    您的代码中仍然有一些小错误,一些遗漏的东西,并且由于WooCommerce 3,在第2步中对我的帐户端点进行了一些相关更改。有些事情也可以简化。

    为了避免不允许的用户角色访问某些禁止的部分或端点,您可以使用挂接在 template_redirect 钩子,将用户重定向到允许的部分。

    以下是完整的代码:

    // Custom function that get My account menu item data based on user roles
    function get_menu_item_by_user_role() {
        $user_roles = wp_get_current_user()->roles;
    
        if ( ! empty($user_roles) ) {
            $menu_item = [];
    
            // if ( in_array('mindesk_var_account', $user_roles) ) {
            if ( in_array( 'mindesk_var_account', $user_roles ) ) {
                $menu_item = [ 'clients' => __( "Clients", "woocommerce" ) ];
            }
            elseif( in_array( 'mindesk_owner_account', $user_roles ) ) {
                $menu_item = [ 'children' => __( "Children", "woocommerce" ) ];
            }
        }
        return $menu_item;
    }
    
    // Step 1 - Add Link (Tab) to My Account menu
    add_filter ( 'woocommerce_account_menu_items', 'add_mindesk_custom_menu_items', 40 );
    function add_mindesk_custom_menu_items( $menu_items ){
        $new_item = get_menu_item_by_user_role();
    
        if ( ! empty($new_item) ) {
            $menu_items = array_slice( $menu_items, 0, 5, true ) + $new_item + array_slice( $menu_items, 5, null, true );
        }
        return $menu_items;
    }
    
    // Step 2 - Enable endpoint (and endpoint permalink) - Since WooCommerce 3
    add_filter( 'woocommerce_get_query_vars', 'add_mindesk_menu_item_endpoint' );
    function add_mindesk_menu_item_endpoint( $query_vars ) {
        $query_vars['clients']  = 'clients';
        $query_vars['children'] = 'children';
    
        return $query_vars;
    }
    
    // Step 3. Content for the new page in My Account, woocommerce_account_{ENDPOINT NAME}_endpoint
    add_action( 'woocommerce_account_clients_endpoint', 'add_mindesk_account_clients_endpoint_content' );
    function add_mindesk_account_clients_endpoint_content() {
        require_once(get_template_directory() . '/myaccount/clients.php') ;
    }
    
    add_action( 'woocommerce_account_children_endpoint', 'add_mindesk_account_children_endpoint_content' );
    function add_mindesk_account_children_endpoint_content() {
        require_once(get_template_directory() . '/myaccount/children.php') ;
    }
    
    // Step 4. Endpoint page title
    add_filter( 'woocommerce_endpoint_clients_title', 'set_mindesk_account_clients_endpoint_title', 10, 2 );
    function set_mindesk_account_clients_endpoint_title( $title, $endpoint ) {
        $title = __("Clients", "woocommerce" );
    
        return $title;
    }
    
    add_filter( 'woocommerce_endpoint_children_title', 'set_mindesk_account_children_endpoint_title', 10, 2 );
    function set_mindesk_account_children_endpoint_title( $title, $endpoint ) {
        $title = __( "Children", "woocommerce" );
    
        return $title;
    }
    
    // Step 5. Redirect if not allowed user role
    add_action( 'template_redirect', 'redirect_mindesk_account_dashboard' );
    function redirect_mindesk_account_dashboard() {
        if ( is_account_page() ) {
            global $wp;
    
            $item_key = array_keys(get_menu_item_by_user_role());
            $page_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
    
            if ( empty($item_key) &&  ( isset($wp->query_vars['children']) || isset($wp->query_vars['clients']) ) ) {
                wp_safe_redirect( get_permalink($page_id) );
                exit();
            }
            elseif ( 'clients' == reset($item_key) && isset($wp->query_vars['children']) ) {
                wp_safe_redirect( get_permalink($page_id) . 'clients/' );
                exit();
            }
            elseif ( 'children' == reset($item_key) && isset($wp->query_vars['clients']) ) {
                wp_safe_redirect( get_permalink($page_id) . 'children/'  );
                exit();
            }
        }
    }
    
    // Step 6. FLush rewrite rules:
    // Go to Settings > Permalinks and click on "Save Changes".
    

    代码进入函数。活动子主题(或活动主题)的php文件。经过测试并正常工作。

    相关: WooCommerce My Account custom endpoint menu item