Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created August 9, 2024 13:25
Show Gist options
  • Save plugin-republic/f621c52f35eba82935ccc05e24ffa384 to your computer and use it in GitHub Desktop.
Save plugin-republic/f621c52f35eba82935ccc05e24ffa384 to your computer and use it in GitHub Desktop.
<?php
/**
* Automatically populate a field with user data
* Enter the user field in the 'Default' field
* Wrapped in {}
* E.g. {first_name}
*/
function junhee_populate_user_field( $value, $id, $item, $posted ) {
if( is_user_logged_in() ) {
// Check if the default value is wrapped in {}
if( substr( $value, 0, 1) == '{' ) {
$stripped_value = str_replace( array( '{', '}' ), '', $value );
$user_id = get_current_user_id();
$value = get_user_meta( $user_id, $stripped_value, true );
}
}
return $value;
}
add_filter( 'pewc_default_field_value', 'junhee_populate_user_field', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment