WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Kenneth Jensen
    Member
    Posted 1 year 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.

    Example:
    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?

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. refreshingdesign
    Member
    Posted 11 months ago #

    Did you ever find a way to do this?

  3. Kenneth Jensen
    Member
    Posted 11 months ago #

    No, sadly not.

  4. Ov3rfly
    Member
    Posted 11 months 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

About this Topic

Tags