Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created August 23, 2022 13:40
Show Gist options
  • Save ideadude/da9923f94b0d6151faf5dcdae6f30980 to your computer and use it in GitHub Desktop.
Save ideadude/da9923f94b0d6151faf5dcdae6f30980 to your computer and use it in GitHub Desktop.
Example of how to update a PMPro user field with code.
<?php
/**
* Update a PMPro User Field with Code.
* We find the home_country field and update the options.
* User fields are loaded into the pmpro_user_fields globals
* during the init hook, priority 1. So we just need to run after that.
*/
function my_update_location_options() {
global $pmpro_user_fields, $pmpro_countries;
// In case fields aren't set, e.g. if PMPro isn't loaded.
if ( empty( $pmpro_user_fields ) ) {
return;
}
// Find our field.
foreach ( $pmpro_user_fields as $group => $fields ) {
foreach( $fields as $key => $field ) {
if ( $field->name == 'home_country' ) {
$home_country_group = $group;
$home_country_field = $key;
break 2;
}
}
}
// If found, update it.
if ( isset( $home_country_group ) && isset( $home_country_field ) ) {
$pmpro_user_fields[$home_country_group][$home_country_field]->options = array_merge( array('' => 'Choose One' ), $pmpro_countries );
}
}
add_action( 'init', 'my_update_location_options' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment