Support

Account

Home Forums Front-end Issues problems with post edit/add

Solving

problems with post edit/add

  • Hi I have read articles
    1)http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/
    2)http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

    From what i understood there is provision for creating an HTML form to edit and add posts enhanced with field groups that match rules you have created at the custom fields GUI.

    However I did not achive to get it to work.The only thing that shows is the submit button.I will now enumerate steps taken.please verify – add missing actions.

    1.I made two new php empty template files.I copied <?php code in both one for create from the first article &one for editing giving different template names
    2)I added ‘acf/pre_save_post’ and ‘my_deregister_styles()’ to functions.php of the theme including the add_action and add_filter (which I do no yet know their functionality)
    3)I created two new pages selecting templates and
    …nothing happened
    I freaked out and started trying adding titles of fieldgroups in quotes in an array to both pages
    changed post_ID to some given post for edit,still no results
    copied $options and $args to be set within the new pages,still nothing

    Can you help?Am i missing a step or any code needed(ie some other acf plugin?).Is there another route for creating front-end (ie a php submit snippet)? Please contribute…I have limited time to research wordpress development.

  • i just understood we can’t escape definging fields in html/php. pity .including all defined fields for rules in effect would be a nice shortcode for acf_form to implement

    if anyone knows the answer can he help modifying
    http://wordpress.org/support/topic/frontend-post-submission-form
    for custom fields

  • Hi @savvaskef

    Perhaps you could post the code for your template so I can see why the form is not displaying correctly.

    Thanks
    E

  • I did not want to dump the forum with loads of code but since eliot suggested I do as instruced
    So here goes…

    In theme’s functions.php I added (for the next two solutions(links included))

    function my_pre_save_post( $post_id )
    {
    // check if this is to be a new post
    if( $post_id != ‘new’ )
    {
    return $post_id;
    }

    // Create a new post
    $post = array(
    ‘post_status’ => ‘draft’ ,
    ‘post_title’ => ‘A title, maybe a $_POST variable’ ,
    ‘post_type’ => ‘post’ ,
    );

    // insert the post
    $post_id = wp_insert_post( $post );

    // update $_POST[‘return’]
    $_POST[‘return’] = add_query_arg( array(‘post_id’ => $post_id), $_POST[‘return’] );

    // return the new ID
    return $post_id;
    }

    add_filter(‘acf/pre_save_post’ , ‘my_pre_save_post’ );

    function my_deregister_styles() {
    wp_deregister_style( ‘wp-admin’ );
    }

    add_action( ‘wp_print_styles’, ‘my_deregister_styles’, 100 );
    ********************************************************
    pages using the two templates have the following extra <phpcode> code

    <phpcode><?php
    $args = array(‘post_id’ => ‘new’,’field_groups’ => array(‘Συνδιασμοί αναρτήσεων’,’Τύπος αναρτήσεων’ ));
    ?></phpcode>

    **** and

    <phpcode><?php
    $options = array(
    ‘post_id’ => 213, // post id to get field groups from and save data to
    ‘field_groups’ => array(‘Συνδιασμοί αναρτήσεων’,’Τύπος αναρτήσεων’ ), // this will find the field groups for this post (post ID’s of the acf post objects)
    ‘form’ => true, // set this to false to prevent the <form> tag from being created
    ‘form_attributes’ => array( // attributes will be added to the form element
    ‘id’ => ‘post’,
    ‘class’ => ”,
    ‘action’ => ”,
    ‘method’ => ‘post’,
    ),
    ‘return’ => add_query_arg( ‘updated’, ‘true’, get_permalink() ), // return url
    ‘html_before_fields’ => ”, // html inside form before fields
    ‘html_after_fields’ => ”, // html inside form after fields
    ‘submit_value’ => ‘Update’, // value for submit field
    ‘updated_message’ => ‘Post updated.’, // default updated message. Can be false to show no message
    );

    ?></phpcode>
    ********************************************************
    i am not sure I modified the $args and $options correctly but I did a couple
    of attempts.I also don’t get the concept of how can we get fields from post(How’s that when you create a new post OR how will it edit a post not defined in code (ie 213) which took the place of $post->ID (although i figured that this can be implied if a wp_query is ran having effect on global(?) variables .(please explain)
    ********************************************************

    Then I created a couple of php templates in theme editor

    in themes form_create.php I added
    (found on http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/)
    ********************************************************
    <?php

    /**
    * Template Name: Page with ACF form
    */

    acf_form_head();

    get_header();

    the_post();

    ?>

    <div id=”primary”>
    <div id=”content” role=”main”>

    <?php

    $args = array(
    ‘post_id’ => ‘new’,
    ‘field_groups’ => array(‘Συνδιασμοί αναρτήσεων’,’Τύπος αναρτήσεων’ )
    );

    acf_form( $args );

    ?>

    </div><!– #content –>
    </div><!– #primary –>

    in themes form_edit.php I added
    (found on http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/)
    ********************************************************

    <?php

    /**
    * Template Name: Form Page
    */

    acf_form_head();

    get_header(); ?>

    <div id=”primary”>
    <div id=”content” role=”main”>

    <?php the_post(); ?>

    <?php acf_form( $options ); ?>

    </div><!– #content –>
    </div><!– #primary –>

    Finally in a page I tried a php code -for posting(that worked with ordinary post fields but not custom fields)I’dlike a consistent solution.
    ********************************************************
    <?php
    /*
    Template Name: Post publish form
    */

    ?>
    <?php
    if( ‘POST’ == $_SERVER[‘REQUEST_METHOD’] && !empty( $_POST[‘action’] ) && $_POST[‘action’] == “new_post”) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST[‘title’])) {
    $title = $_POST[‘title’];
    } else {
    echo ‘Προσθέστε Τίτλο πριν την ανάρτηση’;
    }
    if (isset ($_POST[‘description’])) {
    $description = $_POST[‘description’];
    } else {
    echo ‘Προσθέστε Περιεχόμενο της ανάρτησης’;
    }

    $tags = $_POST[‘post_tags’];

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    ‘post_title’ => $title,
    ‘post_content’ => $description,
    ‘post_category’ => array($_POST[‘cat’]), // Usable for custom taxonomies too
    ‘tags_input’ => array($tags),
    ‘post_status’ => ‘publish’, // Choose: publish, preview, future, draft, etc.
    ‘post_type’ => ‘post’, //’post’,page’ or use a custom post type if you want to
    ‘_product’=>$fproduct
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);

    //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    wp_set_post_tags($pid, $_POST[‘post_tags’]);

    //REDIRECT TO THE NEW POST ON SAVE
    //wp_redirect( ‘/publikation/’ );

    //ADD OUR CUSTOM FIELDS
    add_post_meta($fproduct, ‘_product’, true);

    //INSERT OUR MEDIA ATTACHMENTS
    if ($_FILES) {
    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);
    // $newupload returns the attachment id of the file that
    // was just uploaded. Do whatever you want with that now.
    }

    } // END THE IF STATEMENT FOR FILES

    } // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

    //POST THE POST YO
    do_action(‘wp_insert_post’, ‘wp_insert_post’);

    ?>
    <div id=”content” role=”main”>

    <div class=”form-content”>

    <!– ΦΟΡΜΑ ΑΝΑΡΤΗΣΗΣ ΠΡΟΒΛΗΜΑΤΑ –>

    <div class=”wpcf7″>
    <form id=”new_post” name=”new_post” method=”post” action=”” class=”wpcf7-form” enctype=”multipart/form-data”>
    <!– post name –>
    <fieldset name=”name”>
    <label for=”title”>Τίτλος</label>
    <input type=”text” id=”title” value=”” tabindex=”5″ name=”title” />
    </fieldset>

    <!– post Category –>
    <fieldset class=”category”>
    <label for=”cat”>Κατηγορία</label>
    <?php wp_dropdown_categories( ‘tab_index=10&taxonomy=category&hide_empty=0’ ); ?>
    </fieldset>

    <!– post Content –>
    <fieldset class=”content”>
    <label for=”description”></label>
    <textarea id=”description” tabindex=”15″ name=”description” cols=”80″ rows=”10″></textarea>
    </fieldset>

    <fieldset class=”_product”>
    <label for=”description”>Προιόν</label>
    <input type=”text” id=”fproduct” value=”” tabindex=”5″ name=”fproduct” />

    </fieldset>
    <!– post tags –>
    <fieldset class=”tags”>
    <label for=”post_tags”>Tags</label>
    <input type=”text” value=”” tabindex=”35″ name=”post_tags” id=”post_tags” />
    </fieldset>

    <fieldset class=”submit”>
    <input type=”submit” value=”Ανάρτησέ το” tabindex=”40″ id=”submit” name=”submit” />
    </fieldset>

    <input type=”hidden” name=”action” value=”new_post” />
    <?php wp_nonce_field( ‘new-post’ ); ?>
    </form>
    </div> <!– END WPCF7 –>

    <!– END OF FORM –>
    </div><!– .entry-content –>
    </div><!– #post-## –>

    **************************************************************************
    that code is working but I want a consistent solution both in terms of security(moderation) and also GUI-integration and getting initialized with ie select values for entry and a datepicker etc .Remember I have not created a custom post-type and altered standard post for different roles.How can I create a form including different fields for different roles/rules?

  • encouragement 2 gurus : That might seem like a load of trouble for a volunteer 2 change. But it isn’t solutions can be given with a couple of lines EITHER wit acf working template OR with some css/widget for php. Just a workaround for post customfields in a WPconsistent way

  • Hi @savvaskef

    Sorry, It is not possible for me to understand all the code you dumped above.
    You say that it works, so I am a bit confused as to how I could help.

    Perhaps you will need to simplify your above comment to highlight specific lines of code which are troubling you.

    Thanks
    E

  • Hi @savvaskef

    Please also wrap all code in the ‘code’ button.

    Thanks
    E

  • hi,E you seem ver helpfull and the plugin is good . congrats.
    Now here is the problem I face.The code is as is found on the links.only minor modifications are performed
    First link/piece of code:create post form is the title of the tut. How do I need to modify it to actually visualize all fields in a contact form based on rules.

    Link 2: same problem;only the submit button appears.(the code is meant to edit a post and ‘get fields from post’.How’s that; i modified post ; I did not added a custom post type.So will a contributor and admin and guest be detected and the correct fields be presented?

    Link3: I hope it is better integration to use your tutorials found on links.But if not or if you can allocate resources to help me please answer how will the presented fields be selected, or if that would be(maybe it is) too much just inform me of a quick-fix to get values to the custom fields and i will re-produce a select control myself

  • Hi @savvaskef

    I think the main issue with your code is this:
    acf_form( $options );

    What is $options? here is no code which defines the $options array for this function. Perhaps you could re read the documentation to understand how to use this function?

    Thanks
    E

  • Hi, E.
    Let’s narrow down the focus since i posted code for add/edit in paralllel. First in theme’s functions, then in the pages, last in the templates.

    Now let’s focus on edit(uses $options while create uses $args).Specifically, the code used(included in a messy way in my first post) is:

    <?php
    $options = array(
        'post_id' => 213, // post id to get field groups from and save data to
        'field_groups' => array('Συνδιασμοί αναρτήσεων','Τύπος αναρτήσεων' ), // this will find the field groups for this post (post ID's of the acf post objects)
        'form' => true, // set this to false to prevent the <form> tag from being created
        'form_attributes' => array( // attributes will be added to the form element
            'id' => 'post',
            'class' => '',
            'action' => '',
            'method' => 'post',
        ),
        'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
        'html_before_fields' => '', // html inside form before fields
        'html_after_fields' => '', // html inside form after fields
        'submit_value' => 'Update', // value for submit field
        'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
    );
     
    ?>

    Recall, nothing is displayed (i also did steps to add somefunction to theme functions)

  • Hi @savvaskef

    I think the issue is your field_groups arg This should be an array of ID’s, not titles.

  • that’s a good idea.How do I find the IDs of the fieldgroups I created.

    I also don’t understand the following comment; maybe it’s crucial
    // this will find the field groups for this post (post ID’s of the acf post objects).
    Why do I have to get security rules & fields from a random post when it should be that the post obeys to rules created. ie admin sees all custom fields, authors see one.

    does Implementing the ACF form to edit a post includes standard fields like body/categories,etc ? If it does can you enable/disable fields?

  • Hi @savvaskef

    If the post ‘213’ is a real post which contains field groups via location rules, then the field_groups param is not needed. Please remove it from the array.

    Please also note that you do not need to include all params, just the ones you want to customize. What you are entering are the defaults.

    You can find the field group ID by editing the field group and looking in the URL. You will see a post_id=xxx

    I don’t understand your other questions.

    Thanks
    E

  • hi, E
    can you tell me how to locate the fieldgroup ID

    I think though, that this is not the case
    There are two tutorials in your plugin site
    One for new post, one for edit post
    In the addpost tutorial you are instructed to leave the customfields property empty.

    Again there might be some misunderstanding, is the code there to just add the customfields in a form(how to create such a form in html/php? ) or will it include custom/normal post-type fields created as a holistic solution

    There are competitive sites that do the trick but they are not free and constraining, I want control&free…so can you please helpme- you will have another fan 🙂

  • ..as an extention to the first question today, can you suggest a form plugin that is combatible with ACF?
    (in case your acf form shortcode does not complete the whole feedback form task)

  • Hi @savvaskef

    You can find the field group ID by editing the field group and looking in the URL. You will see a post_id=xxx

    the xxx is the field group ID. This is the number which you can use in the args like so:

    field_groups => array( xxx )

    Does that help?

    Thanks
    E

  • OK I DID THAT, NOW MY CODE LOOKS LIKE

    
    <phpcode><?php
    $options = array(
        'post_id' => 377, // post id to get field groups from and save data to
        'field_groups' => array(109,108 ), // this will find the field groups for this post (post ID's of the acf post objects)
        'form' => true, // set this to false to prevent the <form> tag from being created
        'form_attributes' => array( // attributes will be added to the form element
            'id' => 'post',
            'class' => '',
            'action' => '',
            'method' => 'post',
        ),
        'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
        'html_before_fields' => '', // html inside form before fields
        'html_after_fields' => '', // html inside form after fields
        'submit_value' => 'Update', // value for submit field
        'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
    );
    acf_form( $options ); 
    ?></phpcode>
    

    THE RESULT IS BLANK…MAYBE MY PHPCODE PLUGIN ISN’T COMBATIBLE,SUGGESTIONS?
    PLEASE ANSWER ALSO IF ACFFORM HANDLES STANDARD FIELDS
    (I HAVE ALTERED POST,NOT CREATED NEW POSTTYPE)

  • Hi @savvaskef

    Are you trying to ‘edit’ an existing post?
    Or are you trying to create a form to create a new post?

    Usually, when you need to define the field_groups arg, you are creating a new post. If this is the case, please remove the ‘post_id’ parameter.

    Also, you must better describe what you are seeing. When you say ‘blank’, do you mean the entire screen is white? Or just that the form is not displaying?

    If the screen is completely white, please turn on DEBUG_MODE in your wp-config.php file to show PHP errors. It is possible that you have a syntax error.

    Thanks
    E

  • My first posts were about both of the tutorials(the code has very minor differences from the suggested code in the tutorials). The problem is the same. I saw the header and sidebar of the customizr theme but in the content area nothing,blank.

    In create post there appears only a submit button but no fields, custom or not

  • Hi @savvaskef

    I can’t help but feel we are going around in circles as you are missing some important notes in my comments.

    Can you please go back to my previous comment and reply to each question. You have already explained the ‘blank page’, so no need to re explain that.

    Thanks
    E

  • Q:Are you trying to ‘edit’ an existing post?A:both but let’s start at create…

    The code from the documentation

    	$args = array(
    				'post_id' => 'new',
    				'field_groups' => array(109,108 )
    			);
     
    			acf_form( $args ); 

    was used.

    there is no form fields,no custom fields just a submit button with ‘Update’ caption.

    I tested combinations of using the $args def. & acf_form($args)
    php code in both the template and the page.No improovement.

    (I followed the guidelines and included acf_form_head(); php code on the
    template just before get_header(); and also added code to theme functions.php as in documentation.)

  • Hi,

    I experience same problems with implementation of creating and editing forms from frontend using the tutorial.
    It would be great to have some simple step by step working example with what files to be modified for a sample post with one custom field.

    Thank you

  • HI,
    I REDID THE STEPS CAREFULLY AND MANAGED TO GET THE CUSTOM FIELDS ALONE IN A PREETY UGLY SHAPE.

    is there any CSS to enable changing the looks of it as well as html/php for including the standard fields like title and description?

    i think this should be added to the tutorial

    Thanx E,
    I finally got somewhere but my questions are still pending

  • for lucian,
    I think the trick is to put all the code in the template, nothing on the page.
    so you may need to retry

    However this is not the case with edit form tut.
    You have to get the post Id from the page not the template i guess
    and edit it appropriately…
    I get blank in that case while putting the $options array in the template
    I get a 500 internal server error
    ideas, E?

  • Hi @savvaskef

    The code:

    
    $args = array(
    				'post_id' => 'new',
    				'field_groups' => array(109,108 )
    			);
     
    			acf_form( $args ); 
    

    Is simple and should work. Can you confirm that you have 2 field groups with ID’s of 109 and 108? If these are not loading, there is another issue going on here. The easiest way to debug this is to jump into the core/api.php file and find the acf_form function. There will be some code which uses the field_groups param to load in the field groups via a filter. Perhaps you can debug the result?

    Thanks
    E

Viewing 25 posts - 1 through 25 (of 41 total)

The topic ‘problems with post edit/add’ is closed to new replies.