Last active
January 8, 2022 01:26
-
-
Save sc0ttkclark/7a5b53a05b2ffbfd4d017c9db4426675 to your computer and use it in GitHub Desktop.
Register custom field types with Paid Memberships Pro
This file contains hidden or 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 | |
/** | |
* Register custom field types with Paid Memberships Pro. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
/** | |
* Register example custom fields for the "my_datetime", "my_date", and "my_time" custom field types. | |
*/ | |
function my_pmpro_custom_field_type_init() { | |
// Check if the function we need is available. | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return; | |
} | |
$fields = []; | |
$fields[] = new PMProRH_Field( 'test_my_datetime', 'my_datetime', [ | |
'label' => 'Test custom field type for HTML5 datetime', | |
'profile' => true, | |
'required' => true, | |
] ); | |
$fields[] = new PMProRH_Field( 'test_my_date', 'my_date', [ | |
'label' => 'Test custom field type for HTML5 date', | |
'profile' => true, | |
'required' => true, | |
] ); | |
$fields[] = new PMProRH_Field( 'test_my_time', 'my_time', [ | |
'label' => 'Test custom field type for HTML5 time', | |
'profile' => true, | |
'required' => true, | |
] ); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( 'checkout_boxes', $field ); | |
} | |
} | |
add_action( 'init', 'my_pmpro_custom_field_type_init' ); | |
/** | |
* Render the HTML for the "my_datetime" custom field type. | |
* | |
* @param string $r The field HTML. | |
* @param mixed $value The field value. | |
* @param PMPro_Field $field The field object. | |
* | |
* @return string The field HTML. | |
*/ | |
function my_pmpro_custom_field_type_datetime_html( $r, $value, $field ) { | |
if ( empty( $field->html_attributes ) ) { | |
$field->html_attributes = []; | |
} | |
$field->html_attributes['type'] = 'datetime'; | |
if ( ! empty( $field->id ) ) { | |
$field->html_attributes['id'] = $field->id; | |
} | |
if ( ! empty( $field->name ) ) { | |
$field->html_attributes['name'] = $field->name; | |
} | |
$field->html_attributes['value'] = wp_unslash( $value ); | |
if ( ! empty( $field->class ) ) { | |
$field->html_attributes['class'] = $field->class; | |
} | |
if ( ! empty( $field->readonly ) ) { | |
$field->html_attributes['readonly'] = $field->readonly; | |
} | |
return '<input ' . $field->getHTMLAttributes() . ' />'; | |
} | |
add_filter( 'pmpro_field_get_html_my_datetime', 'my_pmpro_custom_field_type_datetime_html', 10, 3 ); | |
/** | |
* Render the HTML for the "my_date" custom field type. | |
* | |
* @param string $r The field HTML. | |
* @param mixed $value The field value. | |
* @param PMPro_Field $field The field object. | |
* | |
* @return string The field HTML. | |
*/ | |
function my_pmpro_custom_field_type_date_html( $r, $value, $field ) { | |
if ( empty( $field->html_attributes ) ) { | |
$field->html_attributes = []; | |
} | |
$field->html_attributes['type'] = 'date'; | |
if ( ! empty( $field->id ) ) { | |
$field->html_attributes['id'] = $field->id; | |
} | |
if ( ! empty( $field->name ) ) { | |
$field->html_attributes['name'] = $field->name; | |
} | |
$field->html_attributes['value'] = wp_unslash( $value ); | |
if ( ! empty( $field->class ) ) { | |
$field->html_attributes['class'] = $field->class; | |
} | |
if ( ! empty( $field->readonly ) ) { | |
$field->html_attributes['readonly'] = $field->readonly; | |
} | |
return '<input ' . $field->getHTMLAttributes() . ' />'; | |
} | |
add_filter( 'pmpro_field_get_html_my_date', 'my_pmpro_custom_field_type_date_html', 10, 3 ); | |
/** | |
* Render the HTML for the "my_time" custom field type. | |
* | |
* @param string $r The field HTML. | |
* @param mixed $value The field value. | |
* @param PMPro_Field $field The field object. | |
* | |
* @return string The field HTML. | |
*/ | |
function my_pmpro_custom_field_type_time_html( $r, $value, $field ) { | |
if ( empty( $field->html_attributes ) ) { | |
$field->html_attributes = []; | |
} | |
$field->html_attributes['type'] = 'time'; | |
if ( ! empty( $field->id ) ) { | |
$field->html_attributes['id'] = $field->id; | |
} | |
if ( ! empty( $field->name ) ) { | |
$field->html_attributes['name'] = $field->name; | |
} | |
$field->html_attributes['value'] = wp_unslash( $value ); | |
if ( ! empty( $field->class ) ) { | |
$field->html_attributes['class'] = $field->class; | |
} | |
if ( ! empty( $field->readonly ) ) { | |
$field->html_attributes['readonly'] = $field->readonly; | |
} | |
return '<input ' . $field->getHTMLAttributes() . ' />'; | |
} | |
add_filter( 'pmpro_field_get_html_my_time', 'my_pmpro_custom_field_type_time_html', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment