Custom function example
Support › MB Include Exclude › Custom function exampleResolved
- This topic has 7 replies, 2 voices, and was last updated 3 months ago by
Hazmi.
-
AuthorPosts
-
November 28, 2024 at 7:02 PM #47053
Hazmi
ParticipantHi, I'm trying to use a custom function to display a meta box, but it doesn't seem to work:
'include' => [ 'custom' => 'myFunction', ]
,
and
function myFunction( $metabox ) { return true; }
do nothing.
November 29, 2024 at 10:28 PM #47069Peter
ModeratorHello Hazmi,
I recheck the feature custom function of the extension MB Include Exclude and it still works correctly. Where do you add the custom function
myFunction
and the code to register the meta box? Please try to add all the code to the file functions.php in the theme/child theme folder and check this again.January 13, 2025 at 7:39 PM #47386Hazmi
ParticipantHi,
the code works fine (I had a typo in the code).But I ran into a big problem. If I display the metabox using this condition (custom function), then the data in the custom fields like "key_value" or "group" is not saved.
I just fill in the fields and after saving these fields are empty. I need to fix this bug ASAP.
Thank you.
WP 6.7.1, Meta Box 5.10.6, MB Group 1.4.5
January 13, 2025 at 11:30 PM #47395Peter
ModeratorHello Hazmi,
Can you please share the code that creates the custom fields "key_value" or "group"? If you use the builder, you can export the field group to a JSON file and share it here. I will help you check the issue.
January 14, 2025 at 2:33 AM #47397Hazmi
ParticipantHi,
there is a simple code, that does not save data. Without a condition (custom include function) - everything work as normal.function myFunction( $meta_box ) { return true; } $meta_boxes[] = [ 'title' => 'Test Meta Box', 'post_types' => ['post'], 'include' => [ 'custom' => 'myFunction', ], 'fields' => [ [ 'id' => 'keys', 'type' => 'key_value' ], [ 'id' => 'groups', 'type' => 'group', 'clone' => true, 'fields' => [ [ 'id' => 'group-title', 'type' => 'text' ], [ 'id' => 'group-description', 'type' => 'wysiwyg', 'options' => [ 'textarea_rows' => 10, 'media_buttons' => false, 'tinymce' => false, 'quicktags' => false ] ] ] ] ] ];
January 14, 2025 at 2:37 AM #47398Hazmi
ParticipantThe custom function is of course outside the definition of meta boxes. I'm just including it here as an example.
January 14, 2025 at 11:43 PM #47407Peter
ModeratorHello,
I add your code to the file functions.php in the theme folder, edit a post, add some sample value and save. The value is saved to the database properly, without any issues. Here is the screen record https://imgur.com/pMriFJ1
If it still doesn't work on your site, please share the site admin account by submitting this contact form https://metabox.io/contact/
I will take a look.January 15, 2025 at 2:55 PM #47414Hazmi
ParticipantHi Peter,
after a thorough examination of the code, I found that the error was ultimately on my side.. I apologize for the mystification and thank you for your cooperation. This thread could easily be removed.
-
AuthorPosts
- You must be logged in to reply to this topic.