Site logo

Archived topic

How to add acf_form_head() function before get_header()

8 replies · Started by Kathryn on August 20, 2021

Viewing posts 1–9 of 9

Hello there, hope you can help me. I have an edit page which renders edit form for my custom post type on front end. It loads all the data correctly, but not saving correctly, e.g. either making a duplicate custom post instead of updating current one or overwriting actual page where the form is rendered. I found that I need to place <?php acf_form_head(); ?> inside the form template, but before <?php get_header(); ?>.

From their documentation:

This function must be placed before any HTML has been output, preferably above the get_header() function of your theme file.

I thought of using GeneratePress Element Hook to hook into wp_head and only paste <?php acf_form_head(); ?>, set display rule to edit page where the form is rendered, set to execute PHP and set priority to 1? Am I on good track and if not, what would be the best way to achieve this, e.g. get this function to load before get_header() ?

Thank you

Tried, but it is not working. Somewhere else is the problem, but it is not your problem, since this is exlusevely ACF related. The biggest dissapointment is that I have ACF and ACF Extended both pro versions $49/year and can't get any support. I couldn't even get loading custom posts without hard research, which ended up with 3 lines of php to somehow pass the ID to the form I created for explicitly that custom post type. No matter, they have forms with Post/Update actions, no matter I connected all the fields for both loading and saving, still wasn't loading untill I added filter to return the ID and still not saving, no matter what I try to do. Thank you nonetheless!

Seem strange that you cannot get any support even with the pro version.

So in order to achieve what they are asking with the get_header(), you would need to:

- Start a child theme:
https://docs.generatepress.com/article/using-child-theme/

- Copy the files page.php and single.php and add them in the child theme folder:
https://github.com/tomusborne/generatepress/blob/master/page.php
https://github.com/tomusborne/generatepress/blob/master/single.php

Then add the edit the files to add in the line:
https://github.com/tomusborne/generatepress/blob/master/page.php#L16
https://github.com/tomusborne/generatepress/blob/master/single.php#L11

I always start with child theme when it comes to GeneratePress. Tried modifying files - they work, but the form is still not updated, so the function before get_header() is not the cause of the problem. It is only something I found myself, they rather talk without getting into the problem.

In order to get ID of the custom post type I made a filter that is loaded prior to the form:

add_filter('acfe/form/load/post_id/form=my-edit-form', 'my_form_post_values_source', 10, 3);
function my_form_post_values_source($post_id, $form, $action){
    $post_id = $_GET["post"];
    return $post_id;
    
}

This was the crucial so the form loads correct custom post type. Since the guy from ACF Extended only appoints people to hooks inside his plugin, presuming everyone knows php as good as himself, I searched through the hooks and found the hook to change the arguments of the form. Using the same $post_id variable inside that hook didn't make custom posts update.

add_filter('acfe/form/submit/post_args/form=my-edit-form', 'my_form_post_args', 10, 4);
function my_form_post_args($args, $type, $form, $action){	
   $args['ID'] = $post_id;   
	  
    return $args;
}

I tried all different kind of stuff to get the ID here. I tried with and withhout $post_id = $_GET["post"], with and without checking for update action name, different ways to get the post ID, but nothing - it mostly just overwrites my edit page (when I dont use any filters it saves another custom post with that name) instead of updating the existing. There more hooks to try, but not sure how they would work, if this one doesn't work ("Alters the post arguments before database insert/update").

Hooks for changing arguments:

add_filter('acfe/form/submit/post_args', 'my_form_post_args', 10, 4);
add_filter('acfe/form/submit/post_args/form=my-edit-form', 'my_form_post_args', 10, 4);
add_filter('acfe/form/submit/post_args/action=my-post-action', 'my_form_post_args', 10, 4);

Hooks for saving:

add_action('acfe/form/submit/post', 'my_form_post_save', 10, 5);
add_action('acfe/form/submit/post/form=my-edit-form', 'my_form_post_save', 10, 5);
add_action('acfe/form/submit/post/action=my-post-action', 'my_form_post_save', 10, 5);

Just came to me, it might be that only need to add action on saving hook.

No success, unfortunately. I've been trying and trying, but can't get it work properly and nothing really I could find online. Several support problems, but their solutions didn't work. Thanks anyway, I am marking this as resolved.

No problem :)

This archived topic is closed to new replies.