haffi sponsors metabox
class sponsors_metabox {
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(
'sponsor-metabox',
'Additional Info',
array( $this, 'sponsor_metabox' ),
'Sponsors',
'normal',
'default'
);
}
public function sponsor_metabox( $post ) {
// Retrieve an existing value from the database.
$sponsors_url = get_post_meta( $post->ID, 'sponsors_url', true );
// Set default values.
if( empty( $sponsors_url ) ) $sponsors_url = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sponsors_url" class="sponsors_url_label">' . 'URL' . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="sponsors_url" name="sponsors_url" class="sponsors_url_field" placeholder="' . '' . '" value="' . esc_attr__( $sponsors_url ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$sponsors_new_url = isset( $_POST[ 'sponsors_url' ] ) ? sanitize_text_field( $_POST[ 'sponsors_url' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sponsors_url', $sponsors_new_url );
}
}
new sponsors_metabox;