Hello,
My first message, I live in France and I used ACF PRO 5.3.2.2 for 3 days.
Before to open this new tread, I checked this forum, google, etc and no solution for my problem.
I created a front-end form to create new post (with my CPT), so I need taxonomies fields, some custom fields but a “title” and a “content” fields in particular.
From the acf_form() documentation, I use this code:
acf_form(array(
'post_id' => 'new_post',
'new_post' => array(
'post_type' => 'my_cpt',
'post_status' => 'draft'
),
'field_groups' => array(365,440),
'post_title' => true,
'post_content' => true,
'submit_value' => 'Create a new post',
'updated_message' => 'Saved!'
));
My “title” and “content” fields well show and it works BUT …
1 – How can I change these fields properties (max length, required (yes/no), readonly, disabled ….) ?
1.1 – in fact I find a solution to change label, instructions, maxlength = nnn, …with a “acf/get_valid_field” function (see below) but (for example) if I put “required = 1” for the content field with this function, the red star is well shown on the front-end form (good) BUT this “required” property is not considered during the form submission. Example: if the required content field is empty, the post is nevertheless created … so it’s not a good solution. 🙁
add_filter( 'acf/get_valid_field', 'change_form_fields_properties');
function change_form_fields_properties( $field ) {
if($field['type'] == 'wysiwyg') {
$field['type'] = 'textarea';
$field['label'] = 'Description';
$field['instructions'] = 'an instruction text';
$field['required'] = 1;
}
else
{
if($field['name'] == '_post_title') {
$field['label'] = 'Titre';
$field['instructions'] = 'an instruction texte';
$field['maxlength'] = 20;
}
}
return $field;
}
So what is the “complete” solution ? (it’s a shame there is no “natural” and “simple” solution for this)
Remark: I try to use the Jivedig solution (http://thestizmedia.com/front-end-post-editing-with-acf-pro/) but it doesn’t work with ACF PRO 5.3.2.2
1.2 – How can i put custom fields BETWEEN “title” and “content” fields ? Impossible to do this 🙁
example:
title field
custom field 1
custom field 2
content field
I tried to change the “position” option (==> “high (after title)”) in ACF Admin field group, but nothing change … all my custom fields are still UNDER content field… it’s a shame to …
So how can I modify this position ?
Thanks a lot for your answer ?
Claw
Hi @claw
I think the easiest way for you to achieve all of these is to create your own custom fields for the title and the editor. You can then hook into the acf/save_post
action hook and retrieve the values in these fields yourself and add them as the post title and post content.
A few things to note:
1. You have to run the remove_action before saving the post title and content (if you save them using wp_update_post) and add it back in after. Basically same thing as this explains with infinite loops except you apply it to acf/save_post: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
2. You’re gonna end up with two extra fields in the admin area for these title and content fields. You can remove them in the admin area using CSS or simply disable them (to stop user from being able to interact with them) using this filter: https://www.advancedcustomfields.com/resources/acfload_field/
and setting $field['disabled'] = 1;
Hello Jonathan,
Thanks a lot for your answer but I’ve given up ACF (PRO) for 6 months when I saw I had no answer in ACF forum !
So I deleted ACF PRO from my website and I created an home solution and it works perfectly !
Claw
Hi @claw
Sorry to hear that but I’m glad you figured it out 🙂
I suppose if that was the only reason you needed ACF I can understand you, I often write my own plugins when I find others not doing it the way I want to. ACF is so much more than front end forms tho!
I want to note that the ACF forum is not a guaranteed place for response. We are just 2-3 fellas working hourly here in our spare time for a low penny (it’s more about giving some back to the community). If you’ve purchased an ACF Pro license you can also try contacting the support by email. There’s a button in the right sidebar under “Support”.
As @Jonathan said above you just need hook acf/pre_save_post
action to save custom title and content field into post title and content field. See sample code below
Just replace custom field keys below with specific field keys from field group.
/**
*
* Saving Custom fields into post fields acf_forms
*
*/
function acf_review_before_save_post($post_id) {
if (empty($_POST['acf']))
return;
$_POST['acf']['_post_title'] = $_POST['acf']['field_577ba2a05bb54'];
$_POST['acf']['_post_content'] = $_POST['acf']['field_577ba2b15bb55'];
return $post_id;
}
add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
Hey Jimi007 !
I have used this code and it worked good for one post type.
how can i use this on two post types separately?
Thanks in advnace
Hi @novelgroup
Well I think so we can get the post_type from the acf_form array and use that to change the content for each post type but as to me simple way is to just add a hidden field with post type value and get that in acf/pre_save_post
action. I just tried this and its working for me.
First add the hidden field in acf_from and then get the value
'html_after_fields' => '<div><input type="hidden" name="my_pt" value="u_talez"></div>',
function acf_review_before_save_post($post_id) {
if (empty($_POST['acf']))
return;
//form 1
$Post_type = $_POST['my_pt'];
if ( $Post_type == 'cpt' ) {
$_POST['acf']['_post_title'] = $_POST['acf']['field_577ba2a05bb33'];
$_POST['acf']['_post_content'] = $_POST['acf']['field_577ba2b153bb51'];
}
//form2
$Post_type = $_POST['my_pt2'];
if ( $Post_type == 'pt2' ) {
$_POST['acf']['_post_title'] = $_POST['acf']['field_577ba2a05bb33'];
$_POST['acf']['_post_content'] = $_POST['acf']['field_577ba2b153bb51'];
}
return $post_id;
}
add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
Hi @novelgroup
Without creating an hidden field I have tried to get post_type value in acf/pre_save_post
action but get_post_type($post_id)
did not worked. So far I can get the value using $GLOBALS
variable e.g
if ( $GLOBALS["acf_form"]["new_post"]["post_type"] == "cpt_name" ) {
do whatever
}
The topic ‘Front end form,how to change post_title and post_content properties’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.