• Resolved Artus

    (@tintalent)


    I have a custom field in the user profile where they can select country. Is is possible to display a flag of the selected country in a users list?

    I have the images of all countries already.

    Any help would be appreciated. Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    What users list are you referring to? You could certainly custom code a template tag or shortcode to output a user list with flags. You mainly need a way for your code to equate the country selection to the flag image file. If how you store the country (2 char. code, 3 char. code, full name, etc.) is identical to a portion of the flag filename, the task is just a string concatenation exercise.

    If you’re referring to the users list table admin screen in the back end, it’s possible to add custom columns to display custom data or images.

    Thread Starter Artus

    (@tintalent)

    Thanks for you replying. Im using Simple User listing pluging that outputs the list of all users in a frontend page.
    I created a custom field in the users profile to get the countries: ur_country
    I can display the chosen countries and other info like this:

    <p><?php echo $user_info->description; ?></p>
       <p><?php echo $user_info->ur_country; ?></p>

    What I want is to display a flag next to the country name.

    Am i in the right direccion? I used this funtion next to: <?php echo $user_info->ur_country; ?> with no success.

    function country_flags()
    {
    	global $wpdb;
        $table = $wpdb->prefix."usermeta";
    	$cflag = $wpdb->get_row("SELECT meta_key FROM $table");
    	$mm=get_bloginfo('url');
    	if ( !$cflag )return "";
        else return '<img src="'.$mm.'/wp-content/images/flags/'.strtolower($cflag->meta_value).'.png" />';
    }
    Moderator bcworkz

    (@bcworkz)

    Yes, you have the right idea. There could easily be some small error that stymies your efforts. You can eliminate some potential pitfalls by using get_user_meta(). It essentially does the same query you’ve done, but you don’t have to worry about getting all the syntax just right. I couldn’t say for sure without some research, but there’s a few things in your query that don’t look quite right. Using the function eliminates any doubt.

    Besides that, it’s basic debugging to determine where something went awry. print_r() every variable along the way to verify it contains the value you expect it to, and/or to verify that conditional statements are being executed when they should. When variable or condition is found that’s wrong, determine why and fix it. One bug squashed. The next run will reveal a new bug or bring success.

    Thread Starter Artus

    (@tintalent)

    Yes get_user_meta() works ok. The problem here is that the names of the countries should match the name of the picture and it gets messy with South_Africa.jpg I wouldt put that in the dropdown list.

    Another option is to store the country names with iso codes.

    Table
    id iso
    Country 1 ct1

    But then how do i get a flag to display if country=ct1 is selected?

    Im not a good coder unfurtunately
    Help please.

    Moderator bcworkz

    (@bcworkz)

    With HTML drop downs the value returned is different than the string displayed to the user. You can display a human friendly string to the user and the form will send a machine friendly value to the server. The South Africa item in the list may look like this:
    <option value="ZA"><?php _e('South Africa', 'my_theme')?></option>

    The translate bit “_e()” is optional, but leaves the option open for the labels to be translated to the users language, perhaps “Zuid Afrika”. The form will send “ZA” as the value in $_POST['select_name'] (where select_name is the actual name you assign to the select element) Then the flag images could have the form “flag-ZA.png”, thus, after the ‘ZA’ is stored in user meta, the user list display code might have this: <img src="flag-<?php echo get_user_meta( $id, 'user_country', true ); ?>.png" />

    You don’t have to use the 2 char country code, you could use “south-africa” if you wanted to. The point is the user doesn’t see what you are using to distinguish countries, they see what ever label you think would make the most sense.

    You are essentially using the <option> structure as a sort of lookup table. You could actually use a lookup table if you needed to. Let’s say the dropdown is from a plugin where altering the values is not possible and you have a set of images from a resource where you cannot change the existing names. The value returned by the dropdown’s form might be “south-africa” and the associated image is flag-ZA.png.

    You can create a lookup table using an associative array structure, the table with only the one South Africa entry would look like this:
    $country = array('south-africa'=>'flag-ZA.png',);

    You can then either store ‘south-africa’ in user meta, doing the translation later, or do the translation on save and store ‘flag-ZA.png’. In the first case the user list code would then be like this: <img src="<?php echo $country[ get_user_meta( $id, 'user_country', true )]; ?>" />

    I hope you can see now that your options are not restricted by what code allows. You can do whatever makes sense to you as a human, then create code to make that work. There may be more efficiency in one method over the other, but unless you’re dealing with hundreds of thousands of users, it really wouldn’t make much difference. There’s something to be said for code that makes sense to us humans, even if there is a better, though more obscure and hard to follow method.

    Thread Starter Artus

    (@tintalent)

    Great! Thanks you. That explanation took some time to write. I will definitely try it out and report back.

    Thread Starter Artus

    (@tintalent)

    I was able to create the select dropdown list usign the plugin ACF and manage to output a css class with the same name as the value selected.

    '<span class="'.$user_id->country'">'.'<br>'.$value.'</span>'
    
    .country{background-image: url('image');}

    Thanks for your ideas!

    Moderator bcworkz

    (@bcworkz)

    You’re most welcome! I’m glad I was able to help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Custom field (dropdown) how to add country flag?’ is closed to new replies.