Skip to content
Advertisement

Add result of php function to WP database

I have the following function to create a member_id:

JavaScript

It appears to work fine and I can call the function from a shortcode and get the correct value.

Now I want to add the returned value to a database and have tried using

  • update_user_meta()
  • $wpdb->update
  • $wpdb->insert
  • $wpdb->query all with necessary arguments but nothing seems to work.

I expected this to do it, but it just generates a generic error that I am not able to debug (I have a problem with error logging):

JavaScript

However if I change generate_member_id() to a string like 'memberId', it works and the database is updated. So I don’t know what I’m doing wrong. Why can’t I add’ the result of the generate_member_id() function and to a WP database?

EDIT

JavaScript

Advertisement

Answer

There are a few things I notice that could cause problems:

  1. You have this line that (1) has no ; at the end and (2) it doesn’t look like you need it anyway – you don’t use $generated_id anywhere:
JavaScript
  1. You are not passing the user_id into generate_member_id for your users in the loop calling update_user_meta

  2. … but even if you did pass in the user_id, generate_member_id isn’t using it anyway – it is using the current user id. That is the wrong value for the users in your loop.

You need to fix your generate_member_id function to work with the user_id parameter. I assume you want this function to work without one too, so it still gets the get_current_user_id is no user id is passed in:

JavaScript

And then remove the erroneous $generated_id... line and pass the user_id to the generate_member_id in your update_user_meta:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement