Before I display ACF field, I want to manipulate it

Before I display ACF field, I want to manipulate it with my codes how to do this?
Are there any hooks documentation?
I want to manipulate my price field to display converted price on multiligual page. How do I do this?

since 1.2.5.2 you can

  • Improvement: Call function by using dynamic tag {return=your_function('string', get_the_ID())}
    give it a try, I use it to make automatic caption unit conversions in/cm cm/in and it works just as written
1 Like

Can I use the Code Snippets plugin to call my function out on my ACF field.

Do I have to use it in the post edit screen?

The thing is, I have price saved in USD in ACF field, which I want to convert on other Multiligual pages by manipulating the field using hook.

Hi @JuGa could you please post a full exemple i.cluding Cwii ly sttings and code php

1 Like

Hi, thank you for your patience, yesterday was Thanksgiving here, kept me luckily away from my computer.

Regarding manipulating ACF fileds before displaying them, here a little example:

So you have a php function that does all the changes you are looking for such as,
and yes, it can absolutely be added at a snipped through code snippets :

function change_my_field($post_id ) {
// get ACF field value through the $post_id
$my_local_field=get_field(ā€˜the_source_fieldā€™,$post_id);
// do all you have to do with it
$my_changed_field=$my_local_field*the_changes_I_want_to_operate_on_taht_field
//return it
return $my_changed_field;
}

Then within my page or template, within a text block (either parapraph or heading)
I would just call that function as follow:

{return=change_my_field(get_the_ID())}

and voila!

Do you think that would help with your project?
You still need now to get the the language of the page in order to get the correct currency but as I donā€™t know how you manage that information, hard to help on that partā€¦

Best,
J

2 Likes

Super @JuGa
I will try and keep you posted.
Merci

1 Like

Sorry but I canā€™t set this function.

Here is what I have done.

An image with the show condition:

2022-11-28_120707
{return=change_my_field(get_current_user_id())}

An image with the show condition:

2022-11-28_120841
{return=change_my_field(get_current_user_id())}

The function:

function change_my_field( $user_id ) {
	$my_local_field = true;
	return $my_changed_field;
}

Normally, at lest one of the image must be displayed: but nothing is displayed!

Can you help me to find where the mistake is.

well the function returns $my_changed_field that isnā€™t set anywhere
try to return $my_local_field instead or set
$my_changed_field=$my_local_field;

Sorry, I made a bad copy of my function.

The function that I tested is:

function change_my_field($user_id ) {
	$my_changed_field = true;
	return $my_changed_field;
}

and nothing is displayed: neither ā€œreturn=trueā€ nor ā€œreturn=falseā€

Great, it worked on the main language, but since Iā€™m using WPML, it is not working in other languages why?

Just checked, as I never used functions in conditions before,
well, in the conditions, you donā€™t need to add {return=change_my_field()}
because you chose function return in the first select menu, you just need the name of your function and arguments in the next field
such as change_my_filed()
I just tried and it seems to work

You want to use the {return=my_function()} to evaluate a function within a textfield directly.

Hope that will help.

2 Likes

I never used WPML and I understand you are looking to change the currency, depending of the language of the post, right?
So you need to check the language of the current post and apply your change in currency accordingly.

Here, I found a basic example of such for WPML

So in your php snippet youā€™ll have something like:

function My_Local_Currency($post_id){
//get the amount you want to convert
$amount_to_convert=get_field(ā€˜amountā€™,$post_id);
//get the language of current post
$post_language_information = apply_filters( ā€˜wpml_post_language_detailsā€™, NULL, $post_id );
$this_post_lang = $post_language_information[ā€œlanguage_codeā€];
//then apply the changes you want depending of the language
if($this_post_lang == ā€˜enā€™){
//do your currency conversion
$conversion_rate=3.2; }
elseif($this_post_lang==ā€˜deā€™){
//do your currency conversion
$conversion_rate=0.2; }
//convert the amount
$my_converted_amount=$amount_to_convert*$convertion_rate;
//return the converted value
return $my_converted_amount;
}

Then you can just call that function in your Cwicly textfield where you need it to display using the technique we discribed earlier.

Hope that helps.

Thank you @JuGa
Works fine!

2 Likes

:+1:
Super! Glad to help!

2 Likes