Created
July 3, 2025 09:41
-
-
Save isuke01/62ce6d96436c536f73bca122419e6993 to your computer and use it in GitHub Desktop.
Simple Wordpress Helper to register multiple post meta based on Associative array. register_multiple_post_meta is wrapping around register_post_meta
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
<?php | |
/** | |
* Register multiple post meta fields with default args. | |
* | |
* @param string $post_type The post type to register meta for. | |
* @param array $fields Associative array of meta_key => args. | |
*/ | |
function register_multiple_post_meta( $post_type, array $fields ) { | |
$default_args = [ | |
'single' => true, | |
'show_in_rest' => true, | |
'default' => '', | |
'auth_callback' => function () { | |
return \current_user_can( 'edit_posts' ); | |
}, | |
]; | |
foreach ( $fields as $meta_key => $args ) { | |
$merged_args = wp_parse_args( $args, $default_args ); | |
\register_post_meta( $post_type, $meta_key, $merged_args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment