Support Forum
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?
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' )
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.
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?
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...
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?
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.
... 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?
Hello,
Please share your site admin account by submitting this contact form https://metabox.io/contact/
I will take a look.
Hi,
Thanks, but unfortunately I'm working on a local wp-env installation so you won't be able to get to it...
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.
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 🙂 ?