myplodiday
class myholidays {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'myHoliday',
__( '休日設定', 'ai-bridge=bs5' ),
array( $this, 'render_metabox' ),
'paig',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$custom_hokidaystart = get_post_meta( $post->ID, 'custom_hokidaystart', true );
$custom_hoiday_end = get_post_meta( $post->ID, 'custom_hoiday_end', true );
// Set default values.
if( empty( $custom_hokidaystart ) ) $custom_hokidaystart = '';
if( empty( $custom_hoiday_end ) ) $custom_hoiday_end = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_hokidaystart" class="custom_hokidaystart_label">' . __( '', 'ai-bridge=bs5' ) . '</label></th>';
echo ' <td>';
echo ' <input type="date" id="custom_hokidaystart" name="custom_hokidaystart" class="custom_hokidaystart_field" placeholder="' . esc_attr__( '', 'ai-bridge=bs5' ) . '" value="' . esc_attr( $custom_hokidaystart ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_hoiday_end" class="custom_hoiday_end_label">' . __( '休み終了', 'ai-bridge=bs5' ) . '</label></th>';
echo ' <td>';
echo ' <input type="date" id="custom_hoiday_end" name="custom_hoiday_end" class="custom_hoiday_end_field" placeholder="' . esc_attr__( '', 'ai-bridge=bs5' ) . '" value="' . esc_attr( $custom_hoiday_end ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Sanitize user input.
$custom_new_hokidaystart = isset( $_POST[ 'custom_hokidaystart' ] ) ? sanitize_text_field( $_POST[ 'custom_hokidaystart' ] ) : '';
$custom_new_hoiday_end = isset( $_POST[ 'custom_hoiday_end' ] ) ? sanitize_text_field( $_POST[ 'custom_hoiday_end' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_hokidaystart', $custom_new_hokidaystart );
update_post_meta( $post_id, 'custom_hoiday_end', $custom_new_hoiday_end );
}
}
new myholidays;