Best way to import into custom_html field

Support General Best way to import into custom_html field

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #46537
    webdevwebdev
    Participant

    Hi,

    I'm trying to put together a help section for a client on a Settings page and was wondering what the best way is to have the HTML for each custom_html field in a separate file.

    I know I can use a callback, eg 'get_help' with the function doing something like
    include('help_file.php');

    but this will quickly get unmanageable with lots of files.

    What would be the best way of having a single function or class that I pass a parameter, ie filename, into?

    #46543
    PeterPeter
    Moderator

    Hello,

    You can create a callback function with a parameter and include the file from that parameter. For example:

    function my_callback_func( $file_name ) {
    	include( $file_name . '.php');
    }

    the pass the file name to the callback function:
    my_callback_func( 'help_file' )

    #46544
    webdevwebdev
    Participant

    Hi,
    Sorry, I don't think I was clear in the initial question. I'm generating the Settings page using a custom_html field and want to use the 'callback' key for the custom_html field.

    Adding a parameter to a callback doesn't work for me there.

    #46545
    PeterPeter
    Moderator

    So where do you add the custom_html field? On an editing post or on a settings page? Can you please explain more about the case?

    #46546
    webdevwebdev
    Participant

    On a settings page, so I have something like this:

    
            'title'          => __('General Settings', 'text-domain'),
            'id'             => 'general-settings',
            'settings_pages' => ['some-settings'],
            'tab'            => 'Settings',
            'fields'         => [
                [
                    'name'            => __('How to generate some content', 'text-domain'),
                    'type'            => 'custom_html',
                    'callback'        => get_help('help_page'),
                ],
    

    and putting my function name with a paramater in the callback section doesn't work. If I have the callback function as a string, then it does work, but then I don't know how to include a parameter...

    #46568
    PeterPeter
    Moderator

    Hello,

    The callback function of the custom HTML field with a parameter works properly on my demo site, here is the screen record https://drive.google.com/file/d/1NCmayn0rm1SIf3WiS_MHmfuPYS9Mf5aw/view?usp=sharing

    Can you please recheck this?

    #47343
    webdevwebdev
    Participant

    Hi again,
    Apologies for delay in replying to this - Other things have got in the way... I just tried again, and I can get the contents of the included PHP file to be rendered, but they are not in the right place - it seems to render it before any other content (behind the main WordPress backend nav) rather than in the correct place on the screen.
    I am trying to do this in a tab on a Settings page (if that makes any difference). I have tried removing the tabs to see if that makes any difference but it doesn't help.

    #47370
    webdevwebdev
    Participant

    ... and I just tried with it in a standard post instead of a settings page - as before, the content is rendered, but directly after the <body> tag of the html, so in the wrong place. Any ideas please?

    #47388
    PeterPeter
    Moderator

    Hello,

    Please share your site admin account by submitting this contact form https://metabox.io/contact/
    I will take a look.

    #47389
    webdevwebdev
    Participant

    Hi,
    Thanks, but unfortunately I'm working on a local wp-env installation so you won't be able to get to it...

    #47403
    PeterPeter
    Moderator

    Simply, you can upload your site to a staging site and share the admin account. Then I can check what's wrong there. Thank you.

    #47491
    webdevwebdev
    Participant

    Hi again, I did share admin details with you, but forgot to update here, so perhaps it was missed. But...

    UPDATE:
    I still haven't managed to get it working linking to a function in 'callback', however if I put the function name in the 'std' field, I can get it to work. I just have to use output buffering, like so:

    The std field contains the function call, instead of the callback field...

            'fields'         => [
                [
                    'name'            => __('How to generate some content', 'text-domain'),
                    'type'            => 'custom_html',
                    'std'        => get_help('help_page'),
                    // 'callback' => get_help('help_page'),
                ],
    

    and return the output buffer in get_help()...

    function get_help($file){
        ob_start();
        include ($file.'.php');
        return ob_get_clean();
    }

    So this looks like a work-around.

    My question now though, is this ok to use?
    Could it break in future updates to MB?
    In short, is it a good idea 🙂 ?

    #47506
    PeterPeter
    Moderator

    Hello,

    The callback function requires to have a return state and value to display the message in the correct position (field appearance). So it is a solution and good to go.

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