Saving an array to Advanced Custom Fields WordPress - wordpress

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 usually more 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 :
'ID|URL|STRING'
note that the delimiter pipe (|) is just an example, you could use ( , ) ( : ) ( # ) or whatever.
in other words :
$url = 'http://myurl.url/something';
$id = 35;
$string = 'my string';
$pseudo_r = $id . '|' . $url . '|' . $string . '|'; // results in 'ID|URL|STRING';
or you can use the implode() function
$pseudo_r = array(
"url" => $url,
"id" => $id ,
"string" => $string
);
$pseudo_r = implode("|", $pseudo_r); // gives a string
and pass it as a string that later on you can decompose it with explode() like so :
$pseudo_r = explode( '|', $pseudo_r ); // gives back the original array
( 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 a STRING .
All 3 ( should ) have potentially different validation schemes , and I suspect that the htmlspecialchars() part belong to the URL.
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() and explode() functions.

Yo can save the array using json_encode($your_array) and json_decode() to recover it. This works also for associative arrays.

Related

WordPress SDL Translation for shortcodes

I have some sort of sort of shortcodes like [wp-text text="Lorem Impsum etc" button_text="Read More"][/wp-text].
I want to extract this shortcode from post content field and then send these two attributes: text and button_text for translation to SDL World Server.
After getting response from SDL, I want to replace that shortcode accordingly.
Any Suggestion or Help?
Here is a suggestion about your approach:
Parse the shortcode attributes
Having the content you can parse the variable and extract all short code attributes with the provided Wordpress function shortcode_parse_atts
For each parsed value make a API call to the provided SDL World Server REST API (Or for better performance you can group all the translations and translate them with one complex API call if supported from the API)
After getting the response replace the original strings with the translated strings.
Here is example pseudo code in PHP:
$shotcode_attributes = shortcode_parse_atts( $post->post_content );
foreach ( $shortcode_attributes as $attribute => $value ) {
// Make SDL World Server API call refer to the API documentation for the exact way
$translated_value = $sdlApiClient->translate( $value );
// Replace the translated value.
// You can be more precise here ensuring that the value is inside the shortcode
$post->post_content = str_replace(
"{$attribute}=\"{$value}\"",
"{$attribute}=\"{$translated_value}\"",
$post->post_content
);
}
You can also research and examine the provided SDL WordPress Connector

Wordpress get custom fields value

So I am trying to get custom post values in WP. The thing is I can have multiple custom fields value. So I don't want to hard code in the code. What I am thinking of doing is providing the a prefix in the key value something like
abc_email
abc_website
So I want to use this function
get_post_meta
or some other to get all the key,value pair that starts with abc. That way I can just add values on the back end and don't have to update my code. One way would be to get all the meta data using above function and then loop over it and filter. But is there any other way where I can send in the pattern I am looking for?
Thanks
You could directly query the database for a posts entries containing the prefix (relevant table etc here https://wordpress.stackexchange.com/questions/104434/where-are-custom-field-values-stored-in-the-database )
but this is more complex, still involves a loop and doesn't provide you with anything you could do using WP and PHP. If you want to cater for unknowns you will have to loop or interogate an index etc.
I may be missing something in your question but this seems straight forward:
You are trying to avoid this scenario:
process_field($abc_known1);
process_field($abc_known2);
// continually edit code for new entries e.g.:
process_field($abc_NEW1);
Something like this will handle later additions without modification.
// get array of ALL cust fields for post
$all_cust_fields = get_post_meta( get_queried_object_id());
foreach ($all_cust_fields as $key => $value) {
// some function for determing if $value starts with "abc_"
// if so either:
// add to an "abc" array for later use/processing
// or take action in this loop e.g.
process_field($value);
}
I am not sure whether you are talking about keys or values prefixed "abc_" but the same principle applies.
you should write code like this,
$email_meta = get_post_meta( 'your_post_id', 'abc_email', true );
$website_meta = get_post_meta( 'your_post_id', 'abc_website', true );
Or
you can follow these links,
example
example
Hope this will help you.

Woocommerce Display Category Image - get_woocommerce_term_meta deprecated

I'm in need of some help.
I want to display the category image on the current category page, and I have googled this, and each answer I find uses the same code.
They all use get_woocommerce_term_meta to retrieve the ID of the thumbnail used so that you can then use wp_get_attachment_url to get the image address.
All sounds great, but whenever I try this code it returns nothing, and I think that it is because get_woocommerce_term_meta is deprecated.
Does anyone know of a way round this, so I can get the image address when I have the category ID?
This is the code that I have in place:
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
echo $cat->term_id."<br />";
echo $thumbnail_id;
$image_src = wp_get_attachment_url($thumbnail_id);
$cat->term_id returns the correct ID of the category, but $thumbnail_id returns 0.
The code is in header.php.
get_woocommerce_term_meta() may be deprecated however it hasn't yet been removed. The issue is elsewhere in your setup.
With that said we can resolve the deprecation issue quite easily. Simply replace usages of get_woocommerce_term_meta() with the new native WordPress function get_term_meta().
get_woocommerce_term_meta() will simply pass its arguments on to the new function anyway so we can be sure the problem isn't with the deprecated function.
Likely causes of the issue:
Key used to save the image isn't the same as the key being used to retrieve
Not passing in correct term ID
No image set

How bad is $wp_rewrite->flush_rules() on 'init' action hook?

According to the Codex:
[...] This function can be extremely costly in terms of performance. It should be used as sparingly as possible - such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.
Ok, so, I know it shouldn't be used in every page load and I'm not, but I still need a very conditional rewrite rule on this project.
I have these 2 url structures:
- example.com/products/tables/fancy-computer-table //This is a product url, here I expect the normal behavior from wp.
- example.com/products/tables/office //This should be some kind of a filter, where the site gets all the tables related to the office department. Note how both URL structure matches.
To make it work, I narrowed it down to these very specific URLs, using some regex. If matched, I'll run 2 queries to verify if the url I'm in is for the filter I want that rule to apply, or if it's a product url. In the latter, I want wordpress to behave normally. But I have to flush the rules either way, whether it's a product or it's the category and the filter, so that both pages work properly and dinamically.
I do all this to narrow down the use of this function to the least possible, but the Codex doesn't really tell me how bad it does affect the performance, or why.
I'm passing the parameter on that function as false by the way, so it doesn't rewrite my htaccess file, but I'm not sure where that rewrite rule option is stored, if it's on the memory somewhere, or on the database, wish they would provide some clarification on that.
Thanks in advance for reading this question. Any pointers are appreciated here, even if you want to point me to some other way of doing this. :)
EDIT: Posting some code here so you guys can actually understand what I mean and let me know if this maybe this could be bad practice... maybe a suggestion on how to do it better.
<?php function custom_rewrite_products()
{
// regex to match either "products/tables/fancy-computer-table" or "products/tables/office"
preg_match('#products\/([^\/]+)\/([^\/]+)\/?#', $_SERVER['REQUEST_URI'], $url_matches);
if(is_array($url_matches)) {
$query_category = new WP_Query('category_name='.$url_matches[1]);
$query_category->get_posts();
$args = array(
'post_type' => 'filters',
'name' => $url_matches[2]
);
$query_post = new WP_Query($args);
$query_post->get_posts();
if($query_category->have_posts() && $query_post->have_posts()) {
$category_ID = '';
$filter = '';
$category_ID = '' . $query_category->query_vars['cat'] . '';
$filter = '' . $query_post->query_vars['name'] . '';
$string = "index.php?cat={$category_ID}&filter={$filter}";
add_rewrite_rule('products/([^/]+)/([^/]+)/?$', $string, 'top');
}
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules(false);
}
}
add_action('init', 'custom_rewrite_products');
I still don't understand why, but the flush is needed here, so the $category_ID and $filter variables actually get passed to the rewrite rules. If I take it out, the rewrite just goes to the index page, the place where the values should be are empty.
In case you're wondering, I created the filters on a custom post type and they are related to each post by using a custom field, since they're like four or five and they're present on every category on this project. As you can see, I already have a category/subcategory structure and I didn't find it smart to create these filters once again by hand inside each main category.
There is a 'rewrite_rules' record in the 'wp_options' table that includes all the rewrite rules. When calling flush_rules(), WordPress will clear this record and regenerate all the new rules, including the ones you're not changing.

What do i put in get_permalink(get_option(' here???')

I'm trying to work out what the option value is in get_permalink(get_option('jr_dashboard_page_id') I'm trying to follow this convention but cant work where it comes from - dashboard is the post-name in the DB what the rest of it means or how its works I cant figure out...
Any help would be great..
The Options API stores key-value pairs. For example, add_option( 'key', 'value' ); could be used. In that case, get_option( 'key' ); would return value.
So at some point a plugin or the themes that you use defined jr_dashboard_page_id to be something. Perhaps via a meta box or something like that. There is not enough information in this question to say exactly what "key" you need. It's like saying, I have an associative array $array['key'] what key should I use? We simply can't know because we don't know how array looks and we don't know what kind of value you expect.

Resources