• Resolved pedjas

    (@pedjas)


    I have created some custom fields, like image and url on User.

    I want to display those custom fields values at User page. I have Template for user but when I add Meta Field Block in template and set it up for custom fields it shows nothing.

    If I set text for Empty message, that text shows up instead of the value. It seems like I am missing setting up reference to custom field properly so it cannot find it so it just says that there is no value.

    I tried all three field types (default ‘meta; ACF field and Custom Rest Field.

    How should I reference to custom filed to actually get its value in place of Meta Filed Block?

    I did a test. I created filter on ‘meta_field_block_get_block_content’ to check what is actually visible at the point where custom filed is displayed.

    It seems $post_id refers to the first post in User archive, not User itself, even if Custom Rest Field block is positioned outside of QueryLoop. As post is nt a user, it is understandable why custom fields are not found – they are on User not on a Post.

    But how to access User custom fields?

    • This topic was modified 1 year, 9 months ago by pedjas.
    • This topic was modified 1 year, 9 months ago by pedjas.
    • This topic was modified 1 year, 9 months ago by pedjas.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hello @pedjas,

    Thank you for your feedback.

    To display custom user fields, you have to add a filter on meta_field_block_get_block_content. Here is an example code:

    // Create a function to build the value for the field.
    function yourprefix_get_user_field( $user_id ) {
      $user_field_value = get_user_meta( $user_id, 'your_field_name', true );
    
      // If the meta field is an ACF Field. The code will be:
      // $user_field_value = get_field( 'your_field_name', 'user_' . $user_id );
    
      return $user_field_value;
    }
    
    // Render the block on the front end.
    add_filter( 'meta_field_block_get_block_content', function ( $block_content, $attributes, $block, $post_id ) {
      $field_name = $attributes['fieldName'] ?? '';
    
      // Replace your_field_name with your unique name.
      if ( 'your_field_name' === $field_name && is_author() ) {
        $user_id = get_queried_object_id();
        $block_content = yourprefix_get_user_field( $user_id );
      }
    
      return $block_content;
    }, 10, 4);

    I have not tested the above snippet, but I think it should work for your use case. Please try it yourself and see if it works. If you still need more help, please provide more details about your issue like WP version, theme, MFB version, ACF version.

    Thanks, Phi.

    Plugin Author Phi Phan

    (@mr2p)

    Hello, @pedjas. I have tried the snippet myself and it works perfectly for me. Could you please confirm if it works for you as well? If it does not, feel free to discuss further the issue, I’m happy to help.

    Thanks, Phi.

    Thread Starter pedjas

    (@pedjas)

    Interesting, this is second time I noticed that I posted reply, but it vanished later.

    Yes, 'user_' . $user_id did the trick. I god user ID differently but it got me to the solution.

    Thanks for help.

    Thread Starter pedjas

    (@pedjas)

    Now, I have a question. What is point of Meta Field Block plugin when all it does it making placeholder and then all has to be done by coding?

    I expected I can simply add MFB block and configure what to display at its place, not to have to create content by custom programming.

    What is advantage of MFB against using short_codes?

    Plugin Author Phi Phan

    (@mr2p)

    To display post meta fields, you do not need to add any custom code. However, to display term fields, user fields, or global setting fields, you need to add custom code to show their values. Most of the time, 90% of users only need to display post meta fields.

    In my opinion, using Gutenberg blocks is much better than the old shortcode for non-technical users, so blocks like MFB are helpful for them.

    Thread Starter pedjas

    (@pedjas)

    I completely agree, Blocks are much better way – if they work.

    In my case, the only thing I did not need custom fields for are posts, so I had to do all manually.

    I do have experience writing code for WordPress, but not Blocks. That is why I found your plugin promising.

    I am now experimenting with getting custom fields from any kind of post, but I use short code as I am familiar with it.

    However, as Meta Custom blocks is Open Source, if I get to viable solution that would work with more post types than just standard posts, I am willing to share code with you to use it to enhance MFB.

    It would be great if MFB could be made able to deal with all kind of content.

    Plugin Author Phi Phan

    (@mr2p)

    @pedjas This block supports all kinds of post types. You don’t need to do anything else for other post types.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display custom field for a User?’ is closed to new replies.