Lex Meta Box
class Lex_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(
'lexmb',
__( 'Lex Meta Box', 'text_domain' ),
array( $this, 'render_metabox' ),
'inv_funds',
'normal',
'core'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$lex_ero_url = get_post_meta( $post->ID, 'lex_ero_url', true );
// Set default values.
if( empty( $lex_ero_url ) ) $lex_ero_url = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="lex_ero_url" class="lex_ero_url_label">' . __( 'ERO URL', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="lex_ero_url" name="lex_ero_url" class="lex_ero_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $lex_ero_url ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$lex_new_ero_url = isset( $_POST[ 'lex_ero_url' ] ) ? esc_url( $_POST[ 'lex_ero_url' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'lex_ero_url', $lex_new_ero_url );
}
}
new Lex_Meta_Box;