根据帖子类型创建重力表格轮询

时间:2015-02-05 16:26:17

标签: php wordpress gravity-forms-plugin

有没有办法根据内容类型创建/更新表单轮询字段?例如,我有一个名为Candidate的帖子类型,我想在添加新候选项时或者删除一个候选列表时动态更新投票列表。

我之所以这样做是因为我正在为这个客户创建一个投票机制,他们要求用户查看他们投票的图像,名称和简短的Bio。我的想法是绑定名称,以便我可以在页面上定位隐藏的重力表格轮询,这样当选民点击时,它会更新相应的命名复选框。

我当然可以逐个添加每个候选者,然后在表单中逐个添加每个候选者,但希望有一种方法可以在代码中执行此操作。到目前为止,除了Gravity Forms文档中的过滤器之外,我还没有找到更多。

重申一下,我的问题不是前端连接,而是在为Candidate帖子类型创建内容时如何在表单中的轮询字段中动态更新/添加选项。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我相信你所追求的解决方案在此记录:

  

http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_pre_render/

该页面上的解决方案示例#1和#2的组合可能适合您的需求?所以 -

  1. 创建一个标准Wordpress页面类别(类别名称:“候选人”),页面将包含候选人信息(生物,照片等)。

  2. 在当前主题的functions.php文件中,您需要添加以下内容来提取该类别的帖子:

    // modify form output for the public page
    add_filter("gform_pre_render", "populate_checkbox");
    // modify form in admin
    add_filter("gform_admin_pre_render", "populate_checkbox"); 
    
    function populate_dropdown($form) {
        // some kind of logic / code to limit what form you're editing
        if ($form["id"] != 1) { return $form; }
    
        //Reading posts for "Business" category;
        $posts = get_posts("category=" . get_cat_ID("Candidates"));
    
        foreach ($form['fields'] as &$field) {
    
            // assuming field #1, if this is a voting form that uses checkboxes
            $target_field = 1;
            if ($field['id'] != $target_field) { break; }
    
            // init the counting var for how many checkboxes we'll be outputting
            // there's some kind of error with checkboxes and multiples of 10?
            $input_id = 1;
            foreach ($posts as $post) {
    
                //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
                if($input_id % 10 == 0) { $input_id++; }
    
                // here's where you format your field inputs as you need
                $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
                $inputs[] = array("label" => $post->post_title, "id" => "{$field_id}.{$input_id}");
    
                // increment the number of checkboxes for ID handling
                $input_id++;
            }
    
            $field['choices'] = $choices;
            $field['inputs'] = $inputs;
        }
    
        // return the updated form 
        return $form;
    }