Skip to content

Instantly share code, notes, and snippets.

@MaximilianoRicoTabo
Created September 21, 2023 20:05
Show Gist options
  • Save MaximilianoRicoTabo/cb5741d66b09032eefbc7eee558774fd to your computer and use it in GitHub Desktop.
Save MaximilianoRicoTabo/cb5741d66b09032eefbc7eee558774fd to your computer and use it in GitHub Desktop.
Avoiding Undefined Functions doc update
<?php
/**
*
* https://www.paidmembershipspro.com/avoiding-undefined-function-errors-hooks-filters-custom-code/
*
* SUMMARY of article examples
*
* 1- pmprorh_add_registration_field (Register Helper)
* 2- pmpro_has_membership_access (PMPro core)
* 3- pmpro_hasMembershipLevel (PMPro core)
* 4- MemberOrder (PMPro core)
* 5- TGMPA https://github.com/TGMPA/TGM-Plugin-Activation
* 6- PMPRO_VERSION (PMPro core)
* 7- pmpro_checkout_level filter (PMPro core)
*
*/
//1)
//Old register helper https://github.com/strangerstudios/pmpro-register-helper/blob/aa140198b62dc6abd6c4328d8c829ac56426569e/pmpro-register-helper.php#L117
/*
Add a field to the PMProRH regisration fields global
$where refers to various hooks in the PMPro checkout page and can be:
- after_username
- after_password
- after_email
- after_captcha
- checkout_boxes
- after_billing_fields
- before_submit_button
- just_profile (make sure you set the profile attr of the field to true or admins)
*/
function pmprorh_add_registration_field($where, $field) {
global $pmprorh_registration_fields;
if(empty($pmprorh_registration_fields[$where])) {
$pmprorh_registration_fields[$where] = array();
}
if ( !empty($field) && is_a( $field, 'PMProRH_Field') ) {
$pmprorh_registration_fields[$where][] = $field;
return true;
}
return false;
}
// New core function https://github.com/strangerstudios/paid-memberships-pro/blob/2226f37d713b73fbefba81c7f6ca8a3600b708ce/includes/fields.php#L38
/**
* Add a field to the PMProRH regisration fields global
*
* $where refers to various hooks in the PMPro checkout page and can be:
* - after_username
* - after_password
* - after_email
* - after_captcha
* - checkout_boxes
* - after_billing_fields
* - before_submit_button
* - just_profile (make sure you set the profile attr of the field to true or admins)
*/
function pmpro_add_user_field( $where, $field ) {
global $pmpro_user_fields;
/**
* Filter the group to add the field to.
*
* @since 2.9.3
*
* @param string $where The name of the group to add the field to.
* @param PMProField $field The field being added.
*/
$where = apply_filters( 'pmpro_add_user_field_where', $where, $field );
/**
* Filter the field to add.
*
* @since 2.9.3
*
* @param PMProField $field The field being added.
* @param string $where The name of the group to add the field to.
*/
$field = apply_filters( 'pmpro_add_user_field', $field, $where );
if(empty($pmpro_user_fields[$where])) {
$pmpro_user_fields[$where] = array();
}
if ( ! empty( $field ) && pmpro_is_field( $field ) ) {
$pmpro_user_fields[$where][] = $field;
return true;
}
return false;
}
/********************************
* NEW PROPOSED EXAMPLE *
*********************************/
function my_pmpro_add_fields_to_checkout() {
// Don't break if PMPro is out of date or not loaded.
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return false;
}
// This is where our fields code will go.
$fields = new PMPro_Field(
'company',
'text',
array(
'label' => 'Company',
'size' => 40,
'class' => 'company',
'profile' => true,
'required' => true,
'levels' => array(1,2),
'memberslistcsv' => true
)
);
// Add a field group to put our fields into.
pmpro_add_field_group( 'Business Details' );
// Add all of our fields into that group.
foreach ( $fields as $field ) {
pmpro_add_user_field(
'Business Details',
$field
);
}
}
add_action( 'init', 'my_pmpro_add_fields_to_checkout' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment