Example #1
0
function update_sub_field($selector, $value, $post_id = false)
{
    // filter post_id
    $post_id = acf_get_valid_post_id($post_id);
    // vars
    $field = false;
    // within a have_rows loop
    if (is_string($selector)) {
        // get current row
        $row = acf_get_loop('active');
        // override $post_id
        $post_id = $row['post_id'];
        // attempt to find sub field
        $field = get_row_sub_field($selector);
        // create dummy field
        if (!$field) {
            $field = acf_get_valid_field(array('name' => "{$row['name']}_{$row['i']}_{$selector}", 'key' => '', 'type' => ''));
        }
    } elseif (is_array($selector)) {
        // validate
        if (count($selector) < 3) {
            return false;
        }
        // vars
        $parent_name = acf_extract_var($selector, 0);
        // load parent
        $field = acf_maybe_get_field($parent_name, $post_id);
        // add to name
        $name = $field['name'];
        // sub fields
        foreach ($selector as $s) {
            if (is_numeric($s)) {
                // get row index
                $row_i = intval($s) - 1;
                // add to name
                $name .= "_{$row_i}";
            } else {
                // update parent
                $field = acf_get_sub_field($s, $field);
                // create dummy field
                if (!$field) {
                    $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => ''));
                }
                // add to name
                $name .= "_{$field['name']}";
            }
            // if
        }
        // foreach
        // update name
        $field['name'] = $name;
    }
    // delete
    if ($value === null) {
        return acf_delete_value($post_id, $field);
    }
    // update
    return acf_update_value($value, $post_id, $field);
}
 function get_sub_field_choice_label($selector, $value)
 {
     $row = acf_get_loop('active');
     if (!$row) {
         return NULL;
     }
     $sub_field = get_row_sub_field($selector);
     if (!$sub_field || !isset($sub_field['choices']) || !isset($sub_field['choices'][$value])) {
         return NULL;
     }
     return $sub_field['choices'][$value];
 }
Example #3
0
function delete_sub_row($selector, $i = 1, $post_id = false)
{
    // vars
    $sub_field = false;
    $i--;
    // filter post_id
    $post_id = acf_get_valid_post_id($post_id);
    // get sub field
    if (is_array($selector)) {
        $sub_field = acf_maybe_get_sub_field($selector, $post_id, false);
    } else {
        $sub_field = get_row_sub_field($selector);
    }
    // bail early if no sub field
    if (!$sub_field) {
        return false;
    }
    // get raw value
    $value = acf_get_value($post_id, $sub_field);
    // ensure array
    $value = acf_get_array($value);
    // append
    unset($value[$i]);
    // update
    return acf_update_value($value, $post_id, $sub_field);
}