Pull MetaBox Settings Page fields using PHP
Support › MB Settings Page › Pull MetaBox Settings Page fields using PHPResolved
- This topic has 2 replies, 2 voices, and was last updated 2 years, 5 months ago by
MikeX3.
-
AuthorPosts
-
November 12, 2022 at 8:27 AM #39085
MikeX3
ParticipantI'm rebuilding an ACF site using MetaBox and I'm having trouble pulling data from the settings page using PHP.
In ACF, I could use the following simple line to pull an options page field:
$field = get_field( 'option_name', 'options' )
The only thing here I need to edit is the $variable and the 'option_name'
I'm trying to figure out the similar code with MetaBox. I did a google search and found https://metabox.io/get-field-settings/ ..so if I'm on the right track, I'm guessing I would need to use something like this:
$field = rwmb_get_field_settings( $field_id, array( 'object_type' => 'setting' ) );
However, it doesn't appear to be working.
I found another line of code with one more option that looks like this:
$field = rwmb_get_field_settings( $field_id, ['object_type' => 'setting'], 'site_option' );
So, I've replaced $field_id with my field_id and tried replacing 'site-option' with my settings page id (site-settings and/or mb_site-settings) with no luck.
Here's the exact code I'm using:
<?php $field = rwmb_get_field_settings( 'mh_website_name', ['object_type' => 'setting'], 'site-settings' ); echo ($field); ?>
Depending on what I try, I either get nothing on the front end, or the following error:
Warning: Array to string conversion in /var/www/mywebsite.com/htdocs/wp-content/themes/bricks/includes/elements/code.php(159) : eval()'d code on line 5
ArrayFor reference, the field I'm trying to grab (for testing purposes) is called mh_website_name. It lives in the Site Identity Tab of the Site Settings page.
Relevant PHP from my settings page"
function your_prefix_function_name( $settings_pages ) { $settings_pages[] = [ 'menu_title' => __( 'Site Settings', 'your-text-domain' ), 'id' => 'site-settings', 'position' => 80, 'style' => 'no-boxes', 'columns' => 1, 'tabs' => [ 'mh_settings_site_identity' => 'Site Identity',
Relevant PHP from the custom field in question:
<?php add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' ); function your_prefix_function_name( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'Site Settings - Site Identity', 'your-text-domain' ), 'id' => 'site-settings', 'settings_pages' => ['site-settings'], 'tab' => 'mh_settings_site_identity', 'fields' => [ [ 'name' => __( 'Website Name:', 'your-text-domain' ), 'id' => $prefix . 'mh_website_name', 'type' => 'text', 'label_description' => __( '[mh_website_name,option]', 'your-text-domain' ), 'desc' => __( 'Please enter the legal website name. This will be used throughout the site in dynamic data fields.', 'your-text-domain' ), 'std' => 'Website Name', 'required' => true, ],
November 13, 2022 at 6:05 PM #39102Peter
ModeratorHello Mike,
If you want to get the field value, please use the helper function rwmb_meta(), for example
$field = rwmb_meta( 'mh_website_name', ['object_type' => 'setting'], 'site-settings' ); echo ($field);
The helper function rwmb_get_field_settings() helps you to get the field settings like name, type, id ..., not the field value.
November 15, 2022 at 5:52 AM #39137MikeX3
ParticipantAwesome. Thank you!
-
AuthorPosts
- You must be logged in to reply to this topic.