Hi Aaron,
If you need to add options “dynamically” to a dropdown, you can use the built-in filters of the plugin to modify fields.
Is it a profile or registration form field?
Let me know so I can tell you which filter to use.
Both Types,
I have a country dropdown that needs to be populated by a “country” post_type and a “specialisation” dropdown that needs to be populated from a meta_value.
I’m currently using jQuery but would be happier if there was a built in filter, function or hook.
This is my current jquery and php for the country dropdown.
<?php
// query for your post type
$post_type_query = new WP_Query(
array (
'post_type' => 'country',
'posts_per_page' => -1 ,
'orderby' => 'title',
'order' => 'ASC',
)
);
// we need the array of posts
$posts_array = $post_type_query->posts;
// create a list with needed information
// the key equals the ID, the value is the post_title
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'post_name' );
//echo '<pre>' . var_export(json_encode($post_title_array), true) . '</pre>';
?>
<script>
(function($) {
var selectValues = <?php echo json_encode($post_title_array); ?>;
var selectMenu = $('#country-select');
$.each(selectValues, function(key, value) {
selectMenu
.append($('<option>', { value : key })
.text(value));
});
})(jQuery);
</script>
Hi Aaron,
Yes there’s filters, so you don’t have to use jquery.
The following snippet is for the registration form, you can add the snippet into your theme’s functions.php file.
function wpum_modify_registration_field( $fields ) {
$fields['my_field_id_here']['options'] = [
'opt_val' => 'option label',
];
return $fields;
}
add_filter( 'wpum_get_registration_fields', 'wpum_modify_registration_field', 20 );
You need to replace my_field_id_here with the id of the field you wish to customize. The easiest way to find out the id is by printing out the $fields variable. You’ll see the associative array that is loaded into the form. Here’s a screenshot so you can see what the id is https://www.dropbox.com/s/1tmay85ju6n7oqj/Schermata%202018-10-12%20alle%2010.44.49.png?dl=0
Once you’ve found your id, you can load your custom options within the array.
For the account form, it looks like I forgot to add a filter there, so I’ve added a filter just now so you need to manually update a core file of the plugin ( I’ll release an official update at a later point ) – this is the file https://github.com/WPUserManager/wp-user-manager/blob/master/includes/wpum-forms/class-wpum-form-profile.php#L198
Once you’ve replaced the file, you can use the exact same code from the snippet above and in this case, use the filter ‘wpum_get_account_fields’.
Hope this helps 🙂
That’s great, thank you Alessandro.