-
-
Save vielhuber/1124e0309bb4d655bfc08e1d38ad5bde to your computer and use it in GitHub Desktop.
<?php | |
add_filter('acf/load_field/name=field_name', function($field) { | |
// variant 1 | |
$field['default_value'] = 'FOO'; | |
// variant 2 (does not work anymore) | |
// use get_post_meta instead of get_field to avoid conflicts | |
global $post; | |
if( get_post_meta($post->ID, 'field_name', true) == '' ) { | |
$field['value'] = 'FOO'; | |
} | |
// if field should be disabled | |
$field['disabled'] = 1; | |
return $field; | |
}); |
Hello Brad, thanks for your feedback.
I just tried both versions successfully with WP 5.2.1 and ACF 5.8.1.
Hi thanks for the snippet. Got a question - I've created sth like that but seems not to work:
function my_acf_load_field( $field ) {
global $post;
if( get_post_meta($post->ID, 'month_year', true) == '' ) {
$current_date = get_sub_field('data');
$trimmed = substr($current_date, -7);
$field['value'] = $trimmed;
}
$field['disabled'] = 1;
return $field;
}
add_filter('acf/load_field/name=month_year', 'my_acf_load_field');
I found how to set shortcote in default value.
Add in functions.php
If your shortcode have '' in [] and no "";
Use the plugin "Shortcoder" https://wordpress.org/plugins/shortcoder
add_filter('acf/load_field/name=latitude', function($field) {
$field['default_value'] = '' . do_shortcode('[sc name="latitude"]') . '';
return $field;
});
add_filter('acf/load_field/name=longitude', function($field) {
$field['default_value'] = '' . do_shortcode('[sc name="longitude"]') . '';
return $field;
});
Using this i was trying to set a value with another function. But it doesnt work
function lf_get_price(){
$var_id = get_field('id_producto_lifehuni', $post_id);
$ch_lf = curl_init('https://app.milifehuni.com/public/ws/get_price/'.$var_id);
curl_setopt($ch_lf, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_lf, CURLOPT_HEADER, 0);
$lf_data = curl_exec($ch_lf);
curl_close($ch_lf);
echo $lf_data;
}
add_filter('acf/load_field/name=precio_lifehuni', function($lf_field) {
global $post;
// variant 1 (use get_post_meta instead of get_field to avoid conflicts)
if( get_post_meta($post->ID, 'precio_lifehuni', true) == '' ) {
$lf_field['value'] = lf_get_price();
$lf_field['default_value'] = lf_get_price();
}
return $lf_field;
});
Try this (I used ['default_value']
, since this is currently working.
Also don't forget to pass the post id to lf_get_price
and return something back):
<?php
function lf_get_price($post_id) {
$var_id = get_field('id_producto_lifehuni', $post_id);
$ch_lf = curl_init('https://app.milifehuni.com/public/ws/get_price/'.$var_id);
curl_setopt($ch_lf, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_lf, CURLOPT_HEADER, 0);
$lf_data = curl_exec($ch_lf);
curl_close($ch_lf);
return $lf_data;
}
add_filter('acf/load_field/name=precio_lifehuni', function($lf_field) {
global $post;
$lf_field['default_value'] = lf_get_price($post->ID);
return $lf_field;
});
Good luck!
Try this (I used
['default_value']
, since this is currently working.
Also don't forget to pass the post id tolf_get_price
and return something back):<?php function lf_get_price($post_id) { $var_id = get_field('id_producto_lifehuni', $post_id); $ch_lf = curl_init('https://app.milifehuni.com/public/ws/get_price/'.$var_id); curl_setopt($ch_lf, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch_lf, CURLOPT_HEADER, 0); $lf_data = curl_exec($ch_lf); curl_close($ch_lf); return $lf_data; } add_filter('acf/load_field/name=precio_lifehuni', function($lf_field) { global $post; $lf_field['default_value'] = lf_get_price($post->ID); return $lf_field; });
Good luck!
Thanks for your fast response. No luck at all unafortunately. It shows nothing.
Does this work in your case with $lf_field['default_value'] = 'FOO'
?
If not: Which version of WordPress and ACF are you using and of which type is the field precio_lifehuni
?
Does this work in your case with
$lf_field['default_value'] = 'FOO'
?If not: Which version of WordPress and ACF are you using and of which type is the field
precio_lifehuni
?
Yes, if i put plain text there, it works perfectly. But functions or variables, dont. WP Versión 5.7.1 ACF 5.9.5 and precio_lifehuni
is number type.
Does this work?
function lf_get_price($post_id) {
return 42;
}
add_filter('acf/load_field/name=precio_lifehuni', function($lf_field) {
global $post;
$lf_field['default_value'] = lf_get_price($post->ID);
return $lf_field;
});
Does this work?
function lf_get_price($post_id) { return 42; } add_filter('acf/load_field/name=precio_lifehuni', function($lf_field) { global $post; $lf_field['default_value'] = lf_get_price($post->ID); return $lf_field; });
That worked. What you discovered?
This portion of your code simply returns nothing.
$var_id = get_field('id_producto_lifehuni', $post_id);
$ch_lf = curl_init('https://app.milifehuni.com/public/ws/get_price/'.$var_id);
curl_setopt($ch_lf, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_lf, CURLOPT_HEADER, 0);
$lf_data = curl_exec($ch_lf);
curl_close($ch_lf);
return $lf_data;
This has nothing to do with ACF / default values, you have to check, why curl doesn't return anything.
This portion of your code simply returns nothing.
$var_id = get_field('id_producto_lifehuni', $post_id); $ch_lf = curl_init('https://app.milifehuni.com/public/ws/get_price/'.$var_id); curl_setopt($ch_lf, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch_lf, CURLOPT_HEADER, 0); $lf_data = curl_exec($ch_lf); curl_close($ch_lf); return $lf_data;
This has nothing to do with ACF / default values, you have to check, why curl doesn't return anything.
Yeah, doing some testing here aswell it looks like the curl its not running when its called or its data is not being captured. Have no idea how, why or what to do.
Got it to work, actually your update to the post helped me a lot. But the Disabled, didnt work. Thank you so much, you relieved me 3 days of full stress... Now the problem is that it takes time to load the pages.
<?php
add_filter('acf/load_value/name=precio_lifehuni', function($field) {
global $post;
$var_id = get_post_meta($post->ID, 'id_producto_lifehuni', true);
$ch_lf = curl_init('https://app.milifehuni.com/public/ws/get_price/'.$var_id);
curl_setopt($ch_lf, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_lf, CURLOPT_HEADER, 0);
$lf_data = curl_exec($ch_lf);
curl_close($ch_lf);
return $lf_data;
get_post_meta($post->ID, 'precio_lifehuni', true);
$field['default_value'] = $lf_data;
$field['disabled'] = 1;
return $field;
});
For the ones who are looking for a solution:
<?php
add_filter('acf/load_field/name=field_name', function($field) {
// variant 1
$field['default_value'] = 'FOO';
// variant 2 (does work with most recent ACF plugin)
if( get_post_meta($post->ID, 'field_name', true) == '' ) {
$field['value'] = 'FOO';
}
return $field;
});
This code works when you remove global $post;
.
Most probably you are already setting this, so it will give a Cannot Modify Header Information – Headers Already Sent
error. That error will not show the text you would like to fetch.
Thanks for this. I might be wrong but variant 1 works and variant 2 doesn't.