Photo Credit
class dd_slider_meta {
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(
'dd_slider_meta',
__( 'Slider Extra Data', 'text_domain' ),
array( $this, 'render_metabox' ),
'slider',
'normal',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$dd_photo_credit = get_post_meta( $post->ID, 'dd_photo_credit', true );
// Set default values.
if( empty( $dd_photo_credit ) ) $dd_photo_credit = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="dd_photo_credit" class="dd_photo_credit_label">' . __( 'Photo Credit', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="dd_photo_credit" name="dd_photo_credit" class="dd_photo_credit_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $dd_photo_credit ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$dd_new_photo_credit = isset( $_POST[ 'dd_photo_credit' ] ) ? sanitize_text_field( $_POST[ 'dd_photo_credit' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'dd_photo_credit', $dd_new_photo_credit );
}
}
new dd_slider_meta;