In meinem benutzerdefinierten Plugin habe ich einen benutzerdefinierten Beitragstyp und eine benutzerdefinierte Meta-Box dafür. Ich habe nur wenige Felder, eines davon ist ein Kontrollkästchen. Ich möchte, dass dieses Kontrollkästchen standardmäßig aktiviert ist, wenn ich mich für einen add new post
entscheide und dann mit der Auswahl des Benutzers fortfahre, dh aktiviert/nicht aktiviert.
Ich füge die zugehörigen Codes hinzu, dh: für die Metabox und wie ich sie speichere.
function coupon_add_metabox() {
add_meta_box( 'coupon_details', __( 'Coupon Details', 'domain' ), 'wpse_coupon_metabox', 'coupons', 'normal', 'high' );
}
function wpse_coupon_metabox( $post ) {
// Retrieve the value from the post_meta table
$coupon_status = get_post_meta( $post->ID, 'coupon_status', true );
// Checking for coupon active option.
if ( $coupon_status ) {
if ( checked( '1', $coupon_status, false ) )
$active = checked( '1', $coupon_status, false );
}
// Html for the coupon details
$html = '';
$html .= '<div class="coupon-status"><label for="coupon-status">';
$html .= __( 'Active', 'domain' );
$html .= '</label>';
$html .= '<input type="checkbox" id="coupon-status-field" name="coupon-status-field" value="1"' . $active . ' /></div>';
echo $html;
}
function wpse_coupons_save_details( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( ! current_user_can( 'activate_plugins' ) )
return $post_id;
$coupon_status = sanitize_text_field( $_POST['coupon-status-field'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'coupon_status', $coupon_status );
}
add_action( 'save_post', 'wpse_coupons_save_details' );
Es wird erwartet, dass save_post
mehrmals aufgerufen wird. Aufgrund seiner Verwendung möchten Sie jedoch möglicherweise nicht immer, dass der Code ausgeführt wird, bis der Benutzer tatsächlich auf die Schaltfläche "Speichern" klickt. Wie Sie oben in Bezug auf Ihre Anforderung erwähnt haben.
Sie können also die aktuelle Seite (d. H. post-new.php
) im wp-admin überprüfen und darauf basierende Bedingungen festlegen. Hier ist, wie Sie implementieren können.
global $pagenow;
// Checking for coupon active option.
if ( $coupon_status ) {
if ( checked( '1', $coupon_status, false ) )
$active = checked( '1', $coupon_status, false );
}
if ( 'post-new.php' == $pagenow ) {
$active = 'checked';
}
========== Oder Sie können dies auch überprüfen ============
if( ! ( wp_is_post_revision( $post_id) && wp_is_post_autosave( $post_id ) ) ) {
// do the work that you want to execute only on "Add New Post"
} // end if
Es lohnt sich zu verstehen, warum es passiert und was sich hinter den Kulissen von save_post abspielt:
Wenn der Beitrag neu ist, lautet der Wert von get_post_meta( $post->ID, 'coupon_status' )
null
. Sobald das benutzerdefinierte Feld gespeichert und deaktiviert ist, handelt es sich um eine empty
Zeichenfolge (""
). Sie sollten also nur in der Lage sein, eine Prüfung für einen null
-Wert hinzuzufügen:
if ( checked( '1', $coupon_status, false ) || $coupon_status == null ) {
$active = 'checked';
}