I’m using the ACF plugin for WordPress. I’m posting to a custom post type programmatically via my theme. Now, I thought I would be able to save an array of items to a custom field quite easily, but whenever I try to, nothing gets input. I simply get this error when I check the post screen in the admin area:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /home/lukeseag/public_html/spidr-wp/wp-content/plugins/advanced-custom-fields/core/fields/text.php on line 127
Now, I’m kind of throwing this out there for suggestions as to how I can go about solving this problem.
To give some context, I’m programmatically saving data input by a user, and creating a post with it. Much of the data is simple strings and numbers that can each have their own custom field.
But I have an unknown number of text strings
and URL's
(with an id number for each) coming through this page too, that need to be linked with the same post. I need to be able to output each set of text string
and URL
into their own div on the single.php
post page. Now, ideally these text/url pairs would be saved in a single array so I can just loop through the array and output the data, but it looks like ACF doesn’t want to let this happen?
Any suggestions as to how I can save this data and then output it easily on the single.php
page? Each set of results will have an ID
(number), text string
and url
.
Thanks!
This is exactly why all those
"frameworks"
are usuallymore pain than gain
. they are designed to look flexible to the lazy but then they always prove useless on a real case scenario that is a bit more complex than anything that would actually be easily achieved without them.Anyhow, as for your question.
The problem is that you are passing array instead of a string .
I am not going to go into debugging the plugin, and anyhow , more code and info is needed , so i will just give you ONE possible and easy solution, and that is to compose a
pseudo-array string
like so :note that the delimiter pipe (|) is just an example, you could use (
,
) (:
) (#
) or whatever.in other words :
or you can use the
implode()
functionand pass it as a string that later on you can decompose it with
explode()
like so :( Please note that now the array is
NON ASSOCIATIVE
anymore . )now, since you mentioned that each one of those is a
set
, you can compose the same pseudo array from the sets, using a different delimiter.This might work for you , but
essentially it is wrong
.why is it wrong ?
Because this is a generic and somewhat hackish solution that actually ignores your data type .
You are mixing 3 types of data types ,
URL
,ID
and aSTRING
.All 3 ( should ) have potentially different validation schemes , and I suspect that the
htmlspecialchars()
part belong to theURL
.Another solution is just to find the right data field in the ACF settings or use a different field for each and then just DISPLAY it together , also using the
implode()
andexplode()
functions.Yo can save the array using json_encode($your_array) and json_decode() to recover it. This works also for associative arrays.