1

In my Wordpress.com site I have a form where users can add relationships of different types (coworkers, friends, etc.) and each relationship type has different fields but similar functionality. I had originally created a parent class for a relationship and then child classes for each type of relationship. I have an add_action function that triggers off of the form submissions. In the add_action function, I take the form inputs and create a new relationship object and then save all of the objects to the user meta data.

However, I'm having a problem retrieving the class later from the user data and having it behave like the class. I also noticed that the __destruct method is always being called at the end of the add_action.

I'm not very good with Wordpress or PHP so I might be way off here. Is this method of instantiating a class through an add_action, saving it into user_meta data, and then retrieving it the proper way to approach this problem in Wordpress?

class relationship {
    private $person_id;
    private $phone_number;
    private $name;
    public function __construct($phone_number, $person_name) {
        $this->person_id = uniqid($person_name.'_');
    }
}
class coworker extends relationship {
    private $superior;
    public function __construct($superior, $phone_number, $person_name) {
        parent::__construct($phone_number, $person_name);
        $this->superior= $superior;
    }
}

function form_custom_code($form_entries, $user_id) {
    $coworkers = [];
    $num_coworkers = form_entries['Number of Coworkers'];
    for ($c_index = 0; $c_index  <= num_coworkers ; $c_index ++) {
        $superior = form_entries['coworker'.c_index]['superior'];
        $phone_number = form_entries['coworker'.c_index]['phone_number '];
        $person_name = form_entries['coworker'.c_index]['person_name '];

        array_push($coworkers, new coworker($superior, $phone_number, 
        $person_name);

    }
    update_user_meta($user_id, 'coworkers', $coworkers);
}

add_action( 'form_submit_success', 'form_custom_code');

Browse other questions tagged or ask your own question.