Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active February 10, 2022 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/3e1f3e8cc0a0b2a9182af4f4c4b1c261 to your computer and use it in GitHub Desktop.
Save xnau/3e1f3e8cc0a0b2a9182af4f4c4b1c261 to your computer and use it in GitHub Desktop.
example of creating a Participants Database Custom Form Element
<?php
/**
* Plugin Name: PDB Array Form Element
* Description: a demonstration of the basic technique for creating a custom form element in Participants Database
*
*/
add_filter( 'pdb-set_form_element_types', 'my_array_register_element' );
add_action( 'pdb-form_element_build_array','my_array_element_building_function' );
add_filter( 'pdb-before_display_form_element','my_array_element_value_display_function', 10, 2 );
/**
* adds the form element to the list of form elements
*
* @param array $elements
* @return array
*/
function my_array_register_element( $elements )
{
$elements['array'] = 'List';
return $elements;
}
/**
* supplies the form element HTML
*
* @param PDb_FormElement object $field
* @return null
*/
function my_array_element_building_function( $field )
{
$value = maybe_unserialize( $field->value );
$field->output = '
<div class="array-element-container">
<input type="text" name="' . $field->name . '[]" value="' . $value[0] . '" />
<input type="text" name="' . $field->name . '[]" value="' . $value[1] . '" />
<input type="text" name="' . $field->name . '[]" value="' . $value[2] . '" />
</div>
';
}
/**
* shows the value of the array
*
* @param bool $value the raw value of the field, this comes in as bool false if it has not
* been altered, the filter must return bool false if the value is not
* to be altered by the filter function
* @param PDb_Field_Item object $field
* @return string|bool HTML value to display, bool false if unaltered
*/
function my_array_element_value_display_function( $value, $field )
{
if ( $field->form_element === 'array' ) {
$value = '<span class="array-element-value">' . implode( ', ', maybe_unserialize( $field->value ) ) . '</span>';
}
return $value;
}
@redligot2009
Copy link

Hi, when I use your template I get an error: "Warning: Missing argument 2 for my_array_element_value_display_function() in C:\wamp64\www\ADHD\wp-content\plugins\pdb_date_field.php on line 119"

@xnau
Copy link
Author

xnau commented Feb 10, 2022

I've updated the code to be able to handle the field's value stored as a serialized array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment