Created
September 22, 2021 20:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
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
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: