Custom HTML + PHP callback to display value on Settings Page (MB AIO)
Support › MB Settings Page › Custom HTML + PHP callback to display value on Settings Page (MB AIO)Resolved
- This topic has 4 replies, 2 voices, and was last updated 4 years ago by
Long Nguyen.
-
AuthorPosts
-
April 28, 2021 at 12:09 AM #27712
Jordan
ParticipantHi! I'm building a settings page where user enters data that is processed somewhere else in a code block. On that page (settings), I'm trying to display field group data using Custom HTML with a PHP callback.
I've entered the following function in "Code Snippets". What I want is the $index variable to display on the page so the user knows his settings' result in backend. So I'm entering 'mb_quote_index_return' in the "PHP callback" line of the "Custom HTML" field, but nothing shows.
Can someone tell me where I got it wrong?
Thanks!CODE (mb_quote_index_return):
<?php function get_mb_settings_field( $field_id ) { return rwmb_meta( $field_id, ['object_type' => 'setting'], 'options' ); } date_default_timezone_set("America/New_York"); $quotes = get_mb_settings_field( 'pensees_quotes' ); $max = count($quotes); $lastIndex = get_mb_settings_field( 'start_index' ); $lastTime = get_mb_settings_field( 'start_date' ); $lastTimeA = strtotime($lastTime); $gap_s = strtotime('now') - $lastTimeA; $gap_d = floor($gap_s / (60<em>60</em>24)); $index = ($lastIndex - 1) + $gap_d; $rem = $index; while ($rem >= $max) { $rem = $rem - $max; } $index = $rem; echo "Pensée: ".($index + 1)."/".$max; ?>
April 28, 2021 at 2:43 AM #27713Jordan
ParticipantI realized my above question isn't well formulated: the code above works well in an Oxygen code block, so this isn't the issue. Now what I want to do is access the same table's data from a Custom Fields Group and display it on a settings page using custom html. For example, here's how I tried to output the value of ' start_index ' through
1) php callback: http://prnt.sc/125qd5s
2) html/php: http://prnt.sc/125qfo0Yet the settings page outputs nothing: http://prnt.sc/125qhps
What I really want to display is the $index variable in the above code. Any help understanding how the custom html works would be greatly appreciated (I did read the documentation but could not figure it out.)
Thanks!
April 28, 2021 at 11:07 PM #27739Long Nguyen
ModeratorHi Jordan,
Thank you for reaching out.
The field type
custom_html
only accepts the callback function name. It also does not support passing the argument on this setting.If you use content setting, it only accepts HTML code, not support PHP code. See my screenshot https://share.getcloudapp.com/z8u6G19r.
Get more details on the documentation https://docs.metabox.io/fields/custom-html/.
April 29, 2021 at 12:43 AM #27741Jordan
ParticipantThanks! So if I understand, I must wrap everything in a custom function that will echo the settings_field value. Where should I store that custom function so it is accessible via callback? (I use Oxygen which disables function.php).
April 29, 2021 at 9:32 AM #27746Long Nguyen
ModeratorHi,
You can add the custom function in a custom plugin or use the plugin Code Snippets https://wordpress.org/plugins/code-snippets/
-
AuthorPosts
- You must be logged in to reply to this topic.