WordPress.org

Ready to get started?Download WordPress

Forums

Gravity Forms + Custom Post Types
[resolved] Custom user taxonomies (cont.) (4 posts)

  1. derol
    Member
    Posted 8 months ago #

    I'm using this plugin with user registration, to sign up users and put them into categories (taxonomies)... I've found just about all the solutions on how to do this, up to this point:

    http://wordpress.org/support/topic/plugin-gravity-forms-custom-post-types-custom-user-taxonomies

    This post was closed... but I'd like to revisit it. It's my final solution if I can get it to work properly.

    I'm having issues with not only getting it to work the way it is, but can anyone suggest modifications, that would allow it to attach a user to multiple taxonomies?

    Also, the echo statement in the code: where would that echo too? As I'd like to see the output.

    // Hook Gravity Forms user registration -> Map taxomomy
    
    	function map_taxonomy($user_id, $config, $entry, $user_pass) {
    
    		global $wpdb;
    
    	// Get all taxonomies
    		$taxs = get_taxonomies();
    
    	// Get all user meta
    		$all_meta_for_user = get_user_meta($user_id);
    
    	// Loop through meta data and map to taxonomies with same name as user meta key
    		foreach ($all_meta_for_user as $taxonomy => $value ) {
    
    			if (in_array ($taxonomy, $taxs) ) {			// Check if there is a Taxonomy with the same name as the Custom user meta key
    
    			// Get term id
    				$term_id = get_user_meta($user_id, $taxonomy, true);
    				If (is_numeric($term_id)) {				// Check if Custom user meta is an ID
    
    					Echo $taxonomy.'='.$term_id.'<br>';
    
    				// Add user to taxomomy term
    					$term = get_term( $term_id, $taxonomy );
    					$termslug = $term->slug;
    					wp_set_object_terms( $user_id, array( $termslug ), $taxonomy, false);
    
    				}
    			}
    		}
    
    	}
    	add_action("gform_user_registered", "map_taxonomy", 10, 4);

    http://wordpress.org/plugins/gravity-forms-custom-post-types/

  2. cquinndesign
    Member
    Posted 8 months ago #

    Here is the code you should use:

    function map_taxonomy($user_id, $config, $entry, $user_pass) {
    
    		global $wpdb;
    
    	// Get all taxonomies
    		$taxs = get_taxonomies();
    
    	// Get all user meta
    		$all_meta_for_user = get_user_meta($user_id);
    
    	// Loop through meta data and map to taxonomies with same name as user meta key
    		foreach ($all_meta_for_user as $taxonomy => $value ) {
    
    			if (in_array ($taxonomy, $taxs) ) {			// Check if there is a Taxonomy with the same name as the Custom user meta key
    
    			// Get term id
    				$term_id = get_user_meta($user_id, $taxonomy, true);
    				If (is_numeric($term_id)) {				// Check if Custom user meta is an ID
    
    $taxonomy.'='.$term_id.'<br>';
    
    				// Add user to taxomomy term
    					$term = get_term( $term_id, $taxonomy );
    					$termslug = $term->slug;
    					wp_set_object_terms( $user_id, array( $termslug ), $taxonomy, false);
    
    				}
    			}
    		}
    
    	}
    	add_action("gform_user_registered", "map_taxonomy", 10, 4);
  3. derol
    Member
    Posted 8 months ago #

    Uh, you didn't change anything, just copied and pasted?

    However, for anyone else trying to accomplish this - I've figured out the right answer:

    // Hook Gravity Forms user registration -> Map taxomomy
    
    function map_taxonomy($user_id, $config, $entry, $user_pass) {
    
        global $wpdb;
    
    // Get terms from taxonomy anniversary
        $terms = get_terms( 'anniversary', array('hide_empty' => 0));
        $termslugs = array();
        foreach( $terms as $term ) {
            $i = $term->slug;
            $termslugs[$i] = $term->slug;
        }
        $cats = $termslugs;
    
    // Get all user meta
        $all_meta_for_user = get_user_meta($user_id);
    
    // Loop through meta data and map to categories with same name as user meta key
        foreach ($all_meta_for_user as $category => $value ) {
    
            if (in_array ($category, $cats) ) {         // Check if there is a category with the same name as the Custom user meta key
    
            // Get term id
                $term_id = get_user_meta($user_id, $category, true);
                If (is_numeric($term_id)) {             // Check if Custom user meta is an ID
    
                    echo '<p>'.$user_id.' | '.$category.'='.$term_id.'</p>';
    
                // Add user to taxomomy term
                    $taxonomy = 'anniversary';
                    $term = get_term( $term_id, $taxonomy );
                    $termslug = $term->slug;
                    wp_set_object_terms( $user_id, array( $termslug ), $taxonomy, true);
    
                }
            }
        }
    
    }
    add_action("gform_user_registered", "map_taxonomy", 10, 4);

    The only downside, is you have to call your registered taxonomy by name (in this case "anniversary") I'm sure someone enterprising enough can find a way to make this more dynamic... but it works.

  4. derol
    Member
    Posted 8 months ago #

    Quick edit: Take out the "echo" line...

Reply

You must log in to post.

About this Plugin

About this Topic