Created
June 10, 2022 12:02
-
-
Save sododesign/c49472af75c17e5ca0aac2ece30eb7c4 to your computer and use it in GitHub Desktop.
METABOX CUSTOM FRONTEND
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CUSTOM FIELDS | |
add_filter( 'rwmb_meta_boxes', 'experience_cpt_meta_boxes2' ); | |
function experience_cpt_meta_boxes2( $meta_boxes ) { | |
if( is_admin() ) | |
return; | |
// CREATE TAXONOMY | |
$meta_boxes[] = array( | |
'title' => __('Crea Taxonomy','appgenova'), | |
'id' => 'taxonomy-create', | |
'fields' => array( | |
array( | |
'id' => 'object_id_app', | |
'type' => 'hidden', | |
), | |
array( | |
'id' => 'title', | |
'name' => __('Titolo','appgenova'), | |
'type' => 'text', | |
), | |
array( | |
'id' => 'description', | |
'name' => __('Descrizione','appgenova'), | |
'type' => 'text', | |
), | |
), | |
); | |
return $meta_boxes; | |
} | |
add_action( 'init', 'test', 10); | |
function test() { | |
// nonce_taxonomy-create | |
$id_metabox = 'taxonomy-create'; | |
if ( isset($_POST['nonce_'.$id_metabox]) && wp_verify_nonce( $_POST['nonce_'.$id_metabox], 'rwmb-save-'.$id_metabox ) ){ | |
$page_app = get_pages_app(); | |
$object_id_app = (isset($_POST['object_id_app']) && !empty($_POST['object_id_app']) ) ? $_POST['object_id_app'] : ''; | |
if( empty($object_id_app) ){ | |
// NEW TERM | |
$new_term = wp_insert_term( | |
$_POST['title'], // the term | |
'cat_experience', // the taxonomy | |
array( | |
'description' => $_POST['description'], | |
'slug' => sanitize_title($_POST['title']), | |
) | |
); | |
} else { | |
// UPDATE TERM | |
$update = wp_update_term( $object_id_app, 'cat_experience', array( | |
'name' => $_POST['title'], | |
'slug' => sanitize_title($_POST['title']), | |
'description' => $_POST['description'], | |
)); | |
} | |
wp_redirect( esc_url(home_url($_POST['_wp_http_referer'])) ); | |
die(); | |
} | |
} | |
// LOAD VALUE | |
add_filter('rwmb_object_id_app_field_meta','test_3', 10, 3); | |
function test_3($value, $field, $saved ){ | |
$id_tax = get_query_var('rwmb_frontend_field_tax_id'); | |
if( !empty($id_tax) ){ | |
$value = ( current_user_can('manage_options') ) ? $id_tax : ''; | |
} | |
return $value; | |
} | |
add_filter('rwmb_title_field_meta','test_2', 10, 3); | |
function test_2($value, $field, $saved ){ | |
$id_tax = get_query_var('rwmb_frontend_field_tax_id'); | |
if( !empty($id_tax) ){ | |
$term = get_term( $id_tax ); | |
$value = $term->name; | |
} | |
return $value; | |
} | |
add_filter('rwmb_description_field_meta','test_4', 10, 3); | |
function test_4($value, $field, $saved ){ | |
$id_tax = get_query_var('rwmb_frontend_field_tax_id'); | |
if( !empty($id_tax) ){ | |
$term = get_term( $id_tax ); | |
$value = $term->description; | |
} | |
return $value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment