• Resolved syoung68

    (@syoung68)


    A few months back, you showed me the meta_field_block_get_block_content filter. Wich I love. I recently implemeted the following

    
      if ( ('att-email' === $field_name ) && ($content != '')) {
        return '<a href="mailto:'.$content.'" title="Email"><i class="fa fa-envelope" aria-hidden="true"></i></a>';
      }
    

    But now I want to wrap name meta fields in a link. Is there a good way to do this? I can fudge it with multiple filters, but that does not seem like the RIGHT way to do it.

    e.g. I have First, Middle, and Last names as separate fields, but would like it to be like this

    return '<a href="'.esc_url(get_permalink()).'">'.$first.' '.$middle." ".$last.'</a>';

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hi @syoung68,
    Using a custom rest field is the right way in this case, here is the full working code.

    add_action(
    	'init',
    	function () {
    		register_meta(
    			'post',
    			'first_name',
    			array(
    				'show_in_rest' => true,
    				'type'         => 'string',
    				'default'      => 'First',
    				'single'       => true,
    			)
    		);
    
    		register_meta(
    			'post',
    			'last_name',
    			array(
    				'show_in_rest' => true,
    				'type'         => 'string',
    				'default'      => 'Last',
    				'single'       => true,
    			)
    		);
    	}
    );
    
    function yourprefix_get_full_name( $post_id ) {
    	 return sprintf( '<a href="%1$s">%2$s %3$s</a>', '#', get_post_meta( $post_id, 'first_name', true ), get_post_meta( $post_id, 'last_name', true ) );
    }
    
    add_action(
    	'rest_api_init',
    	function () {
    			register_rest_field(
    				'post',
    				'full_name',
    				array(
    					'get_callback' => function ( $post_array ) {
    						// NOTE: The parameter is an array and ID is in lower case.
    						return yourprefix_get_full_name( $post_array['id'] );
    					},
    					'schema'       => array(
    						'type' => 'string',
    					),
    				)
    			);
    	}
    );
    
    add_filter(
    	'meta_field_block_get_block_content',
    	function ( $content, $attributes, $block, $post_id ) {
    		$field_name = $attributes['fieldName'] ?? '';
    
    		if ( 'full_name' === $field_name ) {
    			return yourprefix_get_full_name( $post_id );
    		}
    
    		return $content;
    	},
    	10,
    	4
    );

    After putting that snippet into the right place, in the editor set the field type to ‘custom field’ and the field name is ‘full_name’.
    This is my favorite way to use the MFB block. It’s not the same as your requirements but hopes you will get the idea. You can adjust it a bit to fit it in your case.

    Phi.

    Thread Starter syoung68

    (@syoung68)

    Thank you. I wound up just registering a Full Name as well as First and Last. It is a little redundant, but it give me the most flexibility in the end. THat way I can have “Dr. John K. Smith III” as full name and “John” as first and “Smith” as last. I really only use first and last for sorting purposes.

    As always you are super helpful. Thanks again.

    Plugin Author Phi Phan

    (@mr2p)

    You’re welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Wrap two or more blocks in a single link’ is closed to new replies.