toolbox/helpers/get_meta_box_field/type={fieldtype}

Estimated reading: 2 minutes

This filters allows you to modify the output of a specific fieldtype.

Parameters

  • $string (array) The passed in output
  • $field_object (array) the field object value
  • $value (array) the field’s value as returned by Meta Box
  • $attr (array) the settings applied to this instance
  • $post_id (integer) the ID of the post to get this field for

Usage

When adding your custom filters for fieldtypes you need to determine the priority of your filter first. You can add build on previous filters, or start. If you decide to start over, for clean code you should unhook the filters running before yours. But you can also start without using the passed in $string variable and only return your filters result.

<?php
    
    /**
 * Default return function for a text or unknown fieldtype
 */
public static function meta_box_return_text( $string , $field_object , $value , $atts = null , $postid = null ) {
	if ( is_array( $value ) ) return $string;
	return $string . $value;
}

The example above is the code for the default text-field display. It checks is the $value for the requested field is of type array and if so returns the passed in string. Otherwise (assuming the value is of type string) it takes the passed in $string and appends that value.

But maybe you know that you want to display each and every text-field named ‘codefield’ as a <code>$value</code>

You can do that by adding the following filter code into your functions.php:

<?php

add_filter( 'toolbox/helpers/get_meta_box_field/type=text', 'my_text_codefield_filter', 10, 5 );

/**
 * Your custom filter for a text-field with the name 'codefield'
 */
public static function my_text_codefield_filter( $string , $field_object , $value , $atts = null , $postid = null ) {
    // check if the requested fieldname is 'codefield' and the type of the field_object is 'text'
    // if so, append and prepend the string by
    if ( $atts['field'] == 'codefield' && $field_object[ 'type' ] == 'text' ) return '<code>' . $string . '</code>'; 
    // if not, pass the input-string to the next filter 
    return $string; 
  } 

Because the callback can also be hooked to other filters, we need to perform a check on the filetype inside the callback, not just assume we won’t hook it on other fieldtypes.

Return value

Make sure to return an array with field-settings.

Share this Doc

toolbox/helpers/get_meta_box_field/type={fieldtype}

Or copy link

CONTENTS