Support » Developing with WordPress » contactmethods throws “undefined index”

  • Resolved eliant

    (@eliant)


    I created some usercontact fields with add_filter( ‘user_contactmethods’, myfunction).

    In a shortcode function, I access them like this:
    $meta = get_user_meta($ID);
    /* The following line throws the “Notice: Undefined Index” message. */
    $example_var = $meta[‘my-contactmethod’][0];

    If the contact methods were declared, by pop the php Notice?

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

    (@bcworkz)

    Which index is undefined? Probably ‘my-contactmethod’. Meaning there is no such meta record for the user $ID. Are you sure you have the correct $ID value for the user that has a contact method defined? Even if you’re sure, I’d advise double checking that.

    It’s always a good idea to verify data exists before trying to use it, even if you fully expect it’ll be there. Also, IMO it’s usually not a good idea to get all user meta. There’s often a LOT of data involved. Better in most cases is to only get the data you actually need, as in get_user_meta('my-contactmethod', $ID, true ); Still verify that the return is not empty before trying to use it.

    Thread Starter eliant

    (@eliant)

    Yes, the “bad” index is my-contactmethod.

    This is a bit of pseudo-code showing which “ADD_ACTION” and “ADD_FILTER” I have used.

    add_action('admin_menu')		
    	add_menu_page	
    	add_submenu_page	
    		
    add_action('admin_init')		
    	register_settings(various)	
    		
    add_action('init');  // Fires after WordPress has finished loading but before any headers are sent.  Fires after admin_menu and admin_init		
    	register_taxonomy	
    		
    Various other actions and filters.  Some firing sequences are listed.  Others, not sure of firing sequences		
    	add_filter('login_redirect')	
    	add_action('register_form');  // Fires following the 'Email' field in the user registration form	
    	add_filter('registration_errors');  //This fires when the form is submitted but before user information is saved to the database.	
    	add_action('user_register');  //Fires immediately after a new user is registered.	
    	add_action('personal_options_update');  // Set user_contactmethods based on $_POST	
    	add_action('edit_user_profile_update');   // Ditto	
    	add_action('user_profile_update_errors')	
    	add_shortcode(various);  THIS IS WHERE PHP Notification MESSAGE IS GENERATED	
    		$meta = get_user_meta ( $ID );
    		if($meta['MYcontactmethod'][0] == 'true') ACTION STATEMENTS;
    	add_filter('user_contactmethods')	
    

    $meta is set thus:
    $meta = get_user_meta ( $ID );

    Since I’m not clear on all of the firing sequences, I guess the real question is this: does
    add_filter(‘user_contactmethods’) fire before
    add_shortcode ?

    And in my shortcode function do I have to declare those user_contactmethods as GLOBAL, or declare the index name ‘my-contactmethod’ used here: $meta[‘my-contactmethod’][0] ?

    • This reply was modified 1 year, 3 months ago by eliant.
    Thread Starter eliant

    (@eliant)

    Sure, I can call the seven contact_methods individually, but I’d still like to see the initial question resolved…

    If they were set previously (and I’ve queried the DB – they are set for all users), why the PHP Notification message?

    • This reply was modified 1 year, 3 months ago by eliant.
    • This reply was modified 1 year, 3 months ago by eliant.
    Moderator bcworkz

    (@bcworkz)

    I don’t think the order of add_*() functions matters in this particular case as long as both are in place when the shortcode is actually invoked.

    You only declare variables global when necessary. Static indices like [‘my-contactmethod’] aren’t variable and could be thought of as super global, though that’s really a mis-application of the term. Actual super globals are those like $_GET which are variables that are always in scope and don’t need to be declared as global.

    Could the notice be from non-logged in users? Then $ID would be invalid. Or it’s invalid for all users because it wasn’t properly assigned.

    Thread Starter eliant

    (@eliant)

    Thanks for being my sounding board; you’re making me investigate things I didn’t think needed investigating.

    Turns out, simply declaring the contactmethods does not create those fields in the database. They get created only when a value is set for a particular user. My shortcode is looping through ALL users and interrogating the value of the contactmethod field. But a couple users never returned to the website after registering – they never clicked the link in their confirmation email, so their contactmethod was not set even though they have a userID in the DB.

    It’s up to me to account for this in my code. So thanks for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘contactmethods throws “undefined index”’ is closed to new replies.