Home › Forums › Backend Issues (wp-admin) › Got false bool when using update_field()
Hi,
I am currently trying to pass a json string into a text area field using the field key, but I am getting a false. Does anyone know any common reasons why this would fail?
This is done from a custom admin menu page if that distinction helps.
Hello Drew!
Please, give us the part of the code when you try update a field and give us more details about where is your field. Does it is from a group? A repeater ? Other? What is the exact structure?
Best regards!
Thanks for responding Sam! So I’m currently trying to update the field with text input from a key and value input text box when a submit button is being pressed in an admin menu page. The field I’m trying to update is a text area. Here is the code:
<form action="" method="POST">
<input type="text" name="key" id="key">
<input type="text" name="value" id="value">
<input type="submit" name="Submit" value="send" >
</form>
<?php
if(isset($_POST['Submit']))
{
$key = $_POST["key"];
$value = $_POST["value"];
$array = array($key,array($value));
$out = json_encode($array);
update_field('field_key',$out);
}
?>
Yo!
I writen you a really nice tutorial from my hands, but ACF has block me because I take to much time or I don’t know and my text has been removed… Just wooow… Nice!
Wait me!
Ok,
(btw ACF, I have takes 45 minutes-1 hour for writing my shit. I’m crying.)
So, I started by telling you that it was going to be a little hard for me to help you without knowing the structure of your field group.
For this reason, but also seeing that there is a lack of knowledge on your part, I preferred to show you how I would do it from A to Z.
Oh! I started by telling you a big sorry for my bad english too hehe.
So it start like this:
First of all, create your HTML structure without your PHP:
(I purposely used a different structure than yours to show other possible ways)
<form id="mySuperForm" action="" method="POST">
<input type="text" name="key" id="key">
<input type="text" name="value" id="value">
<button type="submit">
<span>Send</span>
</button>
</form>
Right after, manage your form with JavaScript like that (I am more concise than before. To do so, ACF really pissed me off.):
<form id="mySuperForm" action="" method="POST">
<input type="text" name="key" id="key">
<input type="text" name="value" id="value">
<button type="submit">
<span>Send</span>
</button>
</form>
<script>
const formElement = document.getElementById("mySuperForm");
if(!formElement) return;
/*
* Recup field
*/
const k = document.getElementById("key");
const v = document.getElementById("value");
/*
* Create a variable for prevent multiple submits
*/
let canSubmit = false;
formElement.addEventListener("submit", (e) => {
if(!canSubmit) return;
/*
* Prevent form to be automaticly submit on "Enter press" or button "Send" click
*/
e.preventDefault();
/*
* Do your field verifications here
*
* Add an error CSS class when a field is not good ...
*/
/*
* If error found, return
*/
if(formElement.querySelector(".error")) return;
canSubmit = false;
formElement.style.opacity = .6;
formElement.style.pointerEvents = "none";
/*
* If no error, send with fetch.
*
* The ajaxurl variable exist from the back-end. You need to create it if you when play with ajax from front-end
*/
const formData = new FormData();
formData.append("action", "save-something");
formData.append("key", k.value);
formData.append("value", v.value);
fetch(ajaxurl, {
post: "post",
body: formData
})
.then(resp => resp.json())
.then(data => {
canSubmit = true;
formElement.style.opacity = 1;
formElement.style.pointerEvents = "initial";
console.log(data);
});
});
</script>
Ok cool, you send your data in the air, but you want play with it, do some verifications and save your data if all is good. Go in your functions.php and add this code. (don’t forget change $post_id by yours or by “option” and do your verifications):
https://pastebin.com/P7CUpT1h
If you put a look to my code, you see some wp_ajax_{action} hook…. with my first text, I explain that to you, but right now, sorry, but I don’t want. (Fuck ACF…)
All done!
Don’t hesitate if you have some questions 💪
And now, I just update my second post and he deleted himself.
Hahaha. Hey lol, just no.
I’ll help you when ACF’ll correct there Forum. The hell.
Update_field() has the same return as the WP function update_postmeta().
When using update_field() the way you are using it you must tell ACF the post ID to be updated
update_field('field_key',$out,$post_id);
If it is returning false then either the value was not updated or the value has not changed. To check if the value is updated look in the _postmeta table in the database for the value of the field name on the post_id being updated.
Hey @hube2!
Do you have correct the probleme? If yes, are you able to see my second post before this big tutorial? It’s an other big tutorial, but better hehe.
If yes, could you replace it and remove my messages? Thanks!
@deuxparquatre I can see all the post, all posts should now be visible to everyone. If there is one missing then it does not exist.
The simple answer to the OP and their original code is that the post ID is required when using update_field() in the manner that it is being used because ACF cannot detect the correct post ID when using a form created outside of ACF.
Oh dang, I just hopped back and saw the posts and currently reading through! So I have a quick question on the postid for update_field. Is there a post id for custom admin menu pages? When I look at the url, all it says is page=<page name>. I don’t see a numerical value that would correlate to a post id.
Hi @deuxparquatre, thanks for the indepth post! I have a quick question on the ajax part. I am unfamiliar with the fetch function. Is that a php or wordpress function?
@hube2 I don’t know why, my answer don’t display. Do you see it? ….
For real please, put a look on the forum. The hell I post and I can’t see my post or the forum said I’m a bot if I back and foward forum and Google for some search.
Is there a post id for custom admin menu pages? When I look at the url, all it says is page=<page name>
Is this an ACF options page that we’re talking about? If yes then the post ID would be “options” or whatever you used for post_id when adding the options page.
So @drew321, I try again maha…
(In fact, it’s my third test. I think the URL I share break my send, but I have no notice…)
For your question about post_id, you have multiple ways. Just, all depend of your structure. How you have set that? …
Since I don’t know the answer, I’ll tell you again how I would do it hehe.
With WordPress you can create admin pages, but with ACF Pro, you can do it in a simpler way.
Put a look here: advancedcustomfields[dot]com/resources/options-page/.
If you manage your admin pages with ACF Options Pages, you’ll be able to use update_field with “option” value for your $post_id instead of a specific post ID.
For your question about Fetch: Fetch has coming with modern browsers. But Fetch is like Ajax and it’s JavaScript. Not related with WordPress. Put a look here: w3schools[dot]com/js/js_ajax_intro.asp
But with WordPress, you can manage Ajax/fetch with security. You need to use the hook wp_ajax_{action} or wp_ajax_nopriv_{action} for that. Put a look here: developer.wordpress[dot]org/reference/hooks/wp_ajax_action/
With my last message, I forgot to tell you what you could do when it’s not an options page. For example, you save a publication in a CPT and you don’t have the ID before publishing.
Use the “save_post” hook for this: https://www.advancedcustomfields.com/resources/acf-save_post/
I don’t believe this is an ACF option page. This is a page I made with the add_menu_page() and add_submenu_page() function calls. Are these still considered option pages?
I see. Is it easy to convert admin pages to acf option pages?
To know if the difficulty of transferring to an options page is complicated, I would have to know your project.
I also thought of another idea. You may also be able to integrate an ACF form into your WordPress admin page with “acf_form()”. Put a look here: https://www.advancedcustomfields.com/resources/acf_form/
(But it’s just one idea among others.)
Just know, you have several possibilities, but I can’t know if what I’m saying is well thought out because I don’t know your project.
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.