Skip to content

Instantly share code, notes, and snippets.

@xnau
Created September 22, 2021 20:07
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/400879e4403ed54ac1e656eb8a923ebd to your computer and use it in GitHub Desktop.
Save xnau/400879e4403ed54ac1e656eb8a923ebd to your computer and use it in GitHub Desktop.
Demonstrates how to implement a Participants Database custom dynamic hidden field that gets its value from the shortcode
<?php
/**
* Plugin Name: PDB Custom Dynamic Field
* Description: demonstrates how to place a value from the shortcode into a dynamic hidden field
*/
class PDb_Custom_Dynamic_Field {
/**
* @var string the dynamic value key
*
* the key (default value) to use in the dynamic hidden field is "shortcode->user_role"
* the attribute to use in the shortcode will be: "user_role"
*/
private $value_key = 'user_role';
/**
* @var string holds the replacement value
*/
private $value;
/**
* sets up the filters
*/
public function __construct()
{
// this will get the shortcode value from the signup shortcode
add_filter( 'pdb-shortcode_call_pdb_signup', array( $this, 'assign_dynamic_field' ) );
}
/**
* gets the value from the shortcode and uses it in the dynamic key replacement
*
* @param array $shortcode_atts
* @return array shortcode_atts
*/
public function assign_dynamic_field( $shortcode_atts )
{
if ( isset( $shortcode_atts[ $this->value_key ] ) ) {
/* get the value from the shortcode
* the attribute in the shortcode should be "user_role"
* so, in the shortcode you'd use something like this:
* [pdb_signup user_role=editor]
*/
$this->value = $shortcode_atts[ $this->value_key ];
add_filter( 'pdb-dynamic_value', array( $this, 'replace_dynamic_value' ), 10, 2 );
}
return $shortcode_atts;
}
/**
* replaces the dynamic key with the value
*
* @param string $value the dynamic value (will be empty string)
* @param string $dynamic_key the key value from the dynamic hidden field
* @return string the replacement value
*/
public function replace_dynamic_value( $value, $dynamic_key )
{
/* see if the hidden field has our dynamic key
* should be "shortcode->user_role"
*/
if ( $dynamic_key === 'shortcode->' . $this->value_key ) {
$value = $this->value;
}
return $value;
}
}
new PDb_Custom_Dynamic_Field();
@xnau
Copy link
Author

xnau commented Sep 22, 2021

This example code creates a new dynamic field value with the key "user_role" that gets its value from an attribute in the shortcode.

For this to work, you need to:

  1. set up a hidden field with a dynamic value (default value) of "shortcode->user_role" and "signup" checked
  2. place the value you want inserted into the shortcode like this: [pdb_signup user_role="editor"]
  3. when the signup form is submitted, the user_role value will be saved with the record

@xnau
Copy link
Author

xnau commented Sep 22, 2021

This plugin is designed so that when this feature is added to Participants Database (probably version 1.9.8), it will work the same way, you just won't need this plugin to set it up.

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