Created
September 13, 2022 17:26
-
-
Save 465media/f420d2b28e83133600d17b31c49060df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'load-post.php', '465_post_meta_boxes_setup' ); | |
add_action( 'load-post-new.php', '465_post_meta_boxes_setup' ); | |
function 465_post_meta_boxes_setup() { | |
add_action( 'add_meta_boxes', '465_add_post_meta_boxes' ); | |
add_action( 'save_post', '465_save_post_class_meta', 10, 2 ); | |
} | |
function 465_add_post_meta_boxes() { | |
add_meta_box( | |
'465-post-class', | |
'Nate's SUper Cool Sidebar ', | |
'465_post_class_meta_box', | |
'post', | |
'side', | |
'default' | |
); | |
} | |
function 465_post_class_meta_box( $post ) { | |
wp_nonce_field( basename( __FILE__ ), '465_post_class_nonce' );?> | |
<div class="components-base-control editor-post-excerpt__textarea"> | |
<div class="components-base-control__field"> | |
<label class="components-base-control__label" for="465-post-class">465 ссылка (необязательно)</label> | |
<input type="text" name="465-post-class" id="465-post-class" class="edit-post-post-schedule" value="<?php echo esc_attr( get_post_meta( $post->ID, '465_post_class', true ) ); ?>"> | |
</div> | |
</div> | |
<?php } | |
function 465_save_post_class_meta( $post_id, $post ) { | |
if ( !isset( $_POST['465_post_class_nonce'] ) || !wp_verify_nonce( $_POST['465_post_class_nonce'], basename( __FILE__ ) ) ) | |
return $post_id; | |
$post_type = get_post_type_object( $post->post_type ); | |
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) | |
return $post_id; | |
$new_meta_value = ( isset( $_POST['465-post-class'] ) ? $_POST['465-post-class'] : '' ); | |
$meta_key = '465_post_class'; | |
$meta_value = get_post_meta( $post_id, $meta_key, true ); | |
if ( $new_meta_value && '' == $meta_value ) | |
add_post_meta( $post_id, $meta_key, $new_meta_value, true ); | |
elseif ( $new_meta_value && $new_meta_value != $meta_value ) | |
update_post_meta( $post_id, $meta_key, $new_meta_value ); | |
elseif ( '' == $new_meta_value && $meta_value ) | |
delete_post_meta( $post_id, $meta_key, $meta_value ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment