data = array( 'page_id' => '', // a string used to load values 'metabox_ids' => array(), 'page_type' => '', // taxonomy / user / media 'page_action' => '', // add / edit 'option_name' => '', // key used to find value in wp_options table. eg: user_1, category_4 ); // actions add_action('admin_menu', array($this,'admin_menu')); add_action('wp_ajax_acf/everything_fields', array($this, 'acf_everything_fields')); // attachment add_filter('attachment_fields_to_edit', array($this, 'attachment_fields_to_edit'), 10, 2); add_filter('attachment_fields_to_save', array($this, 'save_attachment'), 10, 2); // save add_action('create_term', array($this, 'save_taxonomy')); add_action('edited_term', array($this, 'save_taxonomy')); add_action('edit_user_profile_update', array($this, 'save_user')); add_action('personal_options_update', array($this, 'save_user')); add_action('user_register', array($this, 'save_user')); // shopp add_action('shopp_category_saved', array($this, 'shopp_category_saved')); // delete add_action('delete_term', array($this, 'delete_term'), 10, 4); } /* * attachment_fields_to_edit * * Adds ACF fields to the attachment form fields * * @type filter * @date 14/07/13 * * @param {array} $form_fields * @return {object} $post */ function attachment_fields_to_edit( $form_fields, $post ) { // vars $screen = get_current_screen(); $post_id = $post->ID; if( $screen && $screen->base == 'post' ) { return $form_fields; } // get field groups $filter = array( 'post_type' => 'attachment' ); $metabox_ids = array(); $metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter ); // validate if( empty($metabox_ids) ) { return $form_fields; } $acfs = apply_filters('acf/get_field_groups', array()); if( is_array($acfs) ){ foreach( $acfs as $acf ){ // only add the chosen field groups if( !in_array( $acf['id'], $metabox_ids ) ) { continue; } // load fields $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']); if( is_array($fields) ){ foreach( $fields as $i => $field ){ // if they didn't select a type, skip this field if( !$field || !$field['type'] || $field['type'] == 'null' ) { continue; } // set value if( !isset($field['value']) ) { $field['value'] = apply_filters('acf/load_value', false, $post_id, $field); $field['value'] = apply_filters('acf/format_value', $field['value'], $post_id, $field); } // create field $field['name'] = 'fields[' . $field['key'] . ']'; ob_start(); do_action('acf/create_field', $field); $html = ob_get_contents(); ob_end_clean(); $form_fields[ $field['name'] ] = array( 'label' => $field['label'], 'input' => 'html', 'html' => $html ); }}; }} // return return $form_fields; } /* * save_attachment * * Triggers the acf/save_post action * * @type action * @date 14/07/13 * * @param {array} $post * @return {array} $attachment */ function save_attachment( $post, $attachment ) { // verify nonce /* if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') ) { return $post; } */ // $post_id to save against $post_id = $post['ID']; // update the post do_action('acf/save_post', $post_id); return $post; } /* * validate_page * * @description: returns true | false. Used to stop a function from continuing * @since 3.2.6 * @created: 23/06/12 */ function validate_page() { // global global $pagenow, $wp_version; // vars $return = false; // validate page if( in_array( $pagenow, array( 'edit-tags.php', 'term.php', 'profile.php', 'user-new.php', 'user-edit.php', 'media.php' ) ) ) { $return = true; } // validate page (Shopp) if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" ) { $return = true; } // WP4 if( $pagenow === 'upload.php' && version_compare($wp_version, '4.0', '>=') ) { $return = true; } // return return $return; } /*-------------------------------------------------------------------------------------- * * admin_menu * * @author Elliot Condon * @since 3.1.8 * *-------------------------------------------------------------------------------------*/ function admin_menu() { global $pagenow; // validate page if( ! $this->validate_page() ) return; // set page type $filter = array(); if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" ) { // filter $_GET['id'] = filter_var($_GET['id'], FILTER_SANITIZE_STRING); $this->data['page_type'] = "shopp_category"; $filter['ef_taxonomy'] = "shopp_category"; $this->data['page_action'] = "add"; $this->data['option_name'] = ""; if( $_GET['id'] != "new" ) { $this->data['page_action'] = "edit"; $this->data['option_name'] = "shopp_category_" . $_GET['id']; } } elseif( $pagenow == "edit-tags.php" || $pagenow == "term.php" ) { // vars $taxonomy = 'post_tag'; $term_id = 0; // $_GET if( !empty($_GET['taxonomy']) ) { $taxonomy = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING); } if( !empty($_GET['tag_ID']) ) { $term_id = filter_var($_GET['tag_ID'], FILTER_SANITIZE_NUMBER_INT); } // update filter $filter['ef_taxonomy'] = $taxonomy; // add $this->data['page_type'] = "taxonomy"; $this->data['page_action'] = "add"; $this->data['option_name'] = ""; // edit if( $term_id ) { $this->data['page_action'] = "edit"; $this->data['option_name'] = $taxonomy . "_" . $term_id; } } elseif( $pagenow == "profile.php" ) { $this->data['page_type'] = "user"; $filter['ef_user'] = get_current_user_id(); $this->data['page_action'] = "edit"; $this->data['option_name'] = "user_" . get_current_user_id(); } elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) ) { // filter $_GET['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT); $this->data['page_type'] = "user"; $filter['ef_user'] = $_GET['user_id']; $this->data['page_action'] = "edit"; $this->data['option_name'] = "user_" . $_GET['user_id']; } elseif( $pagenow == "user-new.php" ) { $this->data['page_type'] = "user"; $filter['ef_user'] ='all'; $this->data['page_action'] = "add"; $this->data['option_name'] = ""; } elseif( $pagenow == "media.php" || $pagenow == 'upload.php' ) { $this->data['page_type'] = "media"; $filter['post_type'] = 'attachment'; $this->data['page_action'] = "add"; $this->data['option_name'] = ""; if(isset($_GET['attachment_id'])) { // filter $_GET['attachment_id'] = filter_var($_GET['attachment_id'], FILTER_SANITIZE_NUMBER_INT); $this->data['page_action'] = "edit"; $this->data['option_name'] = $_GET['attachment_id']; } } // get field groups $metabox_ids = array(); $this->data['metabox_ids'] = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter ); // dont continue if no ids were found if( empty( $this->data['metabox_ids'] ) ) { return false; } // actions add_action('admin_enqueue_scripts', array($this,'admin_enqueue_scripts')); add_action('admin_head', array($this,'admin_head')); } /* * admin_enqueue_scripts * * @description: * @since: 3.6 * @created: 30/01/13 */ function admin_enqueue_scripts() { do_action('acf/input/admin_enqueue_scripts'); } /*-------------------------------------------------------------------------------------- * * admin_head * * @author Elliot Condon * @since 3.1.8 * *-------------------------------------------------------------------------------------*/ function admin_head() { global $pagenow; // add user js + css do_action('acf/input/admin_head'); ?> id; // update the post do_action('acf/save_post', $post_id); } /*-------------------------------------------------------------------------------------- * * acf_everything_fields * * @description Ajax call that renders the html needed for the page * @author Elliot Condon * @since 3.1.8 * *-------------------------------------------------------------------------------------*/ function acf_everything_fields() { // defaults $defaults = array( 'metabox_ids' => '', 'page_type' => '', 'page_action' => '', 'option_name' => '', ); // load post options $options = array_merge($defaults, $_POST); // metabox ids is a string with commas $options['metabox_ids'] = explode( ',', $options['metabox_ids'] ); // get acfs $acfs = apply_filters('acf/get_field_groups', false); // layout $layout = 'tr'; if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add") { $layout = 'div'; } if( $options['page_type'] == "shopp_category") { $layout = 'metabox'; } if( $acfs ) { foreach( $acfs as $acf ) { // load options $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']); // only add the chosen field groups if( !in_array( $acf['id'], $options['metabox_ids'] ) ) { continue; } // layout dictates heading $title = true; if( $acf['options']['layout'] == 'no_box' ) { $title = false; } // title if( $options['page_action'] == "edit" && $options['page_type'] == 'user' ) { if( $title ) { echo '
'; echo ' | ';
$field['name'] = 'fields[' . $field['key'] . ']';
do_action('acf/create_field', $field );
if($field['instructions']) echo ' ' . $field['instructions'] . ' '; echo ' | ';
echo '
---|