Post Zusatzinfo
class post_metabox_zusatz {
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(
'zusatzinfos',
__( 'Zusätzliche Angaben', 'text_domain' ),
array( $this, 'render_metabox_zusatzinfos' ),
'post',
'side',
'high'
);
}
public function render_metabox_zusatzinfos( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'zusatzinfo_nonce_action', 'zusatzinfo_nonce' );
// Retrieve an existing value from the database.
$zusatzinfo_fotocredit = get_post_meta( $post->ID, 'zusatzinfo_fotocredit', true );
$zusatzinfo_fusszeile = get_post_meta( $post->ID, 'zusatzinfo_fusszeile', true );
$zusatzinfo_notizen = get_post_meta( $post->ID, 'zusatzinfo_notizen', true );
$zusatzinfo_inhaltsverzeichnis = get_post_meta( $post->ID, 'zusatzinfo_inhaltsverzeichnis', true );
// Set default values.
if( empty( $zusatzinfo_fotocredit ) ) $zusatzinfo_fotocredit = '';
if( empty( $zusatzinfo_fusszeile ) ) $zusatzinfo_fusszeile = '';
if( empty( $zusatzinfo_notizen ) ) $zusatzinfo_notizen = '';
if( empty( $zusatzinfo_inhaltsverzeichnis ) ) $zusatzinfo_inhaltsverzeichnis = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="zusatzinfo_fotocredit" class="zusatzinfo_fotocredit_label">' . __( 'Fotocredit', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="zusatzinfo_fotocredit" name="zusatzinfo_fotocredit" class="zusatzinfo_fotocredit_field" placeholder="' . esc_attr__( 'Fotocredit', 'text_domain' ) . '" value="' . esc_attr( $zusatzinfo_fotocredit ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="zusatzinfo_fusszeile" class="zusatzinfo_fusszeile_label">' . __( 'Fusszeile', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="zusatzinfo_fusszeile" name="zusatzinfo_fusszeile" class="zusatzinfo_fusszeile_field" placeholder="' . esc_attr__( 'Fusszeile', 'text_domain' ) . '" value="' . esc_attr( $zusatzinfo_fusszeile ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="zusatzinfo_notizen" class="zusatzinfo_notizen_label">' . __( 'Notizen', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="zusatzinfo_notizen" name="zusatzinfo_notizen" class="zusatzinfo_notizen_field" placeholder="' . esc_attr__( 'Notizen', 'text_domain' ) . '">' . $zusatzinfo_notizen . '</textarea>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="zusatzinfo_inhaltsverzeichnis" class="zusatzinfo_inhaltsverzeichnis_label">' . __( 'Inhaltsverzeichnis', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="radio" name="zusatzinfo_inhaltsverzeichnis" class="zusatzinfo_inhaltsverzeichnis_field" value="zusatzinfo_0" ' . checked( $zusatzinfo_inhaltsverzeichnis, 'zusatzinfo_0', false ) . '> ' . __( 'nicht anzeigen', 'text_domain' ) . '</label><br>';
echo ' <label><input type="radio" name="zusatzinfo_inhaltsverzeichnis" class="zusatzinfo_inhaltsverzeichnis_field" value="zusatzinfo_1" ' . checked( $zusatzinfo_inhaltsverzeichnis, 'zusatzinfo_1', false ) . '> ' . __( 'anzeigen', 'text_domain' ) . '</label><br>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['zusatzinfo_nonce'] ) ? $_POST['zusatzinfo_nonce'] : '';
$nonce_action = 'zusatzinfo_nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Sanitize user input.
$zusatzinfo_new_fotocredit = isset( $_POST[ 'zusatzinfo_fotocredit' ] ) ? sanitize_text_field( $_POST[ 'zusatzinfo_fotocredit' ] ) : '';
$zusatzinfo_new_fusszeile = isset( $_POST[ 'zusatzinfo_fusszeile' ] ) ? sanitize_text_field( $_POST[ 'zusatzinfo_fusszeile' ] ) : '';
$zusatzinfo_new_notizen = isset( $_POST[ 'zusatzinfo_notizen' ] ) ? sanitize_text_field( $_POST[ 'zusatzinfo_notizen' ] ) : '';
$zusatzinfo_new_inhaltsverzeichnis = isset( $_POST[ 'zusatzinfo_inhaltsverzeichnis' ] ) ? $_POST[ 'zusatzinfo_inhaltsverzeichnis' ] : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'zusatzinfo_fotocredit', $zusatzinfo_new_fotocredit );
update_post_meta( $post_id, 'zusatzinfo_fusszeile', $zusatzinfo_new_fusszeile );
update_post_meta( $post_id, 'zusatzinfo_notizen', $zusatzinfo_new_notizen );
update_post_meta( $post_id, 'zusatzinfo_inhaltsverzeichnis', $zusatzinfo_new_inhaltsverzeichnis );
}
}
new post_metabox_zusatz;