Created
March 6, 2024 02:55
-
-
Save doiftrue/9dc14abf5e56fdb0ba5788679979f44d to your computer and use it in GitHub Desktop.
[wp-kama embed] https://wp-kama.ru/id_740 Example of custom post metabox
This file contains 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
<?php | |
$my_metabox = new My_Best_Metaboxes( 'post' ); | |
$my_metabox->init(); | |
class My_Best_Metaboxes { | |
public $post_type; | |
static $meta_prefix = 'company_address'; | |
public function __construct( $post_type ) { | |
$this->post_type = $post_type; | |
} | |
public function init() { | |
add_action( 'add_meta_boxes', [ $this, 'add_metabox' ] ); | |
add_action( 'save_post_' . $this->post_type, [ $this, 'save_metabox' ] ); | |
add_action( 'admin_print_footer_scripts', [ $this, 'show_assets' ], 10, 999 ); | |
} | |
## Добавляет матабоксы | |
public function add_metabox() { | |
add_meta_box( 'box_info_company', 'Информация о компании', [ | |
$this, | |
'render_metabox', | |
], $this->post_type, 'advanced', 'high' ); | |
} | |
## Отображает метабокс на странице редактирования поста | |
public function render_metabox( $post ) { | |
?> | |
<table class="form-table company-info"> | |
<tr> | |
<th> | |
Адреса компании <span class="dashicons dashicons-plus-alt add-company-address"></span> | |
</th> | |
<td class="company-address-list"> | |
<?php | |
$input = ' | |
<span class="item-address"> | |
<input type="text" name="' . self::$meta_prefix . '[]" value="%s"> | |
<span class="dashicons dashicons-trash remove-company-address"></span> | |
</span> | |
'; | |
$addresses = get_post_meta( $post->ID, self::$meta_prefix, true ); | |
if( is_array( $addresses ) ){ | |
foreach( $addresses as $addr ){ | |
printf( $input, esc_attr( $addr ) ); | |
} | |
} | |
else{ | |
printf( $input, '' ); | |
} | |
?> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
## Очищает и сохраняет значения полей | |
public function save_metabox( $post_id ) { | |
if( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ){ | |
return; | |
} | |
if( ! isset( $_POST[ self::$meta_prefix ] ) ){ | |
return; | |
} | |
$addresses = $_POST[ self::$meta_prefix ]; | |
// очистка | |
$addresses = array_map( 'sanitize_text_field', $addresses ); | |
// уберем пустые адреса | |
$addresses = array_filter( $addresses ); | |
if( $addresses ){ | |
update_post_meta( $post_id, self::$meta_prefix, $addresses ); | |
} | |
else{ | |
delete_post_meta( $post_id, self::$meta_prefix ); | |
} | |
} | |
## Подключает скрипты и стили | |
public function show_assets() { | |
if( is_admin() && get_current_screen()->id == $this->post_type ){ | |
$this->show_styles(); | |
$this->show_scripts(); | |
} | |
} | |
## Выводит на экран стили | |
public function show_styles() { | |
?> | |
<style> | |
.add-company-address{ | |
color: #00a0d2; | |
cursor: pointer; | |
} | |
.company-address-list .item-address{ | |
display: flex; | |
align-items: center; | |
} | |
.company-address-list .item-address input{ | |
width: 100%; | |
max-width: 400px; | |
} | |
.remove-company-address{ | |
color: brown; | |
cursor: pointer; | |
} | |
</style> | |
<?php | |
} | |
## Выводит на экран JS | |
public function show_scripts() { | |
?> | |
<script> | |
jQuery( document ).ready( function( $ ){ | |
var $companyInfo = $( '.company-info' ); | |
// Добавляет бокс с вводом адреса фирмы | |
$( '.add-company-address', $companyInfo ).click( function(){ | |
var $list = $( '.company-address-list' ); | |
$item = $list.find( '.item-address' ).first().clone(); | |
$item.find( 'input' ).val( '' ); // чистим знанчение | |
$list.append( $item ); | |
} ); | |
// Удаляет бокс с вводом адреса фирмы | |
$companyInfo.on( 'click', '.remove-company-address', function(){ | |
if( $( '.item-address' ).length > 1 ){ | |
$( this ).closest( '.item-address' ).remove(); | |
} | |
else{ | |
$( this ).closest( '.item-address' ).find( 'input' ).val( '' ); | |
} | |
} ); | |
} ); | |
</script> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment