Below, you’ll find a detailed guide on how to add the Profile Extra Fields by BestWebSoft Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Profile Extra Fields by BestWebSoft Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Profile Extra Fields by BestWebSoft Plugin and the shortcodes it provides:
"Profile Extra Fields by BestWebSoft is a WordPress plugin designed to add custom fields to user profiles, enhancing data collection and user management."
- [prflxtrflds_user_data]
- [prflxtrflds_field]
- [prflxtrflds_user_data_edit_form]
Profile Extra Fields by BestWebSoft [prflxtrflds_user_data] Shortcode
The Profile Extra Fields shortcode is a powerful tool for managing user data within WordPress. It allows you to fetch and display user-specific information based on parameters like user ID, role, and display options. This shortcode can be used to create dynamic, user-oriented content, enhancing the overall user experience. It also supports error handling and can be configured to show or hide empty columns.
Shortcode: [prflxtrflds_user_data]
Parameters
Here is a list of all possible prflxtrflds_user_data shortcode parameters and attributes:
user_id
– Specifies the user ID to be retrieveduser_role
– Specifies the user role to be retrieveddisplay
– Defines how the data should be displayedfield_id
– Specifies the field ID to be retrievedexport
– Exports the user data if set to true
Examples and Usage
Basic example – Displaying user data based on user ID
[prflxtrflds_user_data user_id="1" /]
Advanced examples
Displaying user data based on user role and customizing the display format
[prflxtrflds_user_data user_role="administrator,editor" display="rows" /]
Displaying user data based on multiple user IDs and a specific field ID
[prflxtrflds_user_data user_id="1,2,3" field_id="5" /]
Displaying user data for the current logged-in user
[prflxtrflds_user_data user_id="get_current_user" /]
Exporting user data based on user role
[prflxtrflds_user_data user_role="subscriber" export="true" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [prflxtrflds_user_data]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'prflxtrflds_user_data', 'prflxtrflds_show_data' );
Shortcode PHP function:
function prflxtrflds_show_data( $param ) {
global $wpdb, $prflxtrflds_options;
$error_message = '';
$user_ids = $field_id = array();
$export_action = ( isset( $param['export'] ) && true === $param['export'] ) ? true : false;
if ( ! isset( $prflxtrflds_options ) ) {
prflxtrflds_settings();
}
extract(
shortcode_atts(
array(
'user_id' => '',
'user_role' => '',
'display' => '',
'field_id' => '',
),
$param
)
);
/** Get user id param */
if ( ! empty( $param['user_id'] ) ) {
if ( 'get_current_user' === $param['user_id'] ) {
$user_ids = array( get_current_user_id() );
} else {
$user_ids = explode( ',', $param['user_id'] );
if ( is_array( $user_ids ) ) {
/** If lot user ids */
foreach ( $user_ids as $user_id ) {
/** Check for existing user */
if ( ! is_numeric( $user_id ) || ! get_user_by( 'id', intval( $user_id ) ) ) {
/** Show error if user id not exist, or data is uncorrect */
$error_message = sprintf( __( 'User with entered id(id=%1$s) does not exist!', 'profile-extra-fields' ), esc_attr( $user_id ) );
}
}
}
}
}
/** Get user role param */
if ( ! empty( $param['user_role'] ) ) {
$user_roles = explode( ',', $param['user_role'] );
if ( is_array( $user_roles ) ) {
foreach ( $user_roles as $role ) {
/** Check for exist user role */
$role_id = $wpdb->get_var( $wpdb->prepare( 'SELECT `role_id` FROM `' . $wpdb->base_prefix . 'prflxtrflds_roles_id` WHERE `role` = %s', $role ) );
if ( ! empty( $role_id ) ) {
/** Get user ids by role */
$ids_for_role = $wpdb->get_col( $wpdb->prepare( 'SELECT `user_id` FROM `' . $wpdb->base_prefix . 'prflxtrflds_user_roles` WHERE `role_id`=%d', $role_id ) );
if ( ! empty( $ids_for_role ) ) {
$user_ids = array_merge( $user_ids, $ids_for_role );
}
}
}
/** If not exist users for choisen role. User ids is empty and select all users */
if ( empty( $user_ids ) ) {
$error_message = sprintf( __( 'There are no users for the selected roles ( %1$s )', 'profile-extra-fields' ), esc_attr( $param['user_role'] ) );
}
}
}
/** Get display options */
if ( ! empty( $param['display'] ) ) {
/** If this values is not supported */
if ( ! in_array( $param['display'], array( 'left', 'top', 'right', 'side', 'columns', 'rows' ) ) ) {
$error_message .= sprintf( __( 'Unsupported shortcode option(display=%1$s)', 'profile-extra-fields' ), esc_attr( $param['display'] ) );
} else {
$display = $param['display'];
}
} else {
/** If value not in shortcode, get from options. Top by default */
$display = isset( $prflxtrflds_options['header_table'] ) ? $prflxtrflds_options['header_table'] : 'columns';
}
if ( ! empty( $error_message ) ) {
if ( ! empty( $prflxtrflds_options['shortcode_debug'] ) ) {
return sprintf( '<p>%1$s. %2$s</p>', __( 'Shortcode output error', 'profile-extra-fields' ), $error_message );
} else {
return '';
}
} else {
$wp_users = $wpdb->base_prefix . 'users';
$wp_usermeta = $wpdb->base_prefix . 'usermeta';
$table_fields_id = $wpdb->base_prefix . 'prflxtrflds_fields_id';
$table_user_field_data = $wpdb->base_prefix . 'prflxtrflds_user_field_data';
$table_field_values = $wpdb->base_prefix . 'prflxtrflds_field_values';
$table_roles_id = $wpdb->base_prefix . 'prflxtrflds_roles_id';
$table_roles_and_fields = $wpdb->base_prefix . 'prflxtrflds_roles_and_fields';
$table_user_roles = $wpdb->base_prefix . 'prflxtrflds_user_roles';
/** Collate all users ids */
$get_for_selected_users = '';
if ( ! empty( $user_ids ) ) {
$get_for_selected_users = ' AND `' . $table_user_roles . "`.`user_id` IN ( '" . implode( "', '", $user_ids ) . "' )";
}
/** Get options - Which fields must be displayed */
$get_for_available_fields = '';
if ( $export_action ) {
$fields_sql = 'SELECT `field_id` FROM `' . $wpdb->base_prefix . 'prflxtrflds_fields_id`';
$totalitems = $wpdb->get_col( $fields_sql );
$field_ids = implode( "', '", $totalitems );
$get_for_available_fields = ' AND `' . $table_fields_id . "`.`field_id` IN ('" . $field_ids . "')";
} else {
if ( ! empty( $prflxtrflds_options['available_fields'] ) ) {
$field_ids = implode( "', '", $prflxtrflds_options['available_fields'] );
$get_for_available_fields = ' AND `' . $table_fields_id . "`.`field_id` IN ('" . $field_ids . "')";
}
}
$get_for_available_field_value = '';
if ( ! empty( $prflxtrflds_options['available_values'] ) ) {
$i = 0;
$extended_value = '';
foreach ( $prflxtrflds_options['available_values'] as $value => $key ) {
if ( '' !== $key ) {
if ( 0 !== $i ) {
$extended_value .= ' OR ';
}
$extended_value .= "(`user_value`='" . $key . "' AND `field_id`='" . $value . "')";
$i++;
}
}
if ( '' !== $extended_value ) {
$get_for_available_field_value = ' AND `' . $table_user_roles . '`.`user_id` IN
(SELECT `user_id`
FROM `' . $table_user_field_data . '`
WHERE ' . $extended_value . ')';
}
}
$get_users_data_sql = 'SELECT ' . $wp_users . '.`user_nicename` , `display_name` , ' .
$table_user_roles . '.`user_id`, ' .
$table_fields_id . '.`field_name`, ' .
$table_fields_id . '.`field_id`, ' .
$table_fields_id . '.`field_type_id` ' .
'FROM ' . $wp_users .
' INNER JOIN ' . $table_user_roles .
' ON ' . $table_user_roles . '.`user_id`=' . $wp_users . '.`ID` ' . $get_for_selected_users . $get_for_available_field_value .
' LEFT JOIN ' . $table_roles_and_fields .
' ON ' . $table_roles_and_fields . '.`role_id`=' . $table_user_roles . '.`role_id` ' .
' LEFT JOIN ' . $table_roles_id .
' ON ' . $table_roles_id . '.`role_id`=' . $table_user_roles . '.`role_id` ' .
' LEFT JOIN ' . $table_fields_id .
' ON ' . $table_fields_id . '.`field_id`=' . $table_roles_and_fields . '.`field_id` ' . $get_for_available_fields;
if ( is_multisite() ) {
$get_users_data_sql .= ' WHERE ' . $wp_users . '.`ID` IN ( ' . implode( ',', get_users( 'blog_id=' . get_current_blog_id() . '&fields=ID') ) . ')';
}
/** Group all and Add sorting order */
$get_users_data_sql .= ' GROUP BY `' . $wp_users . '`.`ID`, `' . $table_fields_id . '`.`field_id` ORDER BY `' . $wp_users . '`.`user_nicename` ' . $prflxtrflds_options['sort_sequence'];
/** Begin collate data to print shortcode */
ob_start();
$printed_table = $wpdb->get_results( $get_users_data_sql, ARRAY_A );
if ( ! empty( $printed_table ) ) {
foreach ( $printed_table as $key => $column ) {
if ( ! empty( $column['field_id'] ) ) {
if ( in_array( $column['field_type_id'], array( '3', '4', '5' ) ) ) {
$user_value = $wpdb->get_col(
$wpdb->prepare(
'SELECT `value_name`
FROM ' . $table_field_values .
' WHERE `value_id` IN ( SELECT `user_value` FROM ' . $table_user_field_data . ' WHERE `user_id`=%d AND `field_id`=%d )',
$column['user_id'],
$column['field_id']
)
);
$printed_table[ $key ]['value'] = implode( ', ', $user_value );
} else {
$printed_table[ $key ]['value'] = $wpdb->get_var(
$wpdb->prepare(
'SELECT `user_value`
FROM `' . $table_user_field_data . '` WHERE `user_id`= %d
AND `field_id`=%d LIMIT 1;',
$column['user_id'],
$column['field_id']
)
);
}
}
}
/** Get all field names */
/** By default show all fields */
$all_fields_sql = 'SELECT DISTINCT `field_id`, `field_name` FROM ' . $table_fields_id;
$all_fields = $wpdb->get_results( $all_fields_sql, ARRAY_A );
/** If need not show empty collumns */
if ( ! $export_action && 0 === $prflxtrflds_options['show_empty_columns'] ) {
/** Delete not filled columns */
foreach ( $all_fields as $key => $one_field ) {
$is_empty = 1;
foreach ( $printed_table as $printed_line ) {
/** If field not empty */
if ( $printed_line['field_id'] === $one_field['field_id'] ) {
if ( ! empty( $printed_line['value'] ) ) {
$is_empty = 0;
break;
}
}
}
if ( 1 === $is_empty ) {
/** Delete if empty from all fields */
unset( $all_fields[ $key ] );
}
}
}
if ( 'columns' === $display ) {
if ( $export_action ) {
$return_output_export = array();
$output_export[] = __( 'User ID', 'profile-extra-fields' );
$output_export[] = __( 'Username', 'profile-extra-fields' );
$output_export[] = __( 'User role', 'profile-extra-fields' );
$output_export[] = __( 'Name', 'profile-extra-fields' );
$output_export[] = __( 'Email', 'profile-extra-fields' );
$output_export[] = __( 'Posts', 'profile-extra-fields' );
foreach ( $all_fields as $one_field ) {
$output_export[] = $one_field['field_name'];
}
$return_output_export[] = $output_export;
unset( $output_export );
foreach ( $printed_table as $column_key => $column ) {
/** If is new username */
if ( ! isset( $printed_table[ $column_key - 1 ] ) ||
( isset( $printed_table[ $column_key - 1 ] ) &&
$printed_table[ $column_key - 1 ]['user_nicename'] !== $column['user_nicename'] )
) {
$user = get_user_by( 'ID', $column['user_id'] );
$output_export[] = $column['user_id'];
$output_export[] = esc_attr( $column['user_nicename'] );
$output_export[] = implode( ', ', $user->roles );
$output_export[] = $user->first_name . ' ' . $user->last_name;
$output_export[] = $user->user_email;
$output_export[] = count_user_posts( $user->ID );
$user_fields_temp = $all_fields;
}
foreach ( $user_fields_temp as $key => $one_field ) {
if ( $column['field_id'] === $one_field['field_id'] ) {
$user_fields_temp[ $key ]['user_value'] = esc_attr( $column['value'] );
break;
}
}
if ( ! isset( $printed_table[ $column_key + 1 ] ) ||
( isset( $printed_table[ $column_key + 1 ] ) &&
$printed_table[ $column_key + 1 ]['user_nicename'] !== $column['user_nicename'] )
) {
if ( ! empty( $user_fields_temp ) ) {
foreach ( $user_fields_temp as $key => $value ) {
$output_export[] = $value['user_value'];
}
}
$return_output_export[] = $output_export;
unset( $output_export );
}
}
return $return_output_export;
}
?>
<div style ="max-width: 100%; overflow-x: auto;margin-bottom: 15px;">
<table>
<thead>
<tr>
<?php if ( 1 === $prflxtrflds_options['show_id'] ) { ?>
<th><?php esc_html_e( 'User ID', 'profile-extra-fields' ); ?></th>
<?php } ?>
<th><?php esc_html_e( 'Username', 'profile-extra-fields' ); ?></th>
<?php foreach ( $all_fields as $one_field ) { ?>
<th><?php echo esc_html( $one_field['field_name'] ); ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
foreach ( $printed_table as $column_key => $column ) {
/** If is new username */
if ( ! isset( $printed_table[ $column_key - 1 ] ) ||
( isset( $printed_table[ $column_key - 1 ] ) &&
$printed_table[ $column_key - 1 ]['user_nicename'] !== $column['user_nicename'] )
) {
?>
<tr>
<?php if ( 1 === $prflxtrflds_options['show_id'] ) { ?>
<td><?php echo esc_attr( $column['user_id'] ); ?></td>
<?php } ?>
<td><?php echo esc_attr( $column[ 'username' === $prflxtrflds_options['display_user_name'] ? 'user_nicename' : 'display_name' ] ); ?></td>
<?php
$user_fields_temp = $all_fields;
}
foreach ( $user_fields_temp as $key => $one_field ) {
if ( $column['field_id'] === $one_field['field_id'] ) {
if ( ! empty( $column['value'] ) ) {
if ( 11 === intval( $column['field_type_id'] ) ) {
$user_fields_temp[ $key ]['user_value'] = '<a href="' . esc_url( $column['value'] ) . '" title="">' . esc_attr( $column['value'] ) . '</a>';
} else {
$user_fields_temp[ $key ]['user_value'] = wp_unslash( wp_kses_post( str_replace( PHP_EOL, '<br />', wp_unslash( $column['value'] ) ) ) );
}
break;
} else {
$user_fields_temp[ $key ]['user_value'] = $prflxtrflds_options['empty_value'];
}
}
}
if ( ! isset( $printed_table[ $column_key + 1 ] ) ||
( isset( $printed_table[ $column_key + 1 ] ) &&
$printed_table[ $column_key + 1 ]['user_nicename'] !== $column['user_nicename'] )
) {
if ( ! empty( $user_fields_temp ) ) {
foreach ( $user_fields_temp as $key => $value ) {
if ( isset( $value['user_value'] ) ) {
echo '<td>' . wp_unslash( wp_kses_post( $value['user_value'] ) ) . '</td>';
} else {
echo '<td>' . wp_kses_post( $prflxtrflds_options['not_available_message'] ) . '</td>';
}
}
}
?>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
<?php
} else {
if ( $export_action ) {
$distinct_users = $return_output_export = $output_export = array();
foreach ( $printed_table as $one_row ) {
/** Create array of distinct users */
if ( 0 < $one_row['user_id'] && ! isset( $distinct_users[ $one_row['user_id'] ] ) ) {
$distinct_users[ $one_row['user_id'] ] = $one_row[ 'username' === $prflxtrflds_options['display_user_name'] ? 'user_nicename' : 'display_name' ];
}
}
$output_export[] = __( 'User ID', 'profile-extra-fields' );
foreach ( array_keys( $distinct_users ) as $user_id ) {
$output_export[] = esc_attr( $user_id );
}
$return_output_export[] = $output_export;
unset( $output_export );
$output_export[] = __( 'Username', 'profile-extra-fields' );
foreach ( $distinct_users as $user_name ) {
$output_export[] = esc_attr( $user_name );
}
$return_output_export[] = $output_export;
unset( $output_export );
$output_export[] = __( 'User role', 'profile-extra-fields' );
foreach ( array_keys( $distinct_users ) as $user_id ) {
$user = get_user_by( 'ID', $user_id );
$output_export[] = implode( ', ', $user->roles );
}
$return_output_export[] = $output_export;
unset( $output_export );
$output_export[] = __( 'Name', 'profile-extra-fields' );
foreach ( array_keys( $distinct_users ) as $user_id ) {
$user = get_user_by( 'ID', $user_id );
$output_export[] = $user->first_name . ' ' . $user->last_name;
}
$return_output_export[] = $output_export;
unset( $output_export );
$output_export[] = __( 'Email', 'profile-extra-fields' );
foreach ( array_keys( $distinct_users ) as $user_id ) {
$user = get_user_by( 'ID', $user_id );
$output_export[] = $user->user_email;
}
$return_output_export[] = $output_export;
unset( $output_export );
$output_export[] = __( 'Posts', 'profile-extra-fields' );
foreach ( array_keys( $distinct_users ) as $user_id ) {
$user = get_user_by( 'ID', $user_id );
$output_export[] = count_user_posts( $user->ID );
}
$return_output_export[] = $output_export;
unset( $output_export );
foreach ( $all_fields as $one_field ) { /** Create new row for every field */
$output_export[] = esc_attr( $one_field['field_name'] );
/** Create column for every user */
foreach ( array_keys( $distinct_users ) as $one_user_id ) {
/** Get data for current field id and user */
foreach ( $printed_table as $one_row ) {
/** Skip if data not for current user */
if ( intval( $one_row['user_id'] ) !== $one_user_id ) {
continue;
}
if ( $one_field['field_id'] === $one_row['field_id'] ) {
/** If no key exist, no set $user_field_data */
if ( key_exists( 'value', $one_row ) ) {
if ( empty( $one_row['value'] ) ) {
/** Empty data for empty user value */
$user_field_data = '';
} else {
/** Save user value */
$user_field_data = $one_row['value'];
}
}
}
}
$output_export[] = esc_attr( $user_field_data );
unset( $user_field_data );
}
$return_output_export[] = $output_export;
unset( $output_export );
}
return $return_output_export;
}
$distinct_users = array();
foreach ( $printed_table as $one_row ) {
/** Create array of distinct users */
if ( 0 < $one_row['user_id'] && ! isset( $distinct_users[ $one_row['user_id'] ] ) ) {
$distinct_users[ $one_row['user_id'] ] = $one_row[ 'username' === $prflxtrflds_options['display_user_name'] ? 'user_nicename' : 'display_name' ];
}
}
?>
<div style ="max-width: 100%; overflow-x: auto;margin-bottom: 15px;">
<table>
<?php if ( 1 === $prflxtrflds_options['show_id'] ) { ?>
<tr>
<th><?php esc_html_e( 'User ID', 'profile-extra-fields' ); ?></th>
<?php foreach ( array_keys( $distinct_users ) as $user_id ) { ?>
<td><?php echo esc_attr( $user_id ); ?></td>
<?php } ?>
</tr>
<?php } /** Show user name */ ?>
<tr>
<th><?php esc_html_e( 'Username', 'profile-extra-fields' ); ?></th>
<?php foreach ( $distinct_users as $user_name ) { ?>
<td><?php echo esc_attr( $user_name ); ?></td>
<?php } ?>
</tr>
<?php foreach ( $all_fields as $one_field ) { /** Create new row for every field */ ?>
<tr>
<th><?php echo esc_attr( $one_field['field_name'] ); ?></th>
<?php
foreach ( array_keys( $distinct_users ) as $one_user_id ) { /** Create column for every user */
foreach ( $printed_table as $one_row ) {
/** Get data for current field id and user */
/** Skip if data not for current user */
if ( intval( $one_row['user_id'] ) !== $one_user_id ) {
continue;
}
if ( $one_field['field_id'] === $one_row['field_id'] ) {
/** If no key exist, no set $user_field_data */
if ( key_exists( 'value', $one_row ) ) {
if ( empty( $one_row['value'] ) ) {
/** Empty data for empty user value */
$user_field_data = '';
} else {
/** Save user value */
if ( 11 === intval( $one_row['field_type_id'] ) ) {
$user_field_data = '<a href="' . esc_url( $one_row['value'] ) . '" title="" >' . esc_attr( $one_row['value'] ) . '</a>';
} else {
$user_field_data = wp_unslash( wp_kses_post( str_replace( PHP_EOL, '<br />', $one_row['value'] ) ) );
}
}
}
}
}
?>
<td>
<?php
if ( ! isset( $user_field_data ) ) {
/** Current field not avaialible for current user */
echo esc_html( $prflxtrflds_options['not_available_message'] );
} elseif ( empty( $user_field_data ) ) {
/** This value is empty. Unset user data for next user */
echo esc_html( $prflxtrflds_options['empty_value'] );
unset( $user_field_data );
} else {
/** Print user data. Unset for next user */
echo wp_unslash( wp_kses_post( $user_field_data ) );
unset( $user_field_data );
}
?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div>
<?php
}
/** If printed table is empty */
} else {
?>
<p><?php esc_html_e( 'No data for current shortcode settings', 'profile-extra-fields' ); ?></p>
<?php
}
$prflxtrflds_shortcode_output = ob_get_contents();
ob_end_clean();
if ( ! empty( $prflxtrflds_shortcode_output ) ) {
return $prflxtrflds_shortcode_output;
}
}
}
Code file location:
profile-extra-fields/profile-extra-fields/profile-extra-fields.php
Profile Extra Fields by BestWebSoft [prflxtrflds_field] Shortcode
The Profile Extra Fields shortcode is used to display custom user fields. It retrieves the field value based on the user_id and field_id parameters. The PHP function ‘prflxtrflds_show_field’ is triggered, which checks the validity of the user and field IDs, then fetches the corresponding field value. If the IDs are invalid or non-existent, an error message is displayed.
Shortcode: [prflxtrflds_field]
Parameters
Here is a list of all possible prflxtrflds_field shortcode parameters and attributes:
field_id
– Identifier of the specific field to displayuser_id
– Identifier of the user to fetch the data from
Examples and Usage
Basic example – Displaying a specific field for the current user
[prflxtrflds_field field_id=2 /]
In this basic example, we’re using the shortcode to display a specific field (field_id=2) for the current user. If you’re logged in, this will display the value of that field for your user profile.
Advanced examples
Displaying a specific field for a specific user
[prflxtrflds_field field_id=2 user_id=3 /]
In this advanced example, we’re specifying both the field_id and the user_id. This will display the value of field_id=2 for the user with user_id=3.
Displaying multiple fields for a specific user
[prflxtrflds_field field_id="2,3,4" user_id=3 /]
In this more complex example, we’re displaying multiple fields for a specific user. The field_id parameter is a comma-separated list of field IDs, so this will display the values of fields 2, 3, and 4 for the user with user_id=3.
PHP Function Code
In case you have difficulties debugging what causing issues with [prflxtrflds_field]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'prflxtrflds_field', 'prflxtrflds_show_field' );
Shortcode PHP function:
function prflxtrflds_show_field( $param ) {
global $wpdb, $prflxtrflds_options;
$error_message = '';
if ( ! isset( $prflxtrflds_options ) ) {
prflxtrflds_settings();
}
extract(
shortcode_atts(
array(
'field_id' => '',
'user_id' => '',
),
$param
)
);
if ( empty( $param['user_id'] ) ) {
$user_id = get_current_user_id();
} else {
$user_id = $param['user_id'];
if ( ! is_numeric( $user_id ) || ! get_user_by( 'id', intval( $user_id ) ) ) {
/** Show error if user id not exist, or data is uncorrect */
$error_message = sprintf( __( 'User with entered id(id=%1$s) does not exist!', 'profile-extra-fields' ), esc_attr( $user_id ) );
}
}
$field_ids = $wpdb->get_col( 'SELECT `field_id` FROM `' . $wpdb->base_prefix . 'prflxtrflds_fields_id`' );
if ( ! in_array( $param['field_id'], $field_ids ) ) {
$error_message = sprintf( __( 'Field with entered id(id=%1$s) does not exist!', 'profile-extra-fields' ), esc_attr( $param['field_id'] ) );
}
$field_type = $wpdb->get_var(
$wpdb->prepare(
'SELECT `field_type_id`
FROM `' . $wpdb->prefix . 'prflxtrflds_fields_id`
WHERE `field_id` =%d',
$param['field_id']
)
);
if ( in_array( $field_type, array( '3', '4', '5' ) ) ) {
/** Query if type of field is checkbox, radio or drop list*/
$query = $wpdb->prepare(
'SELECT `value_name`
FROM `' . $wpdb->prefix . 'prflxtrflds_field_values`
WHERE `value_id`
IN ( SELECT `user_value` FROM `' . $wpdb->prefix . 'prflxtrflds_user_field_data` WHERE `user_id`=%d AND `field_id`=%d )',
$user_id,
$param['field_id']
);
$field = implode( ', ', $wpdb->get_col( $query ) );
} else {
$query = $wpdb->prepare(
'SELECT `user_value`
FROM `' . $wpdb->prefix . 'prflxtrflds_user_field_data`
WHERE `field_id` = %d AND `user_id` = %d',
$param['field_id'],
$user_id
);
$field = $wpdb->get_var( $query );
}
if ( ! empty( $error_message ) ) {
if ( ! empty( $prflxtrflds_options['shortcode_debug'] ) ) {
return sprintf( '<p>%1$s. %2$s</p>', esc_html__( 'Shortcode output error', 'profile-extra-fields' ), $error_message );
} else {
return '';
}
} else {
return $field;
}
}
Code file location:
profile-extra-fields/profile-extra-fields/profile-extra-fields.php
Profile Extra Fields by BestWebSoft [prflxtrflds_user_data_edit_form] Shortcode
The Profile Extra Fields shortcode is a tool that allows users to edit their profile information. It checks for a valid user ID and retrieves the user’s information. If the user has submitted the form, the code verifies the nonce for security purposes. If the nonce is valid, it checks for errors and if there are none, it updates the user’s profile information. This shortcode is highly flexible, supporting various field types such as text, textarea, checkbox, radio, select, date, time, number, phone, and URL. It also handles visibility and editability of fields based on user roles.
Shortcode: [prflxtrflds_user_data_edit_form]
Examples and Usage
Basic example – Display the user data edit form on a page or post.
[prflxtrflds_user_data_edit_form /]
Advanced examples
Display the user data edit form with specific user ID. This will allow you to edit the data of a specific user. Please replace ‘user_id’ with the actual ID of the user.
[prflxtrflds_user_data_edit_form user_id=1 /]
Display the user data edit form with specific role. This will allow you to edit the data of users with a specific role. Please replace ‘role’ with the actual role of the users.
[prflxtrflds_user_data_edit_form role='subscriber' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [prflxtrflds_user_data_edit_form]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'prflxtrflds_user_data_edit_form', 'prflxtrflds_show_edit_form' );
Shortcode PHP function:
function prflxtrflds_show_edit_form() {
global $wpdb, $prflxtrflds_options, $hook_suffix, $prflxtrflds_front_shortcode;
$message = $error = '';
$errors = new WP_Error();
if ( wp_is_json_request() ) {
return;
}
if ( ! isset( $prflxtrflds_options ) ) {
prflxtrflds_settings();
}
$user_id = get_current_user_id();
if ( ! empty( $user_id ) ) {
$user = wp_get_current_user();
$user_info = get_userdata( $user_id );
$user_role = isset( $user_info->roles ) ? implode( "', '", $user_info->roles ) : get_option( 'default_role' );
if ( isset( $_POST['prflxtrflds_front_info'] ) ) {
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['prflxtrflds_front_info'] ) ), 'prflxtrflds_save_front_info' ) ) {
$error = __( 'Sorry, your nonce did not verify.', 'profile-extra-fields' );
} else {
do_action_ref_array( 'user_profile_update_errors', array( &$errors, true, &$user ) );
if ( ! empty( $errors->errors ) ) {
$error = $errors->get_error_message();
} else {
if ( ! empty( $_POST['prflxtrflds_not_editable'] ) ) {
/** Execute not_editable fields */
$prflxtrflds_not_editable = array_map( 'intval', $_POST['prflxtrflds_not_editable'] );
$not_editable_ids = "'" . implode( "','", $prflxtrflds_not_editable ) . "'";
$wpdb->query(
$wpdb->prepare(
'DELETE FROM `' . $wpdb->base_prefix . 'prflxtrflds_user_field_data`
WHERE `user_id` = %d
AND `field_id` NOT IN (' . $not_editable_ids . ')',
$user_id
)
);
} else {
$wpdb->delete(
$wpdb->base_prefix . 'prflxtrflds_user_field_data',
array( 'user_id' => $user_id )
);
}
foreach ( $_POST['prflxtrflds_user_field_value'] as $id => $val ) {
if ( ! empty( $val ) ) {
if ( is_array( $val ) ) {
/** For checkboxes */
foreach ( $val as $user_value ) {
/** Insert or update value */
$wpdb->replace(
$wpdb->base_prefix . 'prflxtrflds_user_field_data',
array(
'user_id' => $user_id,
'field_id' => intval( $id ),
'user_value' => wp_filter_post_kses( wp_unslash( $user_value ) ),
)
);
}
$message = __( 'Profile updated', 'profile-extra-fields' );
} else {
$user_value = wp_filter_post_kses( wp_unslash( $val ) );
/** Insert or update value */
$wpdb->replace(
$wpdb->base_prefix . 'prflxtrflds_user_field_data',
array(
'user_id' => $user_id,
'field_id' => intval( $id ),
'user_value' => $user_value,
)
);
$message = __( 'Profile updated', 'profile-extra-fields' );
}
}
}
}
}
}
$plugins_data = apply_filters( 'bws_bkng_prflxtrflds_get_data', $plugins_data = array() );
$enabled_plugins = array_column( $plugins_data, 'slug' );
array_unshift(
$plugins_data,
array(
'name' => 'Profile',
'slug' => $enabled_plugins,
'exclude' => true
)
);
$hidden_nonvisible_field = '';
ob_start(); ?>
<div class="">
<?php
if ( ! empty( $message ) ) {
echo '<div class="updated notice prflxtrflds-success"><p>' . $message . '</p></div>';
}
if ( ! empty( $error ) ) {
echo '<div class="error prflxtrflds-error"><p>' . $error . '</p></div>';
} ?>
<form action="<?php the_permalink(); ?>" method="post">
<?php foreach ( $plugins_data as $plugin ) {
$args = array(
'roles' => array( $user_role ),
'show' => $plugin['slug'],
'exclude' => isset( $plugin['exclude'] ) ? $plugin['exclude'] : false,
);
$all_entry = prflxtrflds_get_fields( $args );
$custom_class = 'prflxtrflds_extra_fields_' . sanitize_title( $plugin['name'] );
if ( empty( $all_entry ) ) {
continue;
}
echo '<h3>' . $plugin['name'] . ' ' . __( 'Extra Fields', 'profile-extra-fields' ) . '</h3>';
echo '<table>';
foreach ( $all_entry as $one_entry ) {
/** Add field values */
$one_entry['available_fields'] = $wpdb->get_results(
$wpdb->prepare(
'SELECT `value_id`, `value_name`
FROM `' . $wpdb->base_prefix . 'prflxtrflds_field_values`
WHERE `field_id` = %d
ORDER BY `order`',
$one_entry['field_id']
),
ARRAY_A
);
if ( ! empty( $_POST['prflxtrflds_user_field_value'] ) && isset( $_POST['prflxtrflds_user_fields'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['prflxtrflds_user_fields'] ) ), 'prflxtrflds_user_field_action' ) ) {
if ( isset( $_POST['prflxtrflds_user_field_value'][ $one_entry['field_id'] ] ) &&
is_array( $_POST['prflxtrflds_user_field_value'][ $one_entry['field_id'] ] )
) {
/** For checkboxes */
foreach ( $_POST['prflxtrflds_user_field_value'][ $one_entry['field_id'] ] as $user_value ) {
$one_entry['user_value'][] = sanitize_text_field( wp_unslash( $user_value ) );
}
} else {
$one_entry['user_value'] = isset( $_POST['prflxtrflds_user_field_value'][ $one_entry['field_id'] ] ) ? sanitize_text_field( wp_unslash( $_POST['prflxtrflds_user_field_value'][ $one_entry['field_id'] ] ) ) : '';
}
} else {
/** Add selected values */
if ( '3' === $one_entry['field_type_id'] ) {
$user_value = $wpdb->get_results(
$wpdb->prepare(
'SELECT `user_value` FROM `' . $wpdb->base_prefix . 'prflxtrflds_user_field_data`
WHERE `user_id`= %d AND `field_id` = %d',
$user_id,
$one_entry['field_id']
),
ARRAY_A
);
if ( ! empty( $user_value ) ) {
foreach ( $user_value as $key_value => $value_single ) {
$one_entry['user_value'][] = $value_single['user_value'];
}
} else {
$one_entry['user_value'] = array();
}
} else {
$one_entry['user_value'] = $wpdb->get_var(
$wpdb->prepare(
'SELECT `user_value` FROM `' . $wpdb->base_prefix . 'prflxtrflds_user_field_data`
WHERE `user_id`= %s AND `field_id` = %s',
$user_id,
$one_entry['field_id']
)
);
}
}
/** Change `editable` and `visible` data for non-current user editing */
$editable_visible = $wpdb->get_row(
$wpdb->prepare(
'SELECT
`' . $wpdb->base_prefix . 'prflxtrflds_roles_and_fields`.`editable`,
`' . $wpdb->base_prefix . 'prflxtrflds_roles_and_fields`.`visible`
FROM
`' . $wpdb->base_prefix . 'prflxtrflds_fields_id`,
`' . $wpdb->base_prefix . 'prflxtrflds_roles_and_fields`,
`' . $wpdb->base_prefix . 'prflxtrflds_roles_id`
WHERE
`' . $wpdb->base_prefix . 'prflxtrflds_roles_and_fields`.`role_id`= `' . $wpdb->base_prefix . 'prflxtrflds_roles_id`.`role_id`
AND `' . $wpdb->base_prefix . 'prflxtrflds_roles_and_fields`.`field_id`=`' . $wpdb->base_prefix . 'prflxtrflds_fields_id`.`field_id`
AND `' . $wpdb->base_prefix . 'prflxtrflds_roles_and_fields`.`field_id`= %d
AND `' . $wpdb->base_prefix . "prflxtrflds_roles_id`.`role` IN ( '" . $user_role . "' )",
$one_entry['field_id']
),
ARRAY_A
);
$one_entry['editable'] = $editable_visible['editable'];
$one_entry['visible'] = $editable_visible['visible'];
if ( '0' === $one_entry['editable'] ) {
$editable_attr = ' readonly="readonly" disabled="disabled"';
$hidden_noneditable_field = '<input type="hidden" name="prflxtrflds_not_editable[]" value="' . $one_entry['field_id'] . '" />';
} else {
$editable_attr = $hidden_noneditable_field = '';
}
$required_attr = ( ! empty( $one_entry['required'] ) && ! empty( $one_entry['editable'] ) ) ? ' required="required"' : '';
if ( '1' === $one_entry['visible'] ) {
?>
<tr>
<th>
<?php
echo esc_html( $one_entry['field_name'] );
if ( ! empty( $one_entry['required'] ) ) {
?>
<span class="description"><?php echo esc_attr( $one_entry['required'] ); ?></span>
<?php if ( '1' === $one_entry['editable'] ) { ?>
<input type="hidden"
name="prflxtrflds_required[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="true"/>
<?php
}
}
?>
<input type="hidden"
name="prflxtrflds_field_name[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $one_entry['field_name'] ); ?>">
</th>
<td>
<?php
switch ( $one_entry['field_type_id'] ) {
case '1':
?>
<input type="text"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( wp_unslash( $one_entry['user_value'] ) );
}
?>"
<?php
if ( isset( $one_entry['available_fields'][0]['value_name'] ) ) {
echo 'maxlength="' . esc_attr( $one_entry['available_fields'][0]['value_name'] ) . '"';
}
echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr );
?>
/>
<?php
break;
case '2':
$unser_textarea = maybe_unserialize( $one_entry['available_fields'][0]['value_name'] );
?>
<textarea
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
rows="<?php echo esc_attr( $unser_textarea['rows'] ); ?>" cols="<?php echo esc_attr( $unser_textarea['cols'] ); ?>" maxlength="<?php echo esc_attr( $unser_textarea['max_length'] ); ?>"
<?php echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr ); ?>><?php echo esc_attr( wp_unslash( $one_entry['user_value'] ) ); ?></textarea>
<?php
break;
case '3':
foreach ( $one_entry['available_fields'] as $one_sub_entry ) {
$checked = ( ! empty( $one_entry['user_value'] ) && in_array( $one_sub_entry['value_id'], $one_entry['user_value'] ) );
?>
<label
<?php
if ( $checked ) {
echo wp_kses_data( 'class="checked"' );
}
?>
>
<input type="checkbox" class="prflxtrflds_input_checkbox"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>][]"
value="<?php echo esc_attr( $one_sub_entry['value_id'] ); ?>"
<?php
if ( $checked ) {
echo esc_attr( ' checked' );
}
echo wp_kses_data( $editable_attr );
?>
/>
<?php echo esc_attr( $one_sub_entry['value_name'] ); ?>
</label>
<br/>
<?php
}
break;
case '4':
foreach ( $one_entry['available_fields'] as $one_sub_entry ) {
?>
<label>
<input type="radio" class="prflxtrflds_input_radio"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $one_sub_entry['value_id'] ); ?>"
<?php
if ( isset( $one_entry['user_value'] ) && $one_sub_entry['value_id'] === $one_entry['user_value'] ) {
echo esc_attr( ' checked' );
}
echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr );
?>
/>
<?php echo esc_attr( $one_sub_entry['value_name'] ); ?>
</label>
<br/>
<?php
}
break;
case '5':
?>
<select
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]" <?php echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr ); ?>>
<option></option>
<?php foreach ( $one_entry['available_fields'] as $one_sub_entry ) { ?>
<option
value="<?php echo esc_attr( $one_sub_entry['value_id'] ); ?>"
<?php
if ( isset( $one_entry['user_value'] ) && $one_sub_entry['value_id'] === $one_entry['user_value'] ) {
echo ' selected';}
?>
><?php echo esc_attr( $one_sub_entry['value_name'] ); ?></option>
<?php } ?>
</select>
<?php
break;
case '6':
?>
<input class="prflxtrflds_datetimepicker" type="text"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( $one_entry['user_value'] );}
?>"
<?php echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr ); ?>>
<?php
if ( isset( $one_entry['available_fields'][0] ) && strripos( $one_entry['available_fields'][0]['value_name'], 'T' ) ) {
echo esc_attr( date_i18n( 'T' ) );
}
?>
<input type="hidden" name="prflxtrflds_date_format"
value="<?php echo esc_attr( trim( str_replace( 'T', '', $one_entry['available_fields'][0]['value_name'] ) ) ); ?>">
<input type="hidden"
name="prflxtrflds_user_field_datetime[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $one_entry['available_fields'][0]['value_name'] ); ?>">
<?php
break;
case '7':
?>
<input class="prflxtrflds_datetimepicker" type="text"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( $one_entry['user_value'] );
}
?>"
<?php echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr ); ?>>
<?php
if ( isset( $one_entry['available_fields'][0] ) && strripos( $one_entry['available_fields'][0]['value_name'], 'T' ) ) {
echo esc_attr( date_i18n( 'T' ) );
}
?>
<input type="hidden" name="prflxtrflds_time_format"
value="<?php echo esc_attr( trim( str_replace( 'T', '', $one_entry['available_fields'][0]['value_name'] ) ) ); ?>">
<input type="hidden"
name="prflxtrflds_user_field_datetime[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $one_entry['available_fields'][0]['value_name'] ); ?>">
<?php
break;
case '8':
$date_and_time = unserialize( $one_entry['available_fields'][0]['value_name'] );
?>
<input class="prflxtrflds_datetimepicker" type="text"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( $one_entry['user_value'] );
}
?>"
<?php echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr ); ?>>
<?php
if ( strripos( $date_and_time['time'], 'T' ) || strripos( $date_and_time['date'], 'T' ) ) {
echo esc_attr( date_i18n( 'T' ) );
}
?>
<input type="hidden" name="prflxtrflds_time_format"
value="<?php echo esc_attr( trim( str_replace( 'T', '', $date_and_time['time'] ) ) ); ?>">
<input type="hidden" name="prflxtrflds_date_format"
value="<?php echo esc_attr( trim( str_replace( 'T', '', $date_and_time['date'] ) ) ); ?>">
<input type="hidden"
name="prflxtrflds_user_field_datetime[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $date_and_time['date'] ) . ' ' . esc_attr( $date_and_time['time'] ); ?>">
<?php
break;
case '9':
?>
<input type="number" class="prflxtrflds_number"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( $one_entry['user_value'] );
}
?>"
<?php
if ( isset( $one_entry['available_fields'][0]['value_name'] ) ) {
echo 'max="' . esc_attr( $one_entry['available_fields'][0]['value_name'] ) . '"';}
echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr );
?>
/>
<?php if ( isset( $one_entry['available_fields'][0]['value_name'] ) ) { ?>
<input type="hidden"
name="prflxtrflds_user_field_max_number[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $one_entry['available_fields'][0]['value_name'] ); ?>">
<?php
}
break;
case '10':
?>
<input type="text" class="prflxtrflds_phone"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( wp_unslash( $one_entry['user_value'] ) );
}
?>" <?php echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr ); ?> >
<input type="hidden"
name="prflxtrflds_user_field_pattern[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php echo esc_attr( $one_entry['available_fields'][0]['value_name'] ); ?>">
<?php
break;
case '11':
?>
<input type="url" class="prflxtrflds_url"
id="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
name="prflxtrflds_user_field_value[<?php echo esc_attr( $one_entry['field_id'] ); ?>]"
value="<?php
if ( isset( $one_entry['user_value'] ) ) {
echo esc_attr( wp_unslash( $one_entry['user_value'] ) );
}
?>"
<?php
if ( isset( $one_entry['available_fields'][0]['value_name'] ) ) {
echo 'maxlength="' . esc_attr( $one_entry['available_fields'][0]['value_name'] ) . '"';}
echo wp_kses_data( $editable_attr ) . wp_kses_data( $required_attr );
?>
/>
<?php
break;
}
echo wp_kses_data( $hidden_noneditable_field );
if ( isset( $one_entry['description'] ) ) {
?>
<p class="description"><?php echo esc_html( $one_entry['description'] ); ?></p>
<?php } ?>
</td>
</tr>
<?php
} else {
$hidden_nonvisible_field .= $hidden_noneditable_field;
}
}
echo '</table>';
echo wp_kses_data( $hidden_nonvisible_field );
$prflxtrflds_front_shortcode = true;
} ?>
<p>
<input type="submit" value="<?php esc_html_e( 'Save Info', 'profile-extra-fields' ); ?>" />
<?php wp_nonce_field( 'prflxtrflds_save_front_info', 'prflxtrflds_front_info' ); ?>
</p>
</form>
</div>
<?php
$prflxtrflds_shortcode_output = ob_get_contents();
ob_end_clean();
if ( ! empty( $prflxtrflds_shortcode_output ) ) {
return $prflxtrflds_shortcode_output;
}
}
}
Code file location:
profile-extra-fields/profile-extra-fields/profile-extra-fields.php
Conclusion
Now that you’ve learned how to embed the Profile Extra Fields by BestWebSoft Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply