1

I want to create and set a custom taxonomy site-wide based on the users location so that I can call taxonomy_exists( $country ) to determine if a user is located in a specific country.

I've installed the GeoIP plugin that lets me get the user's country code from $userInfo->country->isoCode.

To register the custom taxonomy, I have the following function. But what should 'object type' be if I want to apply the taxonomy to the entire site?

add_action( 'init', 'create_country_taxonomy' );

function create_country_taxonomy() {
    register_taxonomy(
        'country',
        $object_type, // Set to what?
        array(
            'label' => __( 'Country' ),
            'rewrite' => array( 'slug' => 'location' ),
        )
    );
}

Finally, how can I set the user's country taxonomy to $userInfo->country->isoCode?

0

I got it to work by registering a custom taxonomy...

add_action( 'init', 'create_country_tax' );
function create_country_tax() {
    $userInfo = geoip_detect2_get_info_from_current_ip();
    $country = $userInfo->country->isoCode;
    register_taxonomy($country,'country');
}

and then calling taxonomy_exists('US').

  • That seems the wrong way, in my opinion. See my answer for an expalnation of my point of view. – cybmeta Feb 5 '16 at 8:15
0

It seems to me that you are doing it wrong. You don't need a set of taxonomies but one taxonomy with a set of terms.

For example, if a user is located in US, US should be a term that belongs to the country taxonomy, not a taxonomy itself.

In the code of your answer you are registering one taxonomy for each country, you will end up with a lot taxonomies when you really need only one taxonomy with several terms. Also, in that code you are not assigning any user to any taxonomy, because you are not creating terms, you are creating taxonomies, and objects (users in this case) can be assigned to terms, but not to taxonomies.

A simple example: a post can belongs to "Category A" (a term of Categories taxonomy) but not to "Categories taxonomy". I'm not sure if I'm able to explain correctly the concept, I hope you understand what I mean.

Also, if you want to classify users using the custom taxonomies, the object_type parameter should be user, not country which is, seeing your code, a unknown object type.

So, first, we register the taxonomy for users:

add_action( 'init', 'create_country_taxonomy' );
function create_country_taxonomy() {
    register_taxonomy(
        // name of the taxonomy
        'country',
        // Object type that can be classified using the taxonomy
        'user',
        array(
            'label' => __( 'Country' ),
            'rewrite' => array( 'slug' => 'location' ),
        )
    );
}

Now, when a user is registered or updated, we can create a term in the country taxonomy, and assing it to the user, as follow:

add_action( 'user_register', 'my_profile_update' );
add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {
    $userInfo = geoip_detect2_get_info_from_current_ip();
    $country_code = $userInfo->country->isoCode;
    // Syntax (see https://codex.wordpress.org/Function_Reference/wp_set_object_terms)
    // wp_set_object_terms( $object_id, $terms, $taxonomy, $append );
    wp_set_object_terms( $user_id, $country_code, 'country' );
}

Then, you can check if the user belongs to some country with the function has_term(). For example, you can check if current user is located in US:

if( has_term( 'US', 'conuntry', get_current_user_id() ) ) {
    // Current user belongs to US
}

We could update previous code to vaoid wp_set_object_terms() if the user already has assigned the country:

add_action( 'user_register', 'my_profile_update' );
add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {
    $userInfo = geoip_detect2_get_info_from_current_ip();
    $country_code = $userInfo->country->isoCode;

   if( ! has_term( $country_code, 'conuntry', $user_id ) ) {
       wp_set_object_terms( $user_id, $country_code, 'country' );
   }

}

You can also use get_objects_in_term() to list all users from US (WP_User_Query seems to not support taxonomies?):

// You need to know the term_id of US term,
// I use 3 here as example
$user_in_US = get_objects_in_term( 3, 'country' );
  • I had a hunch there was a better way. I'll try this out and report back. – j8d Feb 5 '16 at 13:59
  • Rather than update a user's profile with persistent information, I only need info about the current session, specifically, the present location of a visitor. Even though I'm creating a new taxonomy based on the visitors county code, the taxonomy is erased as soon as the visitor closes the page. At least that's how I understood it. – j8d Feb 5 '16 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.