Advanced Custom Fields
Allow PHP in field 'Default Value' (4 posts)

  1. Kenneth Jensen
    Posted 3 years ago #

    Hi there

    I'm using ACF and i'm quite impressed. But one major feature request would be to allow PHP in the Default Value field.

    I need a custom field with the post's slug as default value, and this would be easy to fill in with something like

    <?php $slug = $post->post_name;
    echo $slug;>

    Could you please consider this?


  2. refreshingdesign
    Posted 3 years ago #

    Did you ever find a way to do this?

  3. Kenneth Jensen
    Posted 3 years ago #

    No, sadly not.

  4. Ov3rfly
    Posted 3 years ago #

    You could use a filter of ACF via your theme functions.php, something like this:

    A field named my_field, insert name at name= in add_filter() line.

    A default value of php_default_hack for that field.

    function my_acf_load_value( $value, $post_id, $field ) {
    	if ( $value == 'php_default_hack' ) {
    		// do your php magic here...
    		$value = 'this is set from php';
    	return $value;
    add_filter( 'acf/load_value/name=my_field', 'my_acf_load_value', 10, 3 );

    The code does this: If your field has the value php_default_hack during load, the value will be replaced by this is set from php.

    See also http://www.advancedcustomfields.com/resources/filters/acfload_value/

    For your example, note that a new created post has no slug = post_name yet.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Advanced Custom Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic