Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Refactor code to register custom fields for all post types
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Secchi committed Sep 7, 2024
1 parent 4e3b645 commit 86df7b9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 34 deletions.
10 changes: 8 additions & 2 deletions includes/api/class-register-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,26 @@ public function register_custom_field( $custom_fields, $post_type ) {
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'ciaone',
'default' => '',
);

// Loop through each custom field and register it.
foreach ( $custom_fields as $custom_field => $args ) {
if ( ! post_type_exists( $post_type ) ) {
continue; // Use continue instead of return to avoid breaking the entire function
}

// Handle simple array case
if ( is_int( $custom_field ) ) {
$custom_field = $args;
$args = array();
}

// Merge default arguments with specific arguments.
$args = array_merge( $default_args, $args );

// Register the meta field for the post type.
register_post_meta( $post_type, $custom_field, $args );
}

}
}
34 changes: 3 additions & 31 deletions mu-plugins/project-custom-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,46 +217,18 @@ function () {
function () {
$meta_keys = array(
'test_meta_key' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'default' => false,
),
'test_meta_key2' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'string'
'show_in_rest' => false,
),
'test_meta_key3' => array(),
'test_meta_key4',
);

return $meta_keys;
},
10,
3
);

add_filter(
'writepoetry_add_custom_fields_to_page',
function () {
$meta_keys = array(
'test_page' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'test',
'default' => false,
),
'test_page_key2' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'test2',
),
);

return $meta_keys;
},
10,
3
);


2 changes: 1 addition & 1 deletion write-poetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Plugin Name: Write Poetry
* Plugin URI: https://github.com/giacomo-secchi/write-poetry
* Description: The Swiss knife plugin designed for developers and advanced users. Unlock the full potential of WordPress with this versatile tool. Empower your workflow without getting your hands dirty.
* Version: 0.3.6
* Version: 0.3.7
* Requires at least: 5.9
* Requires PHP: 7.2
* Author: Giacomo Secchi
Expand Down

0 comments on commit 86df7b9

Please sign in to comment.