Metabox in the front-end
-
Hi, I am trying to set up a private zone on the front end for clients and I am using cmb2 to add data from each of them.
Create a custom post called “perfiles”.
function post_perfiles(){ $labels = array( 'name' => _x('Perfiles', 'divi'), 'singular_name' => _x('Perfil', 'post type singular name', 'divi'), 'menu_name' => _x('Perfiles', 'admin menu', 'divi'), 'name_admin_bar' => _x('Perfiles', 'add new on admin bar', 'divi'), 'add_new' => _x('A?adir Perfil', 'book', 'divi'), 'add_new_item' => __('A?adir Nuevo Perfil', 'divi'), 'new_item' => __('Nuevos Perfiles', 'divi'), 'edit_item' => __('Editar Perfil', 'divi'), 'view_item' => __('Ver Perfil', 'divi'), 'all_items' => __('Todos los Perfiles', 'divi'), 'search_items' => __('Buscar Perfiles', 'divi'), 'parent_item_colon' => __('Parent Perfiles:', 'divi'), 'not_found' => __('No se han encontrado Perfiles', 'divi'), 'not_found_in_trash' => __('No se han encontrado Perfiles en la papelera', 'divi') ); $args = array( 'labels' => $labels, 'description' => __('Description.', 'divi'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug' => 'perfiles'), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 4, 'supports' => array('title', 'editor', 'author', 'thumbnail'), ); register_post_type('perfiles', $args); } add_action('init', 'post_perfiles'); function mis_taxonomias_perfiles(){ //Tipo de Perfil $labels = array( 'name' => _x('Tipo de Perfil', 'taxonomy general name', 'divi'), 'singular_name' => _x('Tipo de Perfil', 'taxonomy_general_name', 'divi'), 'search_items' => __('Buscar Tipo de Perfil', 'divi'), 'all_items' => __('Todos los Tipos de Perfiles', 'divi'), 'parent_item' => __('Padre Tipo de Perfil', 'divi'), 'parent_item_colon' => __('Padre Tipo de Perfil', 'divi'), 'edit_item' => __('Editar Tipo de Perfil', 'divi'), 'update_item' => __('Actualizar Tipo de Perfil', 'divi'), 'add_new_item' => __('A?adir Nuevo Perfil', 'divi'), 'new_item_name' => __('Nuevo Tipo de Perfil', 'divi'), 'menu_name' => __('Tipo de Perfil', 'divi'), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'tipo_de_perfil'), ); register_taxonomy('tipo-perfil', array('perfiles'), $args); } add_action('init','mis_taxonomias_perfiles');
Register the metabox.
add_action( 'cmb2_init', 'perfil_cliente_register_metabox' ); /** * Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook. */ function perfil_cliente_register_metabox() { // Start with an underscore to hide fields from custom fields list. $prefix = 'perfil_cliente_'; /** * Sample metabox to demonstrate the different conditions you can set. */ $cmb_demo = new_cmb2_box( array( 'id' => $prefix . 'metabox', 'title' => 'Perfil del cliente', 'object_types' => array( 'clientes' ), // Post type. ) ); $cmb_demo->add_field( array( 'name' => 'Nombre', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'nombre', 'type' => 'text_small', ) ); $cmb_demo->add_field( array( 'name' => 'Apellido', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'apellido', 'type' => 'text_small', ) ); $cmb_demo->add_field( array( 'name' => 'Fecha de nacimiento', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'fecha_nacimiento', 'type' => 'text_date', 'date_format' => 'd-m-Y', 'attributes' => array( // CMB2 checks for datepicker override data here: 'data-datepicker' => json_encode( array( 'yearRange' => '-100:+0', 'closeText' => 'Cerrar', 'prevText' => '< Ant', 'nextText' => 'Sig >', 'currentText' => 'Hoy', 'monthNames' => ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'], 'monthNamesShort' => ['Ene','Feb','Mar','Abr', 'May','Jun','Jul','Ago','Sep', 'Oct','Nov','Dic'], 'dayNames' => ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'], 'dayNamesShort' => ['Dom','Lun','Mar','Mié','Juv','Vie','Sáb'], 'dayNamesMin' => ['Do','Lu','Ma','Mi','Ju','Vi','Sá'], 'firstDay' => 1, //Hace que la semana comience en Lunes //'minDate' => 0, //Hace que no se puedan seleccionar fechas pasadas 'dateFormat' => "dd/mm/yy", //Establece el formato de fecha ) ), ), ) ); $cmb_demo->add_field( array( 'name' => 'Genero', 'desc' => 'Elegir una opcion', 'id' => $prefix . 'genero', 'type' => 'select', 'default' => 'elegir', 'options' => array( 'mujer' => __( 'Mujer', 'cmb2' ), 'varon' => __( 'Varón', 'cmb2' ), 'elegir' => __( 'Elegir', 'cmb2' ), ), ) ); $cmb_demo->add_field( array( 'name' => 'Celular', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'celular', 'type' => 'text_small', ) ); $cmb_demo->add_field( array( 'name' => 'Nombre contacto de emergencia', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'nombre_emergencia', 'type' => 'text_small', ) ); $cmb_demo->add_field( array( 'name' => 'Celular contacto de emergencia', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'celular_emergencia', 'type' => 'text_small', ) ); $cmb_demo->add_field( array( 'name' => 'Fecha actual', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'fecha_actual', 'type' => 'text_date', 'date_format' => 'd-m-Y', 'default_cb' => 'set_to_today', 'attributes' => array( 'disabled' => 'disabled', 'readonly' => 'readonly', ), ) ); $cmb_demo->add_field( array( 'name' => 'Edad', 'cmb2', 'desc' => 'field description (optional)', 'cmb2', 'id' => $prefix . 'edad', 'type' => 'text_small', 'default' => '35', 'attributes' => array( 'disabled' => 'disabled', 'readonly' => 'readonly', ), ) ); } function set_to_today( $field_args, $field ) { return date( 'l' ); }
And every time a new user registers metaboxes are added to custom posts “perfiles” with their user_niceman.
add_action( 'user_register', 'agregar_cp_perfiles_usuario', 10, 1 ); function agregar_cp_perfiles_usuario( $user_id ) { // Get user info $user_info = get_userdata( $user_id ); $user_roles = $user_info->roles; // New code added $this_user_role = implode(', ', $user_roles ); if ($this_user_role == 'cliente') { // Create a new post $user_post = array( 'post_title' => $user_info->user_nicename, 'post_author' => $user_info->ID, 'post_status' => 'publish', // <- here is to publish 'post_type' => 'perfiles', // <- change to your cpt ); // Insert the post into the database $post_id = wp_insert_post( $user_post ); } }
Now I would need to see those metaboxes created so that each of them can modify them on the front-end.
I understand so far that I have to create a short code to call it from the front-end private area but I don’t know how to refer to the metabox created in the custom post created with user_niceman.Thanks in advance, I appreciate your time and effort.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Metabox in the front-end’ is closed to new replies.