funkjedi / acf-qtranslate

Adds qTranslate compatible fields for Text, Text Area, Wysiwyg Editor and Image.
http://wordpress.org/plugins/acf-qtranslate/
57 stars 32 forks source link

Way to get_field only for the current language? #85

Open riverofjanuary opened 7 years ago

riverofjanuary commented 7 years ago

Is there a way to have get_field return false if the current language is an empty text box? Currently, if I have a text field and I get_field() on this box, I get the English translation for English and French for French IF French is filled out. For some reason if French is not filled out, I get the English instead -and I'd like to return blank or false.

Alternatively (for my purposes) if there was a way to have checkboxes reflect the language (eg checked for English, unchecked for French) that would solve my issue.

jevgen commented 7 years ago

Having the same problem. Will this old function work in "X" version of plugin? $myString = get_post_meta( $post_id, 'Functions', true); echo qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($myMeta);

jpsantos7 commented 6 years ago

I'm having the same problem. I don't want the get_field() to return the values for the languages that are left empty. How can I just hide the french content to the english user if the value is not set or vice versa?

funkjedi commented 6 years ago

You could try adding something like this to your functions.php.

remove_all_actions('acf/format_value');

add_action('acf/format_value', function(){
  if (is_string($value)) {
    $value = qtranxf_useCurrentLanguageIfNotFoundShowEmpty($value);
  }
  return $value;
});
jpsantos7 commented 6 years ago

Hey thanks for the quick response 👍

I tried adding that to the functions.php but it didn't quite work.

I searched inside the plugin folder and found that there are 2 acf.php files. One inside acf_4 and another one inside the acf_5.

On the acf_5/acf.php I found the

add_filter('acf/format_value',                array($this, 'format_value'));

and on the acf_4/acf.php I found the

add_filter('acf/format_value_for_api',        array($this, 'format_value_for_api'));`

I tried using the format_value_for_api like this:

remove_all_filters('acf/format_value_for_api');

add_filter('acf/format_value_for_api', function(){
  if (is_string($value)) {
    $value = qtranxf_useCurrentLanguageIfNotFoundShowEmpty($value);
  }
  return $value;
});

I also tried using remove_all_actions(); and add_action();. It got me the same results one way or another which is that now I only get no values for the custom fields even if they're filled.

Is there something I'm missing here?

Thanks again

funkjedi commented 6 years ago

Part of the problem it appears is that qTranslate-X starting in 3.2.3 is now translating get_post_metadata directly. So you'll need to remove that filter as well. And also update the closure on the filter being added to include the $value argument.

remove_filter('get_post_metadata', 'qtranxf_filter_postmeta', 5, 4);

remove_all_filters('acf/format_value_for_api');

add_filter('acf/format_value_for_api', function($value){
  if (is_string($value)) {
    $value = qtranxf_useCurrentLanguageIfNotFoundShowEmpty($value);
  }
  return $value;
});
jpsantos7 commented 6 years ago

Thanks!! that worked great! :)

jpsantos7 commented 6 years ago

Everything's working great with input texts and text areas.

I've tested it with qtranslate Images and Files using the return value as image URL and File URL and it's not working unfortunately. It's returning some numbers.

I haven't tested with WYSIWYG though.

funkjedi commented 6 years ago

This remove_all_filters('acf/format_value_for_api'); removes all the formatting filters including the ones added by ACF. Which means you're ACF isn't formatting the field values for you anymore.

Those numbers you're seeing are the Post IDs for the attachments. To get the URL you need pass the ID into wp_get_attachment_url() to get the URL for the attachment.

Simpler move might be to not use remove_all_filtersat all and just adjust the priority on the filter. Try doing this instead:

remove_filter('get_post_metadata', 'qtranxf_filter_postmeta', 5, 4);

add_filter('acf/format_value_for_api', function($value){
  if (is_string($value)) {
    $value = qtranxf_useCurrentLanguageIfNotFoundShowEmpty($value);
  }
  return $value;
},8);
jpsantos7 commented 6 years ago

I doesn't work without adding remove_all_filters()

I think I'll try using wp_get_attachment_url() everywhere I need to get the URL. It's far from ideal but it works.

Thanks for the support again

dudupavani commented 6 years ago

this code empty all fields. not function.

When I leave an empty field in the ACF, the qtranslate plugin replaces the field with the data of the other language that is on the page. This is not happend with traditional editor field