true ), 'names' ); foreach ( $post_types as $post_type ) { register_post_meta( $post_type, '_og_title', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_text_field', 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); register_post_meta( $post_type, '_og_description', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_textarea_field', 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); } } /** * Register the meta box for all public post types. * * @since 1.2.0 * * @return void */ public function register_meta_box(): void { $post_types = get_post_types( array( 'public' => true ), 'names' ); foreach ( $post_types as $post_type ) { add_meta_box( 'robotstxt-og-meta-box', __( 'Open Graph / Social Media', 'robotstxt-og' ), array( $this, 'render_meta_box' ), $post_type, 'normal', 'default' ); } } /** * Render the meta box HTML. * * @since 1.2.0 * * @param WP_Post $post The current post object. * @return void */ public function render_meta_box( WP_Post $post ): void { wp_nonce_field( 'robotstxt_og_meta_box', 'robotstxt_og_meta_box_nonce' ); $raw_title = get_post_meta( $post->ID, '_og_title', true ); $og_title = is_string( $raw_title ) ? $raw_title : ''; $raw_desc = get_post_meta( $post->ID, '_og_description', true ); $og_description = is_string( $raw_desc ) ? $raw_desc : ''; ?>
|
|
|
|
|