Failing to save data through CustomPress custom fields.

I have my CustomPress set up and working properly except that I cannot save the data that is entered into the form fields on a webpage. I am pretty sure it is just my code that is wrong.

1. I have created a post_type called 'character'.
2. I have created two custom fields called Name and Location.
3. I have created a single-character.php page.
4. I put the form in the loop. (Can be seen at http://www.swattermatter.com/eric/my-campaign/ )
5. I added the save_character function to the functions.php file. (Can be seen at http://www.swattermatter.com/eric/my-campaign/ )

The fields show up fine and are editable, but they do not save when I click submit. I'm sure the code is the problem. Am I supposed to make a function in the form action? Is there some example I can work from?
Thank you for your help.

  • aecnu
    • WP Unicorn

    Greetings Shawn,

    Thank you for the question and I admit I do not have any idea as to what to advise you since I have not experienced this myself plus you are referring to custom coding which is beyond the scope of the support we provide here but we do try to help if we can.

    With that said, I will see if I can get the lead developer in here with his invaluable insight into this plugin for his advice/advise for us.

    Though this may take a bit longer then a normal ticket, I will try to get him in here asap.

    Cheers, Joe

  • Shawn
    • WPMU DEV Initiate

    Thank you for the reply. I thought I did include that bit. It is in the save_character() function in functions.php

    Is there a working example of this I can look at? I would prefer an example that showed the entire form and the entire function that is used to handle the form.

  • Arnold
    • El Macho WP

    Except your code is trying to call a PHP routine in your form action attribute. Since the form is on the browser and PHP is on the server there's nothing to call.

    Your form needs to call an url on your server that will be responsible for handling the form submit. Typically in php it's the same page that the form is on. Your form tag would look like

    <form action="#" method="post">
    ...
    </form

    All the # means is come back to the current page.

    Then in your page you'll need some PHP to process the post.

    if ( !empty($_POST) ) {
    do your processing from the $_POST global
    }
    `
    If post is empty,, which it will be the first time you load the page, then the processing is skipped. Once you submit the $_POST global will have the posted data and you can process it.

  • Shawn
    • WPMU DEV Initiate

    I set up the form as you suggested and that part works now. I have also included the php script you mentioned and it works as well except that the custom fields are not saved.

    At the very top of single-character.php, I have placed the script you mentioned plus a line to display the custom fields.
    <?php
    if ( !empty($_POST) ) {
    echo ("Form submitted.");
    global $CustomPress_Core;
    $CustomPress_Core->save_custom_fields( $post_id );
    echo do_shortcode('[custom_fields_block]');
    }
    ?>

    1. The line 'Form submitted.' shows up correctly.
    2. The Custompress lines don't seem to do anything.
    3. The do_shortcode call displays the fields as they were, with no changes.

    The same thing happens if I put this code in the Loop. The following link will take you to my code examples and the form in question. If you click submit, the data appears at the very top of the page.
    http://www.swattermatter.com/eric/character/sabina/

  • Shawn
    • WPMU DEV Initiate

    Yeah, I skipped that part. Added it in and it all works. Thank you.

    <form method="post" action="#">
    <?php echo do_shortcode('[custom_fields_input]'); ?>
    <input type="submit" value="Submit">
    <input type="hidden" name="post_id" value="<?php echo $post->ID; ?>">
    </form>

    and

    <?php
    if ( !empty($_POST) ) {
    global $CustomPress_Core;
    $CustomPress_Core->save_custom_fields( $_POST['post_id'] );
    }
    ?>

    Thanks guys.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.