Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active March 3, 2023 02:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vielhuber/1124e0309bb4d655bfc08e1d38ad5bde to your computer and use it in GitHub Desktop.
Save vielhuber/1124e0309bb4d655bfc08e1d38ad5bde to your computer and use it in GitHub Desktop.
Advanced Custom Fields ACF dynamically set default value #wordpress
<?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;
});
@braddalton
Copy link

Thanks for this. I might be wrong but variant 1 works and variant 2 doesn't.

@vielhuber
Copy link
Author

Hello Brad, thanks for your feedback.
I just tried both versions successfully with WP 5.2.1 and ACF 5.8.1.

@pawelskaba
Copy link

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');

@dima8nep
Copy link

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;
});

@ful1to
Copy link

ful1to commented Apr 29, 2021

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;
});

@vielhuber
Copy link
Author

vielhuber commented Apr 29, 2021

@ful1to:

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!

@ful1to
Copy link

ful1to commented Apr 29, 2021

@ful1to:

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!

Thanks for your fast response. No luck at all unafortunately. It shows nothing.

@vielhuber
Copy link
Author

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?

@ful1to
Copy link

ful1to commented Apr 29, 2021

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.

@vielhuber
Copy link
Author

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;
});

@ful1to
Copy link

ful1to commented Apr 29, 2021

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?

@vielhuber
Copy link
Author

vielhuber commented Apr 29, 2021

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.

@ful1to
Copy link

ful1to commented Apr 30, 2021

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.

@ful1to
Copy link

ful1to commented Apr 30, 2021

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; 
});

@kaanaytemir
Copy link

kaanaytemir commented Aug 26, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment