(Wiki página de Backend Frontend Template Pro: the WordPress Plugin Template)
WordPress permite almacenar datos con su sistema de ajustes, gracias a BFT Pro puedes crear páginas de ajustes con sus campos de datos:
Edita tus pagínas de ajustes de WordPress en la variable $this->admin_settings del archivo admin/class-your-plugin-admin.php
En cada página describimos los campos, y varias páginas pueden repetir los mismos campos
Diseñar Ajustes BFT se ve así: $this->admin_settings = [ “general” => [ “title” => $this->__(“Test settings”), “fields” => [ “text_test” => [ “title” => $this->__(“Text input”), ], “number_test” => [ “title” => $this->__(“Number input”), “args” => [ “type” => “number”, ], ], ], ], ];
Eplicación del menú BFT:
- Páginas de ajustes
- Parámetros automáticos añadidos al array
- id: la clave del array
- id_wordpress: $this->admin_pages_settings_prefix.”_”.array key
- page: id_wordpress.$this->admin_pages_settings_page_suffix
- Parámetros con datos por defecto si no están
- id: la clave del array
- function, por defecto: $this->settings_section_function_default
- fields, por defecto: array vacío
- title, por defecto: $this->admin_pages_settings_page_title_default
- Parámetros automáticos añadidos al array
- Campo de la página
- Parámetros automáticos añadidos automáticamente en la sección de campos del array
- id: la clave del array
- id_wordpress: $this->plugin_slug.”_”.the array key
- Parámetros con datos por defecto si no están
- title, por defecto: clave del array
- function, por defecto: settings_section_form_field()
- args, array donde definimos el tipo de datos, si es múltiple, las opciones del select, etc. Por defecto: “type” => “text”
“args” => [ “type” => “text” ],
- Tipos de entradas
- text: input text
“text_test” => [ “title” => $this->__(“Text input”), ], - textarea: input textarea, rows y cols son opcionales
“textarea_test” => [ “title” => $this->__(“Textarea input”), “args” => [ “type” => “textarea”, “rows” => 10, “cols” => 50, ], ], - number: input number, args opcionales: min, max y step. Por defeto para step: 1, alternativo: ‘decimal’ o ‘decimals’, esta opción calcula los steps/pasos necesarios
“number_test” => [ “title” => $this->__(“Number input”), “args” => [ “type” => “number”, “min” => 1, “max” => 99999, “step” => 1, ], ], - select: input select, args debe tener ‘options’, guarda cadena con las opciones seleccionadas, ej: ‘en,es’ , args opcionales: search (por defecto false), multiple (por defecto false)
“select_test” => [ “title” => $this->__(“Select input”), “args” => [ “type” => “select”, “search” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], “select_multiple_test” => [ “title” => $this->__(“Select multiple input”), “args” => [ “type” => “select”, “multiple” => true, “search” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], - date: input text con un calendario, la fecha será estilo MySQL ‘2023-01-01’
“date_test” => [ “title” => $this->__(“Date input”), “args” => [ “type” => “date”, ], ], - checkbox, single y multiple, en multiple es necesario ‘options’ en args
“checkbox_test” => [ “title” => $this->__(“Checkbox input”), “args” => [ “type” => “checkbox”, “multiple” => false, ], ], “checkbox_multiple_test” => [ “title” => $this->__(“Checkbox multiple input”), “args” => [ “type” => “checkbox”, “multiple” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], - radio: radio buttons, guarda cadena con las opciones seleeccionadas, ej: ‘en,es’, requiere ‘options’
“radio_test” => [ “title” => $this->__(“Radio input”), “args” => [ “type” => “radio”, “multiple” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], - image: guarda una imagen en la Biblioteca de Medios de WordPress, el dato al almacenado en los ajustes es el ID de WordPress del archivo. Una vez seleccionado muestra la imagen
“image_test” => [ “title” => $this->__(“Image input”), “args” => [ “type” => “image”, ], ], - file: guarda una archivo en la Biblioteca de Medios de WordPress, el dato al almacenado en los ajustes es el ID de WordPress del archivo. Una vez seleccionado muestraun botón de descarga
“file_test” => [ “title” => $this->__(“File input”), “args” => [ “type” => “file”, ], ], - empty: un input comentado, imprime ”
“empty_test” => [ “title” => $this->__(“Input commented”), “args” => [ “type” => “empty”, ], ],
- text: input text
- Parámetros automáticos añadidos automáticamente en la sección de campos del array
- Cómo los campos son guardados
- En el array el campo ID es una fase simple, ej: ‘languages’, ‘currency’, etc., pero BFT lo almacena como: $this->plugin_slug.”_”.”field_id”, entonces será: ‘your_plugin_languages’, ‘your_plugin_currency’, etc.
- Eso es porque WordPress no aisla los ajustes por plugins, el propio plugin necesita aislar y asegurar los ajustes
- Sabiendo esto, hay dos estilos de acceso para actualizar y eliminar los datos por código:
- Estilo BFT
- $a_variable = $this->option_field_get(“field_id”)
- $this->update_option_field(“field_id”, “nuevo valor de la cadena”)
- $this->delete_option_field(“field_id”)
- Estilo WordPress
- $a_variable = get_option(“plugin_slug”.”_”.”field_id”);
- update_option(“plugin_slug”.”_”.”field_id”, “nuevo valor de la cadena”)
- delete_option(“plugin_slug”.”_”.”field_id”);
- Estilo BFT
Un ejemplo con todos los tipos de input: $this->admin_settings = [ “input_types_test” => [ “title” => $this->__(“Test settings”), “fields” => [ “text_test” => [ “title” => $this->__(“Text input”), ], “textarea_test” => [ “title” => $this->__(“Textarea input”), “args” => [ “type” => “textarea”, “rows” => 10, “cols” => 50, ], ], “number_test” => [ “title” => $this->__(“Number input”), “args” => [ “type” => “number”, “min” => 1, “max” => 99999, “step” => 1, ], ], “select_test” => [ “title” => $this->__(“Select input”), “args” => [ “type” => “select”, “search” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], “select_multiple_test” => [ “title” => $this->__(“Select multiple input”), “args” => [ “type” => “select”, “multiple” => true, “search” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], “date_test” => [ “title” => $this->__(“Date input”), “args” => [ “type” => “date”, ], ], “checkbox_test” => [ “title” => $this->__(“Checkbox input”), “args” => [ “type” => “checkbox”, “multiple” => false, ], ], “checkbox_multiple_test” => [ “title” => $this->__(“Checkbox multiple input”), “args” => [ “type” => “checkbox”, “multiple” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], “radio_test” => [ “title” => $this->__(“Radio input”), “args” => [ “type” => “radio”, “multiple” => true, “options” => [ “zh” => $this->__(“Chinesse”), “en” => $this->__(“English”), “es” => $this->__(“Spanish”), ], ], ], “image_test” => [ “title” => $this->__(“Image input”), “args” => [ “type” => “image”, ], ], “file_test” => [ “title” => $this->__(“File input”), “args” => [ “type” => “file”, ], ], “empty_test” => [ “title” => $this->__(“Input commented”), “args” => [ “type” => “empty”, ], ], ], ], ];



0 comentarios