Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active March 20, 2022 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/613f05399cd4aa7d92e21b2516a472e0 to your computer and use it in GitHub Desktop.
Save xnau/613f05399cd4aa7d92e21b2516a472e0 to your computer and use it in GitHub Desktop.
Demonstrates how to create a Participants Database form element that displays an iframe
<?php
/**
* Plugin Name: PDB Iframe Form Element
* Description: Demonstrates how to create a Participants Database form element that displays an iframe
*
*/
add_filter( 'pdb-set_form_element_types', 'pdb_iframe_register_element' );
add_action( 'pdb-form_element_build_iframe','pdb_iframe_element_building_function');
add_filter( 'pdb-before_display_form_element','pdb_iframe_element_value_display_function', 10, 2);
/**
* adds the form element to the list of form elements
*
* @param array $elements
* @return array
*/
function pdb_iframe_register_element( $elements )
{
$elements['iframe'] = 'Iframe';
return $elements;
}
/**
* supplies the form element HTML
*
* @param PDb_FormElement $field
* @return null
*/
function pdb_iframe_element_building_function( $field )
{
/*
* all we need to do here is set it as a text line element so the
* src value can be set
*/
$field->form_element = 'text-line';
}
/**
* shows the value of the array
*
* @param array $value the raw value of the field
* @param PDb_Field_Item object $field
* @return string HTML
*/
function pdb_iframe_element_value_display_function( $value, $field )
{
if ( $field->form_element === 'iframe' ) {
// if there is no src defined or the src value is invalid, show nothing
if ( ! $field->has_content() ) {
return '';
}
$value = '
<div class="pdb-iframe-container ' . $field->name . '-iframe">
<iframe ' . pdb_iframe_html_attributes( $field->attributes() ) . ' src="' . esc_attr( $field->value() ) . '" ></iframe>
</div>
';
}
return $value;
}
/**
* converts the attributes array into HTML attributes
*
* @param array $atts
* @return string the HTML attributes
*/
function pdb_iframe_html_attributes( $atts )
{
$atts_html = '';
// defines a list of allowed attributes
$safe_atts = array(
'align',
'frameborder',
'height',
'longdesc',
'marginheight',
'marginwidth',
'name',
'sandbox',
'scrolling',
'width',
'allowfullscreen',
'style',
);
// this assembles the attributes into a valid attributes string
foreach ( $atts as $attribute => $value ) {
if ( in_array( $attribute, $safe_atts ) ) {
$atts_html .= ' ' . esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}
return $atts_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment