I’m having issues with my post_title saving on the frontend, even after 5.4.2. update (currently running 5.4.4). I’m loading acf_form through an AJAX call – this work perfect in 5.3.8 but 5.4.0 and after breaks post_title.
The rendered HTML has no id, class, or name values on the input field. The label text also does not display. The value does correctly load when editing an existing post, but with no name attribute it obviously doesn’t save.
Here’s the post_title HTML as displayed when the page loads (acf_form just in the plugin php file – this saves correctly):
<div class="acf-field acf-field-text acf-field--post-title" data-name="_post_title" data-type="text" data-key="_post_title" data-required="1">
<div class="acf-label">
<label for="acf-_post_title">Title <span class="acf-required">*</span></label>
</div>
<div class="acf-input">
<div class="acf-input-wrap"><input type="text" id="acf-_post_title" class="" name="acf[_post_title]" value="My Title" placeholder=""></div> </div>
</div>
And here is the same post_title field when loaded with an AJAX call:
<div class="acf-field acf-field-text" data-type="text">
<div class="acf-label">
<label for=""></label>
</div>
<div class="acf-input">
<div class="acf-input-wrap"><input type="text" id="" class="" name="" value="My Title" placeholder=""></div> </div>
</div>
All other custom fields work as expected, via the same AJAX loaded acf_form.
The Admin side of the forms all work as expected.
I’m on a WP Multisite installation, WP v4.6.
Backing up to 5.3.8 makes the post_title load correctly via AJAX. Newer versions don’t work.
I opened a support ticket and will report back when I hear more, but wanted to see if anyone else had this issue as well. Anyone else run into this and have a fix?
I did some more testing – I’ve deactivated all other plugins and had the same results. I then stripped down my AJAX call to the bare minimum and ran it in a single-site WP v4.6 install (separate website, running a bare-bones theme, ACF v5.4.4, no other plugins) – same issue as above
Here’s the relevant PHP & JS:
// in functions.php
add_action( 'wp_enqueue_scripts', 'add_scripts_styles' );
function add_scripts_styles() {
$reqScripts = array('jquery');
wp_enqueue_script( 'cmsAJAX', get_template_directory_uri() . '/js/script.js', $reqScripts, '1.0' );
wp_localize_script( 'cmsAJAX', 'myAjax', array( 'ajaxurl' => admin_url('admin-ajax.php')) );
}
add_action("wp_ajax_test_cms_form", "test_cms_form");
function test_cms_form() {
echo "In AJAX";
acf_form( array('post_id'=>14,'post_title'=>true) );
die();
}
// in script.js
(function($){
$(document).ready( function(){
do_test_form_ajax();
});
function do_test_form_ajax(){
var data = {
'action' : 'test_cms_form'
};
$.ajax({
type : "post",
dataType : "html",
url : myAjax.ajaxurl,
data : data,
success : function(response) {
$( 'body' ).show().append( response );
},
error : function(xhr, ajaxOptions, thrownError) {
$( 'body' ).append( "AJAX error" );
}
});
return false;
}
})(jQuery);
Here again, the post_title field displays with no name, id, class, or placeholder attributes set. Specifically the attribute words show up in the Inspector, they just has no value (see prior post code).
Any ideas?
See Elliot’s comment in this topic, https://support.advancedcustomfields.com/forums/topic/using-acf-form-in-ajax-call/ Does that help?
That’s more so related to interactive JS on the form not working. That’s an issue I’ve run into in the past and actually that exact post helped in that case, but this is a different issue.
It seems like the problem here is related to the actual rendering of the form elements. Unless maybe some JS is removing the attributes after output, but that seems fairly unlikely since all of the other fields render and save correctly.
I get the impression I’m experiencing the same issue that’s noted as being fixed in the 5.4.2 update, just applied to an AJAX loaded form.
Have you submitted a support ticket? It seems odd that the name attribute as well as several others are missing when you load the form using ajax. I’ve looked through the ACF code and I can’t see why this would be happening. Could just be over my head.
Yep, they suggested disabling all plugins, etc. Waiting to hear back again after I tried that and reported back with same results.
No problem, thanks for looking at it!
I did some more looking and I think this has something to do with acf_form_head(). The field _post_title is defined in acf_form_head(), or at least the function that calls them is called by acf_form_head(). This is what I was looking for because of the fact that all the attributes are empty let me to believe that the field was not properly defined. Try calling _acf_form_register_fields()
at the start of your ajax function before you call acf_form()
That seems to have fixed it! The post_title field now has all of the correct attributes and it appears everything is saving correctly on both my live site and test site. Thanks a million, John!
My test function from above now works and looks like:
function test_cms_form() {
echo "In AJAX";
_acf_form_register_fields();
acf_form( array('post_id'=>14,'post_title'=>true) );
die();
}
I wonder what happened in the 5.4.0 update that made this addition necessary (that seems to be the first version where post_title wasn’t rendered properly) and if this is a fix that can be implemented in a future release to ensure it works for others.
The function _acf_form_register_fields and the call in acf_form_head() was added 22 days ago… in version 5.3.8, the last version you say it was working the fields were defined during validation process and now they are defined before the validation process. Basically, before the fields where defined just before they where possibly used and now they are defined earlier process. I don’t know if this is a bug or a feature, but basically now if acf_form_head() is not called the local fields for _post_title and _post_content will not be defined unless you call the new function I posted above.
I’ve brought this to the attention of the developer to see if he has any advice on the matter.
An update on this, there is a fix coming that will also be tied into this issue https://support.advancedcustomfields.com/forums/topic/acf_form-security-issues/. I see a new class that will be automatically instantiated. acf_form() and acf_form_head() will be wrappers for methods in this class.
I’m not 100% sure what going to be done about the security issue, I’d keep an eye on that thread.
The topic ‘post_title issue remains after 5.4.2 AJAX loading acf_form’ 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.