Rütimattli_Metabox
class Rm_joboffer_Meta_Box {
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(
'Rm_joboffer_meta_box_shortcode',
'Shortcode',
array( $this, 'render_metabox' ),
'rm_jobofffer',
'side',
'high'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$rm_joboffer_ = get_post_meta( $post->ID, 'rm_joboffer_', true );
// Set default values.
if( empty( $rm_joboffer_ ) ) $rm_joboffer_ = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="rm_joboffer_" class="rm_joboffer__label">' . 'Shortcode' . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="rm_joboffer_" name="rm_joboffer_" class="rm_joboffer__field" placeholder="' . '' . '" value="' . esc_attr( $rm_joboffer_ ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$rm_joboffer_new_ = isset( $_POST[ 'rm_joboffer_' ] ) ? sanitize_text_field( $_POST[ 'rm_joboffer_' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'rm_joboffer_', $rm_joboffer_new_ );
}
}
new Rm_joboffer_Meta_Box;