function delete_field($selector, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // delete return acf_delete_value($post_id, $field); }
function wp_post_revision_field($value, $field_name, $post = null, $direction = false) { // vars $post_id = 0; // determine $post_id if (isset($post->ID)) { // WP 3.6 $post_id = $post->ID; } elseif (isset($_GET['revision'])) { // WP 3.5 $post_id = (int) $_GET['revision']; } elseif (strpos($value, 'revision_id=') !== false) { // WP 3.5 (left vs right) $post_id = (int) str_replace('revision_id=', '', $value); } // load field $field = acf_maybe_get_field($field_name, $post_id); // update value //$value = $field['value']; // default formatting if (is_array($value)) { $value = implode(', ', $value); } // format if (!empty($value)) { // image || file? if ($field['type'] == 'image' || $field['type'] == 'file') { $url = wp_get_attachment_url($value); $value = $value . ' (' . $url . ')'; } } // return return $value; }
function get_field_choice_label($selector, $value, $post_id = false) { $post_id = acf_get_valid_post_id($post_id); $field = acf_maybe_get_field($selector, $post_id); if (!$field || !isset($field['choices']) || !isset($field['choices'][$value])) { return NULL; } return $field['choices'][$value]; }
function acf_maybe_get_sub_field($selectors, $post_id = false, $strict = true) { // bail ealry if not enough selectors if (!is_array($selectors) || count($selectors) < 3) { return false; } // vars $selector = acf_extract_var($selectors, 0); $selectors = array_values($selectors); // reset keys // attempt get field $field = acf_maybe_get_field($selector, $post_id, $strict); // bail early if no field if (!$field) { return false; } // loop for ($j = 0; $j < count($selectors); $j += 2) { // vars $sub_i = $selectors[$j]; $sub_s = $selectors[$j + 1]; $field_name = $field['name']; // find sub field $field = acf_get_sub_field($sub_s, $field); // bail early if no sub field if (!$field) { return false; } // add to name $field['name'] = $field_name . '_' . ($sub_i - 1) . '_' . $field['name']; } // return return $field; }
function delete_row($selector, $row = 1, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // bail early if no field if (!$field) { return false; } // get value $rows = acf_get_value($post_id, $field); // bail early if no value if (empty($rows)) { return false; } // deincrement if ($row == count($rows)) { acf_update_metadata($post_id, $field['name'], $row - 1); } // update sub field values foreach ($rows[0] as $k => $v) { update_sub_field(array($field['key'], $row, $k), null, $post_id); } // return return true; }
function wp_post_revision_field($value, $field_name, $post = null, $direction = false) { // bail ealry if is empty if (empty($value)) { return $value; } // value has not yet been 'maybe_unserialize' $value = maybe_unserialize($value); // vars $post_id = $post->ID; // load field $field = acf_maybe_get_field($field_name, $post_id); // default formatting if (is_array($value)) { $value = implode(', ', $value); } elseif (is_object($value)) { $value = serialize($value); } // image if ($field['type'] == 'image' || $field['type'] == 'file') { $url = wp_get_attachment_url($value); $value = $value . ' (' . $url . ')'; } // return return $value; }
function render_form($args = array()) { // vars $url = acf_get_current_url(); // defaults $args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', $url), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp', 'honeypot' => true)); $args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => $args['id'], 'class' => 'acf-form', 'action' => '', 'method' => 'post')); // filter post_id $args['post_id'] = acf_get_valid_post_id($args['post_id']); // load values from this post $post_id = $args['post_id']; // new post? if ($post_id == 'new_post') { // dont load values $post_id = false; // new post defaults $args['new_post'] = wp_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft')); } // register local fields foreach ($this->fields as $k => $field) { acf_add_local_field($field); } // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { // load local field $_post_title = acf_get_field('_post_title'); $_post_title['value'] = $post_id ? get_post_field('post_title', $post_id) : ''; // append $fields[] = $_post_title; } // post_content if ($args['post_content']) { // load local field $_post_content = acf_get_field('_post_content'); $_post_content['value'] = $post_id ? get_post_field('post_content', $post_id) : ''; // append $fields[] = $_post_content; } // specific fields if ($args['fields']) { foreach ($args['fields'] as $selector) { // append field ($strict = false to allow for better compatibility with field names) $fields[] = acf_maybe_get_field($selector, $post_id, false); } } elseif ($args['field_groups']) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups($args['new_post']); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } // honeypot if ($args['honeypot']) { $fields[] = acf_get_field('_validate_email'); } // updated message if (!empty($_GET['updated']) && $args['updated_message']) { echo '<div id="message" class="updated"><p>' . $args['updated_message'] . '</p></div>'; } // uploader (always set incase of multiple forms on the page) acf_update_setting('uploader', $args['uploader']); // display form if ($args['form']) { ?> <form <?php acf_esc_attr_e($args['form_attributes']); ?> > <?php } // render post data acf_form_data(array('post_id' => $args['post_id'], 'nonce' => 'acf_form')); ?> <div class="acf-hidden"> <?php acf_hidden_input(array('name' => '_acf_form', 'value' => base64_encode(json_encode($args)))); ?> </div> <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?> "> <?php // html before fields echo $args['html_before_fields']; // render acf_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php if ($args['form']) { ?> <div class="acf-form-submit"> <input type="submit" class="acf-button button button-primary button-large" value="<?php echo $args['submit_value']; ?> " /> <span class="acf-spinner"></span> </div> </form> <?php } }
public function render() { acf_enqueue_scripts(); do_action('acf/view', $this->args, $this); $post_id = $this->post_id; $args = $this->args; // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { $fields[] = acf_get_valid_field(array('name' => '_post_title', 'label' => 'Title', 'type' => 'text', 'value' => $post_id ? get_post_field('post_title', $post_id) : '', 'required' => true)); } // post_content if ($args['post_content']) { $fields[] = acf_get_valid_field(array('name' => '_post_content', 'label' => 'Content', 'type' => 'wysiwyg', 'value' => $post_id ? get_post_field('post_content', $post_id) : '')); } // specific fields if ($args['fields']) { foreach ($args['fields'] as $selector) { // append field ($strict = false to allow for better compatibility with field names) $fields[] = acf_maybe_get_field($selector, $post_id, false); } } elseif ($args['field_groups']) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups(array('post_type' => $args['new_post']['post_type'])); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } ?> <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?> "> <?php // html before fields echo $args['html_before_fields']; // render acf_views_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php }
function delete_row($selector, $row = 1, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // bail early if no field if (!$field) { return false; } // get value $rows = acf_get_value($post_id, $field); // bail early if no value if (empty($rows)) { return false; } // vars $i = $row - 1; // bail early if row doesn't exist if (empty($rows[$i])) { return false; } // unset unset($rows[$i]); // reindex $rows = array_values($rows); // update acf_update_value($rows, $post_id, $field); // return return true; }