如何添加用户角色下拉注册和登录woocommerce wordpress

时间:2015-06-08 12:21:54

标签: woocommerce wordpress

我有一个工作网站,但我想设置下拉角色我有2个角色候选人和雇主所以我想设置下拉选择他们的角色我正在使用woocommerce注册和登录我想整合下拉列表与该角色注册和登录woocommerce。

function wp_dropdown_roles( $selected = '' ) {
    $p = '';
    $r = '';

    $editable_roles = array_reverse( get_editable_roles() );

    foreach ( $editable_roles as $role => $details ) {
        $name = translate_user_role($details['name'] );
        if ( $selected == $role ) // preselect specified role
            $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
        else
            $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    }
    echo $p . $r;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

function ts_wp_dropdown_roles( $selected = '' ) {
    $p = '';
    $r = '';

    $editable_roles = array_reverse( ts_get_editable_roles() );

    foreach ( $editable_roles as $role => $details ) {
        $name = translate_user_role($details['name'] );
        if ( $selected == $role ) // preselect specified role
            $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
        else
            $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    }
    echo $p . $r;
}

// Custom editable_roles function to be used by "ts_wp_dropdown_roles" function
function ts_get_editable_roles() {
    global $wp_roles;

    $all_roles = $wp_roles->roles;

    /**
     * Filter the list of editable roles.
     *
     * @since 2.8.0
     *
     * @param array $all_roles List of roles.
     */
    $editable_roles = apply_filters( 'ts_editable_roles', $all_roles );

    return $editable_roles;
}

add_filter("ts_editable_roles" , "ts_filter_roles", 10 ,1);

function ts_filter_roles($all_roles){
    foreach($all_roles as $key=>$value){
        if($key !== "employer" && $key !== "candidate"){
            unset($all_roles[$key]);
        }
    }
    return $all_roles;

}