Support

Account

Home Forums ACF PRO Does "post_object" field go through "acf/render_field" twice? Reply To: Does "post_object" field go through "acf/render_field" twice?

  • Thank you for more information! @hube2

    After coming 2 know the situation behind, should I report a bug? or stay silent?

    PS
    My code to avoid the double echo is something like this below.

    
    class MyAddExtraInfoToACFField {
    	const EXTRA_INFO_KEY = 'my_extrainfo';
    	public $show_extrainfo_count;
    	public function __construct() {
    		$this->show_extrainfo_count = [];
    		add_action( 'acf/render_field_settings', [ $this, 'add_extrainfo_to_acf' ], 99999 );
    		add_action( 'acf/render_field', [ $this, 'show_extrainfo_to_acf' ], 9, 1 );
    	}
    	public function load_css_js( $hook ) {
    		if ( 'post.php' === $hook || 'post-new.php' === $hook ) {
    			wp_enqueue_style( __CLASS__, plugins_url( 'style.css', __FILE__ ) );
    			wp_enqueue_script( __CLASS__, plugins_url( 'script.js', __FILE__ ) );
    		}
    	}
    	public function add_extrainfo_to_acf( $field ) {
    		$value = ( isset( $field[ self::EXTRA_INFO_KEY ] ) ) ? $field[ self::EXTRA_INFO_KEY ] : '';
    		acf_render_field_wrap( [
    			'label'            => 'my_extrainfo',
    			'instructions'    => 'Write an extra info to this item.',
    			'required'        => 0,
    			'type'            => 'textarea',
    			'name'            => self::EXTRA_INFO_KEY,
    			'prefix'        => $field['prefix'],
    			'value'            => $value,
    			'class'            => 'field-' . self::EXTRA_INFO_KEY,
    		], 'tr' );
    	}
    	public function show_extrainfo_to_acf( $field ) {
    		global $typenow, $pagenow;
    		if ( 'acf-field-group' === $typenow ) { return; }// avoid an error
    		if ( 'post.php' !== $pagenow  && 'post-new.php' !== $pagenow ) { return; }
    		// check if it's second run
    		if ( isset( $this->show_extrainfo_count[ $field['key'] ] )  ) {
    			return;
    		}
    		echo $field[ self::EXTRA_INFO_KEY ];
    		$this->show_extrainfo_count[ $field['key'] ] = true;
    	}
    }
    $my_add_extrainfo_to_acf_field = new MyAddExtraInfoToACFField();