Gform_Pre_Render下拉列表中填充了角色

时间:2013-09-20 作者:Rizzo

我正在使用重力表单,并试图用角色列表预填充下拉列表。这就是我所拥有的,但它出现了一个错误:

add_filter("gform_pre_render", "gform_prepopluate_populate_role");

//Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_role");

function gform_prepopluate_populate_role($form){
    global $wp_roles;
    $posttype = \'em_companies\';
    $formid = \'5\';
    $fieldid = \'8\';

    //only populating drop down for form id 5
    if($form["id"] != $formid)
       return $form;

    //Reading posts for "Business" category;
    //$posts = query_posts(array(\'post_type\' => array(\'post\', \'movies\')));
   $roles = query_posts( array( $wp_roles->get_names() ) );
   // if ( have_posts() ) : while ( have_posts() ) : the_post();

    //Creating drop down item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");

    //Adding post titles to the items array
    foreach($roles as $role)
        $items[] = array("value" => $role, "text" => $role );

    //Adding items to field id 8. Replace 8 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field)
        if($field["id"] == $fieldid ){            
            $field["choices"] = $items;
        }

    return $form;
}
有什么想法吗?

1 个回复
最合适的回答,由SO网友:Matth_eu 整理而成

你就快到了。。。

“query\\u posts”功能仅用于返回post(和其他post类型),因此这不适用于角色。生产线$roles = ... 可能是您的错误原因

相反,它应该是:

$roles = $wp_roles->get_names();

结束

相关推荐