Hooks: Actions & Filters

Discover how you can customize your Modern Fields experience with hooks and filters.

General hooks

This section covers hooks aimed to improve your Modern Fields experience as developer and site creator.

Change the display value of a field

modern-fields/get_field/value

Since: 1.1.0

Functions like get_field() and the_field() retrieve the raw meta value, but you can change that with this filter.

PHP

You can also set the name of the field directly in the field name:

PHP

Change the minimum role to access Modern Fields

modern-fields/permissions

Since: 1.0.2

By default, only administrators or user with manage_options capability can access Modern Fields admin pages.

To change that, use the permission hook:

PHP

Remove blocks from field group editor

modern-fields/blocks/disabled_blocks

Since: 1.0.0

Disable unwanted blocks in the field group editor. Check the full core blocks list on the official WordPress documentation.

PHP

By default, Modern Fields already filters most of the useless blocks in the fields context.

Add a new menu to Modern Fields admin pages

modern-fields/admin/nav_items

Since: 1.0.2

Adds a menu entry on the Modern Fields admin pages. This hook is useful for add-on creators.

PHP

Provide the WordPress page slug as defined in add_menu_page function.

Send additional data to block editor

modern-fields/admin/script_data

Since: 1.0.2

Send additional data from PHP to modernFields JS global in the editor.

PHP

Change who can use the fields in the editor

modern-fields/editor_permissions

Since: 1.4.0

The Field value block, the Query Loop filters and the other tools used while editing content ask for the edit_posts capability by default. Return another capability to change that. Which field groups a user sees is still decided by the roles set on each group.

PHP

Change the meta key of a bidirectional relation

modern-fields/bidirectional/meta_key

Since: 1.4.0

A bidirectional field records the reverse relation in a post meta named mf_linked_from_{field_name}. This filter changes that key. Change it before the field is used: relations already recorded under the old key are not moved.

PHP

Hooks for add-ons

This section cover all the hooks useful for creating a Modern Fields add-on.

Register a screen in Modern fields settings

modern-fields/admin/menu_tabs

Since: 1.1.0

This PHP filters allows you to register a new screen along with a menu entry the Modern Fields settings.

PHP

Sanitize and describe the value of a custom field

modern-fields/meta/sanitize_callback and modern-fields/meta/schema

Since: 1.0.0

The first one changes the function sanitizing the value of a field before it is saved. The second one describes the content of an array or object value. Both receive the field as an object. See Create a custom field type.

PHP

Map field hooks

These filters configure the Map field: which provider draws the maps, and the credentials and services it uses. Each one runs after its matching constant, so it wins.

FilterSinceArgumentsRole
modern-fields/maps/provider1.4.0string $idThe provider drawing the maps: google or osm. Constant: MODERN_FIELDS_MAPS_PROVIDER (since 1.5.0)
modern-fields/maps/api_key1.4.0string $key, string $providerIdThe Google Maps API key. Constant: MODERN_FIELDS_GOOGLE_MAPS_API_KEY
modern-fields/maps/map_id1.4.0string $mapId, string $providerIdThe site-wide Google Map ID. Constant: MODERN_FIELDS_GOOGLE_MAPS_MAP_ID
modern-fields/maps/providers1.4.0array $providersRegister another map provider, keyed by id
modern-fields/maps/osm/tile_url1.5.0string $url, string $providerIdThe OpenStreetMap tile URL. Constant: MODERN_FIELDS_OSM_TILE_URL
modern-fields/maps/osm/geocoder_url1.5.0string $url, string $providerIdThe Nominatim-compatible geocoder. Constant: MODERN_FIELDS_OSM_GEOCODER_URL
modern-fields/maps/osm/attribution1.5.0string $htmlThe credit line shown on OpenStreetMap maps

For example, to use a different Google key outside production:

PHP