• I have the following function in my functions.php file:-

    function apply_for_job() {
    
        $job_id = $_GET['job_id'];
    
        $user_id = get_current_user_id();
        $prev_value = get_field('applied_jobs');
    
        update_user_meta($user_id, 'applied_jobs', $job_id);
    
    }
    add_filter('init' ,'apply_for_job');

    When I load: https://distinct.dev/apply_for_job/?job_id=323

    This updates ‘applied_jobs’ field to: 323 as expected.

    However, when I go on my user profile page it sets the value to ‘NULL’

    Any ideas why?

Viewing 1 replies (of 1 total)
  • naysilva,

    My guess is that this is happening because the apply_for_job function is running on every page and, unless set in the query string, the value for $_GET[‘job_id’] is not not set (null). So, when going to the user profile page $_GET[‘job_id’] is null and the user meta data is updated accordingly. I think you will want to make sure the GET[‘job_id’] is not empty:

    if (empty($job_id)) :
    update_user_meta($user_id, 'applied_jobs', $job_id);
    endif;

    Since this variable is being passed in the query string which someone with bad intentions could modify, you might want to consider sanitizing the variable. Maybe something like:

    $job_id = intval($_GET['job_id']);

    This tells PHP to cast the value as a number to help prevent people putting in things like text attempting to inject SQL or other things.

    I hope this helps!
    Scott

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress – Update User Profile function’ is closed to new replies.