'Options BETA', 'menu_title' => 'Options', 'menu_slug' => 'acfe-options', 'post_id' => 'acfe-options', 'parent_slug' => 'options-general.php', 'capability' => 'manage_options', 'redirect' => false )); } add_action('admin_init', 'acfe_options_field_group'); function acfe_options_field_group(){ global $pagenow; if(empty($pagenow) || $pagenow != 'options-general.php' || !isset($_GET['page']) || $_GET['page'] != 'acfe-options') return; global $wpdb; $options = $wpdb->get_results("SELECT * FROM $wpdb->options ORDER BY option_name"); if(empty($options)) return; $acfe_options = array(); foreach((array) $options as $option){ if($option->option_name == '') continue; $option_name = esc_attr($option->option_name); $acfe_options[] = $option_name; } update_option('acfe_options', $acfe_options); $options = $wpdb->get_results("SELECT * FROM $wpdb->options ORDER BY option_name"); $fields = array(); foreach((array) $options as $option){ if($option->option_name == '') continue; $option_name = esc_attr($option->option_name); $option_value = $option->option_value; $fields[] = array( 'field_acfe_option_name' => $option_name, 'field_acfe_option_value' => $option_value, ); } acf_add_local_field_group(array( 'key' => 'acfe_group_options', 'title' => 'Options', 'location' => array( array( array( 'param' => 'options_page', 'operator' => '==', 'value' => 'acfe-options', ), ), ), 'menu_order' => 0, 'position' => 'normal', 'style' => 'seamless', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => '', 'active' => 1, 'description' => '', 'fields' => array( array( 'label' => '', 'key' => 'field_acfe_options_repeater', 'name' => 'acfe_options_repeater', 'type' => 'repeater', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'collapsed' => '', 'min' => 0, 'max' => 0, 'layout' => 'table', 'button_label' => __('Add option'), 'value' => $fields, 'sub_fields' => array( array( 'label' => 'Option name', 'key' => 'field_acfe_option_name', 'name' => 'acfe_field_option_name', 'type' => 'text', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => 33, 'class' => '', 'id' => '', ), 'acfe_validate' => '', 'acfe_update' => '', 'acfe_permissions' => '', 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', ), array( 'label' => 'Option value', 'key' => 'field_acfe_option_value', 'name' => 'acfe_option_value', 'type' => 'text', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), 'acfe_validate' => '', 'acfe_update' => '', 'acfe_permissions' => '', 'default_value' => '', 'placeholder' => '', 'prepend' => '', 'append' => '', 'maxlength' => '', ), ), ), ), )); } add_filter('acf/prepare_field/key=field_acfe_option_value', 'acfe_options_prepare_value'); function acfe_options_prepare_value($field){ if(is_serialized($field['value'])){ $field['type'] = 'textarea'; $field['rows'] = 8; $field['disabled'] = true; $field['readonly'] = true; $field['value'] = print_r(maybe_unserialize($field['value']), true); } return $field; } add_action('acf/save_post', 'acfe_options_save', 0); function acfe_options_save($post_id){ if($_POST['_acf_post_id'] != 'acfe-options' || empty($_POST['acf'])) return; $acfe_options = get_option('acfe_options', array()); foreach($_POST['acf']['field_acfe_options_repeater'] as $option){ $option_key = array_search($option['field_acfe_option_name'], $acfe_options); if($option_key !== false) unset($acfe_options[$option_key]); if(!isset($option['field_acfe_option_value'])) continue; update_option($option['field_acfe_option_name'], stripslashes($option['field_acfe_option_value'])); } if(!empty($acfe_options)){ foreach($acfe_options as $option){ delete_option($option); } } $_POST['acf'] = array(); return; }