Visibility Check Box mit Beitrag
class Sgr_Visibility_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(
'sgr_visibility_meta',
__( 'Platzierung des Beitrags', 'sgr' ),
array( $this, 'sgr_render_visibility_metabox' ),
'post',
'side',
'high'
);
}
public function sgr_render_visibility_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'sgr_nonce_action', 'sgr_nonce' );
// Retrieve an existing value from the database.
$sgr_homepage = get_post_meta( $post->ID, 'sgr_homepage', true );
$sgr_fb_page = get_post_meta( $post->ID, 'sgr_fb_page', true );
$sgr_article_event_date = get_post_meta( $post->ID, 'sgr_article_event_date', true );
// Set default values.
if( empty( $sgr_article_event_date ) ) $sgr_article_event_date = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sgr_visibility_checkbox" class="sgr_visibility_checkbox_label">' . __( 'Der Beitrag erscheint zusätzlich auf folgenden Seiten:', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" name="sgr_homepage" class="sgr_visibility_checkbox_field" value="' . $sgr_homepage . '" ' . checked( $sgr_homepage, 'checked', false ) . '> ' . __( 'Startseite', 'sgr' ) . '</label><br>';
echo ' <label><input type="checkbox" name="sgr_fb_page" class="sgr_visibility_checkbox_field" value="' . $sgr_fb_page . '" ' . checked( $sgr_fb_page, 'checked', false ) . '> ' . __( 'Fachbereichsseite', 'sgr' ) . '</label><br>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sgr_article_event_date" class="sgr_article_event_date_label">' . __( 'Veranstaltungsdatum (dd.mm.yyyy)', 'sgr' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="sgr_article_event_date" name="sgr_article_event_date" class="sgr_article_event_date_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_article_event_date ) . '">';
echo ' <p class="description">' . __( 'Tragen Sie hier das Datum der Veranstaltung ein, auf die sich der Beitrag bezieht.', 'sgr' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['sgr_nonce'] ) ? $_POST['sgr_nonce'] : '';
$nonce_action = 'sgr_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;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$sgr_new_homepage = isset( $_POST[ 'sgr_homepage' ] ) ? 'checked' : '';
$sgr_new_fb_page = isset( $_POST[ 'sgr_fb_page' ] ) ? 'checked' : '';
$sgr_new_article_event_date = isset( $_POST[ 'sgr_article_event_date' ] ) ? sanitize_text_field( $_POST[ 'sgr_article_event_date' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sgr_homepage', $sgr_new_homepage );
update_post_meta( $post_id, 'sgr_fb_page', $sgr_new_fb_page );
update_post_meta( $post_id, 'sgr_article_event_date', $sgr_new_article_event_date );
}
}
new Sgr_Visibility_Meta_Box;