Best way to import into custom_html field
- This topic has 12 replies, 2 voices, and was last updated 3 months ago by
Peter.
-
AuthorPosts
-
September 26, 2024 at 3:35 PM #46537
webdev
ParticipantHi,
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?
September 26, 2024 at 9:50 PM #46543Peter
ModeratorHello,
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' )
September 26, 2024 at 10:09 PM #46544webdev
ParticipantHi,
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.
September 26, 2024 at 10:40 PM #46545Peter
ModeratorSo 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?
September 26, 2024 at 10:59 PM #46546webdev
ParticipantOn 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...
September 29, 2024 at 8:37 PM #46568Peter
ModeratorHello,
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?
January 8, 2025 at 10:45 PM #47343webdev
ParticipantHi 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.January 10, 2025 at 11:37 PM #47370webdev
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?
January 13, 2025 at 10:11 PM #47388Peter
ModeratorHello,
Please share your site admin account by submitting this contact form https://metabox.io/contact/
I will take a look.January 13, 2025 at 10:18 PM #47389webdev
ParticipantHi,
Thanks, but unfortunately I'm working on a local wp-env installation so you won't be able to get to it...January 14, 2025 at 10:04 PM #47403Peter
ModeratorSimply, you can upload your site to a staging site and share the admin account. Then I can check what's wrong there. Thank you.
January 22, 2025 at 12:18 AM #47491webdev
ParticipantHi 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 🙂 ?January 22, 2025 at 10:43 PM #47506Peter
ModeratorHello,
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.
-
AuthorPosts
- You must be logged in to reply to this topic.