### Example Field Setup
Source: https://docs.metabox.io/fields/osm
Defines the configuration for an address text field and an OSM map field, specifying their IDs, types, and relationships.
```php
// Address field
[
'id' => 'my_address',
'name' => 'Address',
'type' => 'text',
],
// Map field
[
'id' => 'map',
'name' => 'Location',
'type' => 'osm',
'std' => '-6.233406,-35.049906,15',
'address_field' => 'my_address',
],
```
--------------------------------
### Example Field Setup for Map and Address Fields
Source: https://docs.metabox.io/fields/map
Defines the configuration for a text field to capture addresses and a map field to display the location, linking them via the 'address_field' setting. Ensure the Google Maps API key is provided.
```php
// Address field
[
'id' => 'my_address',
'name' => 'Address',
'type' => 'text',
],
// Map field
[
'id' => 'map',
'name' => 'Location',
'type' => 'map',
'std' => '-6.233406,-35.049906,15',
'address_field' => 'my_address',
'api_key' => 'XXXXXXXXX',
]
```
--------------------------------
### Install Meta Box Lite with Composer
Source: https://docs.metabox.io/composer
Use this command to install Meta Box Lite, which bundles Meta Box and all free extensions, using Composer.
```bash
composer require wpmetabox/meta-box-lite // Or use Meta Box Lite, which includes Meta Box and all free extensions
```
--------------------------------
### Model Supports Example
Source: https://docs.metabox.io/extensions/mb-custom-table
Example of how to define 'supports' for a custom model to automatically include author, published date, and modified date.
```php
'supports' => [ 'author', 'published_date', 'modified_date' ],
```
--------------------------------
### Combined Group and Sub-field Import Example
Source: https://docs.metabox.io/extensions/mb-wpai
This example demonstrates how to import data into group fields and their sub-fields using a combination of 'For each' and dot syntax for clarity and scope.
```text
For each: {photos/photo}
We import:
Title: {.title} (same as {photos/photo/title} but scoped to the group)
URL: {.url} (same as {photos/photo/url} but scoped to the group)
```
--------------------------------
### Image Upload Field Settings Example
Source: https://docs.metabox.io/fields/image-upload
Example of creating an image upload field with various settings using code.
```php
[
'id' => 'image',
'name' => 'Image upload',
'type' => 'image_upload',
'force_delete' => false,
'max_file_uploads' => 2,
'max_status' => false,
'image_size' => 'medium',
]
```
--------------------------------
### Example Response for Settings Page Data
Source: https://docs.metabox.io/extensions/mb-rest-api
This is an example of the JSON response you can expect when retrieving settings page data. It contains various settings like address, phone, and email.
```json
{
"address": "4812 Jones Avenue, Winston Salem, NC 27108",
"phone": "+1-336-720-4261",
"email": "info@jourrapide.com"
}
```
--------------------------------
### Link Field Data Structure Example
Source: https://docs.metabox.io/fields/link
This example shows the expected data structure for a single link entry, including URL, title, and target. This is how the field's value is represented.
```php
[
'url' => 'https://metabox.io',
'title' => 'Meta Box',
'target' => '_blank',
]
```
--------------------------------
### Get Settings Page Data using WordPress apiFetch
Source: https://docs.metabox.io/extensions/mb-rest-api
This JavaScript example uses apiFetch to get settings page data. It makes a GET request to the '/meta-box/v1/settings-page' endpoint, passing the 'id' as a query parameter.
```javascript
import apiFetch from '@wordpress/api-fetch';
apiFetch( {
path: `/meta-box/v1/settings-page?id=website-settings`,
} ).then( res => console.log( res ) );
```
--------------------------------
### Get Price Field Value
Source: https://docs.metabox.io/tutorials/create-product-page
Example of retrieving the value for a specific custom field named 'price'.
```php
```
--------------------------------
### Get Field Settings for a Select Field
Source: https://docs.metabox.io/functions/rwmb-get-field-settings
Retrieves the settings for a field named 'country'. This is a basic usage example.
```php
'thumbnail'] );
echo '
';
}
```
--------------------------------
### Example Paths to Meta Box YAML Files in Multiple Locations
Source: https://docs.metabox.io/extensions/meta-box-template
Specify multiple locations for YAML configuration files, including theme directories and custom wp-content folders.
```text
%stylesheet%/meta-box/, %wp-content%/meta-box-yml/
```
--------------------------------
### Setting Block Templates
Source: https://docs.metabox.io/extensions/mb-blocks
Demonstrates how to define a default initial state for an editor session using the 'template' argument when registering a post type.
```php
'template' => [
['meta-box/{$block_id}']
]
```
--------------------------------
### Get Dates Between Two Dates
Source: https://docs.metabox.io/tutorials/build-hotel-booking-website-p3-frontend
A utility function to generate an array of dates between a specified start and end date, inclusive. Useful for date range calculations.
```php
function get_dates_between( $start_date, $end_date ) {
$dates = array();
$current = strtotime( $start_date );
$end = strtotime( $end_date );
while ( $current <= $end ) {
$dates[] = date( 'Y-m-d', $current );
$current = strtotime( '+1 day', $current );
}
return $dates;
}
```
--------------------------------
### Get Posts by Meta Field Criteria
Source: https://docs.metabox.io/tutorials/show-posts-specific-criteria-mb-views
Use `mb.get_posts` to retrieve posts that meet specific meta field conditions. This example filters for posts where 'promotional_price' is not empty.
```twig
{% set args = { post_type: 'cuisine', meta_key : 'promotional_price', meta_value: '', meta_compare : '!=', meta_type : 'CHAR', posts_per_page: 6, order: 'DESC' } %}
{% set posts = mb.get_posts( args ) %}
{% for post in posts %}
{% endfor %}
```
--------------------------------
### Range Field Settings Example
Source: https://docs.metabox.io/fields/range
This code demonstrates how to configure a range field with specific minimum, maximum, and step values using an array of settings.
```php
[
'name' => 'Range',
'id' => 'range',
'type' => 'range',
'min' => 0,
'max' => 60,
'step' => 5,
]
```
--------------------------------
### Get Student ID from URL Parameter
Source: https://docs.metabox.io/tutorials/create-online-admission-form
This PHP function retrieves a specific GET parameter from the URL, which is used to get the student ID for the reviewing page.
```php
function estar_get_method_function( $param ) {
return isset( $_GET[$param] ) ? $_GET[$param] : '';
}
```
--------------------------------
### Initiate Data Migration for Companies
Source: https://docs.metabox.io/tutorials/move-data-to-custom-tables
This function is hooked to 'admin_init' and runs when a specific URL parameter is accessed. It checks user capabilities and handles pagination to process companies in batches.
```php
function estar_child_data_company() {
if ( empty( $_GET['move-data-companies'] ) || ! current_user_can( 'manage_options' ) ) {
return;
}
$paged = isset( $_GET['estar-child-paged'] ) ? $_GET['estar-child-paged'] : 0;
$paged += 1;
$url = add_query_arg( 'estar-child-paged', $paged,
'https://yourwebsite.com/wp-admin/?move-data-companies=1' );
$posts = estar_child_admin_records_get_companies( $paged );
if ( empty( $posts ) ) {
die( 'Done' );
}
foreach ( $posts as $post ) {
estar_child_move_data_company( $post );
}
echo "