• Hi All,

    WordPress beginner here. Apologies for what I’m sure is a noob question but I’ve been spending days on this and still haven’t figured out a way to do this.

    Basically I have created some custom user fields using the ‘Advanced Custom Fields’ plugin and I want to create a simple form on a static page where a user can edit them.

    To start simple I have a custom field called ‘isuseractive’ where values can be either True or False. I just want say a simple checkbox on my page and then a ‘Submit’ button which will then set ‘isuseractive’ to True/False (for that user) depending on the checkbox selection.

    I’m sure there must be an easy way to do this but I haven’t been able to figure it out and can’t find any plugins that do it either.

    Note I did take a look at WPForms but I’m not willing to pay for the pro version to do what I consider is a very easy task. I am almost there myself, I have written a function that updates user metadata in functions.php but I just can’t figure out how to create a form that will pass user arguments to the function from the front end.

    Please can you help?

    Thanks,
    James

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @jamesfuorore ,

    Have a look at the shortcodes. You can create your own shortcode. See the example below:

    
    function updateCustomFieldCallback(){
        ob_start();
        //your code starts here to create the HTML form for the field
    
        /**
        * Here you can create an HTML form for adding the checkbox and submit it. 
        * On submit of the form, you can call your function to update user meta. 
        */ 
    
        //your code ends here
    
        $buffer = ob_get_contents();
        ob_end_clean(); 
    }
    add_shortcode('update_user_custom_field', 'updateCustomFieldCallback');
    
    Thread Starter jamesfuorore

    (@jamesfuorore)

    Thanks @ketanvyawahare I will give this a go!

    Thread Starter jamesfuorore

    (@jamesfuorore)

    @ketanvyawahare sorry this may be another noob question but presumably this function goes in the functions.php file but then shouldn’t the code for the form be in php not HTML?

    If I start my form with e.g. <html> I get a syntax error saying unexpected ‘<‘

    Thread Starter jamesfuorore

    (@jamesfuorore)

    I have figured out how to include HTML code now (by enclosing within ?> and <?php tags) however my next problem is that the “ob_start()” code is suppressing the form when I run the page.

    I’ll keep investigating and researching…

    Thread Starter jamesfuorore

    (@jamesfuorore)

    Update: managed to show the form on the page by ensuring there is a return $buffer at the end of the updateCustomFieldCallback function

    Hi @jamesfuorore ,

    That’s great you managed to show the form.

    Is everything sorted now?

    Thread Starter jamesfuorore

    (@jamesfuorore)

    @ketanvyawahare yes I think I’m good for now, thanks for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Can I create a simple form that updates custom user fields?’ is closed to new replies.