Untitled
unknown
plain_text
2 years ago
9.1 kB
7
Indexable
<strong>Register CPT</strong>
<code>
function mp_cpt_people() {
$labels = array(
'name' => _x( 'Persons', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Person', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'People', 'text_domain' ),
'name_admin_bar' => __( 'People', 'text_domain' ),
'archives' => __( 'People Archives', 'text_domain' ),
'attributes' => __( 'People Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All People', 'text_domain' ),
'add_new_item' => __( 'Add New People', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New People', 'text_domain' ),
'edit_item' => __( 'Edit People', 'text_domain' ),
'update_item' => __( 'Update People', 'text_domain' ),
'view_item' => __( 'View People', 'text_domain' ),
'view_items' => __( 'View People', 'text_domain' ),
'search_items' => __( 'Search People', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Person Photo', 'text_domain' ),
'set_featured_image' => __( 'Set person photo', 'text_domain' ),
'remove_featured_image' => __( 'Remove person photo', 'text_domain' ),
'use_featured_image' => __( 'Use as person photo', 'text_domain' ),
'insert_into_item' => __( 'Insert into person', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this person', 'text_domain' ),
'items_list' => __( 'People list', 'text_domain' ),
'items_list_navigation' => __( 'People list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter people list', 'text_domain' ),
);
$rewrite = array(
'slug' => 'personnel',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'Person', 'text_domain' ),
'description' => __( 'Handles the resumes of the persons in CYENS', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
'taxonomies' => array( 'department' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-businessperson',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'people', $args );
}
add_action( 'init', 'mp_cpt_people', 0 );
</code>
<strong>Register Custom Taxonomy</strong>
<code>
function mp_department() {
$labels = array(
'name' => _x( 'Selected Department', 'taxonomy general name' ),
'singular_name' => _x( 'Department', 'taxonomy singular name' ),
'search_items' => __( 'Search Department' ),
'all_items' => __( 'All Departments' ),
'parent_item' => __( 'Parent Department' ),
'parent_item_colon' => __( 'Parent Department:' ),
'edit_item' => __( 'Edit Department' ),
'update_item' => __( 'Update Department' ),
'add_new_item' => __( 'Add New Department' ),
'new_item_name' => __( 'New Department Name' ),
'menu_name' => __( 'Departments' ),
);
register_taxonomy('department',['people'], array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'department' ),
));
}
add_action( 'init', 'mp_department', 0 );
</code>
<strong>Register Custom Fields</strong>
<code>
class Person_Details {
private $config = '{"title":"Person Details","prefix":"person_details_","domain":"person-details","class_name":"Person_Details","post-type":["people"],"context":"normal","priority":"default","cpt":"people","fields":[{"type":"text","label":"Position to CYENS","id":"person_details_position-to-cyens"},{"type":"textarea","label":"Profession","rows":"2","id":"person_details_profession"},{"type":"text","label":"Company","id":"person_details_company"},{"type":"editor","label":"Introduction","rows":"5","teeny":"1","id":"person_details_intro"},{"type":"editor","label":"Email","rows":"1","teeny":"1","id":"person_details_email"},{"type":"editor","label":"LinkedIn","rows":"1","teeny":"1","id":"person_details_linkedin"},{"type":"editor","label":"Website","rows":"1","teeny":"1","id":"person_details_website"},{"type":"editor","label":"Facebook","rows":"1","teeny":"1","id":"person_details_facebook"},{"type":"editor","label":"Instagram","rows":"1","teeny":"1","id":"person_details_instagram"}]}';
public function __construct() {
$this->config = json_decode( $this->config, true );
$this->process_cpts();
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'save_post', [ $this, 'save_post' ] );
}
public function process_cpts() {
if ( !empty( $this->config['cpt'] ) ) {
if ( empty( $this->config['post-type'] ) ) {
$this->config['post-type'] = [];
}
$parts = explode( ',', $this->config['cpt'] );
$parts = array_map( 'trim', $parts );
$this->config['post-type'] = array_merge( $this->config['post-type'], $parts );
}
}
public function add_meta_boxes() {
foreach ( $this->config['post-type'] as $screen ) {
add_meta_box(
sanitize_title( $this->config['title'] ),
$this->config['title'],
[ $this, 'add_meta_box_callback' ],
$screen,
$this->config['context'],
$this->config['priority']
);
}
}
public function save_post( $post_id ) {
foreach ( $this->config['fields'] as $field ) {
switch ( $field['type'] ) {
case 'editor':
if ( isset( $_POST[ $field['id'] ] ) ) {
$sanitized = wp_filter_post_kses( $_POST[ $field['id'] ] );
update_post_meta( $post_id, $field['id'], $sanitized );
}
break;
default:
if ( isset( $_POST[ $field['id'] ] ) ) {
$sanitized = sanitize_text_field( $_POST[ $field['id'] ] );
update_post_meta( $post_id, $field['id'], $sanitized );
}
}
}
}
public function add_meta_box_callback() {
$this->fields_table();
}
private function fields_table() {
?><table class="form-table" role="presentation">
<tbody><?php
foreach ( $this->config['fields'] as $field ) {
?><tr>
<th scope="row"><?php $this->label( $field ); ?></th>
<td><?php $this->field( $field ); ?></td>
</tr><?php
}
?></tbody>
</table><?php
}
private function label( $field ) {
switch ( $field['type'] ) {
case 'editor':
echo '<div class="">' . $field['label'] . '</div>';
break;
default:
printf(
'<label class="" for="%s">%s</label>',
$field['id'], $field['label']
);
}
}
private function field( $field ) {
switch ( $field['type'] ) {
case 'editor':
$this->editor( $field );
break;
case 'textarea':
$this->textarea( $field );
break;
default:
$this->input( $field );
}
}
private function editor( $field ) {
wp_editor( $this->value( $field ), $field['id'], [
'wpautop' => isset( $field['wpautop'] ) ? true : false,
'media_buttons' => isset( $field['media-buttons'] ) ? true : false,
'textarea_name' => $field['id'],
'textarea_rows' => isset( $field['rows'] ) ? isset( $field['rows'] ) : 20,
'teeny' => isset( $field['teeny'] ) ? true : false
] );
}
private function input( $field ) {
printf(
'<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">',
isset( $field['class'] ) ? $field['class'] : '',
$field['id'], $field['id'],
isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '',
$field['type'],
$this->value( $field )
);
}
private function textarea( $field ) {
printf(
'<textarea class="regular-text" id="%s" name="%s" rows="%d">%s</textarea>',
$field['id'], $field['id'],
isset( $field['rows'] ) ? $field['rows'] : 5,
$this->value( $field )
);
}
private function value( $field ) {
global $post;
if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) {
$value = get_post_meta( $post->ID, $field['id'], true );
} else if ( isset( $field['default'] ) ) {
$value = $field['default'];
} else {
return '';
}
return str_replace( '\u0027', "'", $value );
}
}
new Person_Details;
</code>
Editor is loading...