Custom function example

Support MB Include Exclude Custom function exampleResolved

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #47053
    HazmiHazmi
    Participant

    Hi, 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.

    #47069
    PeterPeter
    Moderator

    Hello 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.

    #47386
    HazmiHazmi
    Participant

    Hi,
    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

    #47395
    PeterPeter
    Moderator

    Hello 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.

    #47397
    HazmiHazmi
    Participant

    Hi,
    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
                        ]
                    ]
                ]
            ]
        ]
    ];
    #47398
    HazmiHazmi
    Participant

    The custom function is of course outside the definition of meta boxes. I'm just including it here as an example.

    #47407
    PeterPeter
    Moderator

    Hello,

    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.

    #47414
    HazmiHazmi
    Participant

    Hi 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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.