Search code examples
phpwordpressadvanced-custom-fields

Delete ACF user profile repeater field row from user account (WordPress)


I am using ACF repeater field for user profiles, which shows like this in the user profile page:

enter image description here

That's cool.

But on the front-end I have a form that I want to use to delete a specific row. My custom form simply lists all the rows with radio buttons, and so if the user selected number 3 and submitted the form then I want to delete the third row (in this example, the row with Catherine Davies would be deleted).

The form works fine in that it submits as expected and returns the value the user selected, but my code to delete the row that was selected doesn't seem to work.

The ACF documentation seems a little vague on the subject. Based on a combination of the ACF doc and this StackOverflow post, I expect this code to work but it does not:

$user_ID        = get_current_user_id();
$field          = 'extra_user_info'; // Name of the repeater field
$row_to_delete  = $_POST["row_to_delete"];

delete_sub_row($field, $row_to_delete, 'user_' . $user_id);

Just to be sure, even if I hardcode the $row_to_delete variable to any number (from 1 to 4) it still does not delete any row.

Just to clarify, I wish to delete an entire row.

Note: I realise I could just embed the ACF form on the front-end, but for reasons I won't go into this is not an option, hence using my own custom form.


Solution

  • There could be another issue but in your example at least, your $user_ID variable has capital letters, while the variable passed to delete_sub_row() is lowercase. PHP variables are case-sensitive.