1

How do I pass author details such as name, bio and avatar to a block?

3
  • 1
    Is this for saving or display? I'd keep in mind that you could get what you asked for but have the problem that changing the author doesn't update the block if you're not careful
    – Tom J Nowell
    Jul 3, 2019 at 21:18
  • For display I would rather the block only get this information rather than have a user pick it that way it's controlled from wp itself. So if i create the block it gets the name and bio of the post author of the post but if I change it from the post edit screen it updates to the new author if that makes sense.
    – Astrid
    Jul 4, 2019 at 8:41
  • doing that will have major problems with blocks showing out of date information or incorrect information if done via JS in the editor UI
    – Tom J Nowell
    Jul 17, 2023 at 10:48

1 Answer 1

0

How do I pass author details such as name, bio and avatar to a block?

You don't.

This would break quick edit/site editor/block themes/synced patterns/etc, and cause a lot of problems. A block that relies on post author information or any other dynamic post value must be a PHP server rendered block.

This is because we don't know the correct value until the moment we render it, and the answer changes depending on where the block is being displayed/edited.

Doing it in javascript is a mistake and will cause problems with both out of date authors, and broken blocks.

Use register_block_type to register the block in PHP, and specify a PHP function to render the blocks output using the render_callback option and use standard author PHP functions such as the_author etc.. You can also use PHP filters to swap out the author at runtime so that it's always the post author.

Then, in the blocks JS implementation, return null in the save method. You can retrieve the current author from the WP Core data store for previewing purposes when inside the block editor, but it isn't necessary for it to work on the frontend

So you would never need or want to find out the current author in the javascript of your block.

Why You Can't Use Core Data in the edit component.

Ok so how do I call the current author using core/data ?

You don't, that would break your block especially if you tried to use it for a save component. Render it in PHP, since we don't know what the value should be until the block is shown on the frontend.

For example, lets say that we displayed it inside the edit component using core data. Now all Query loop blocks will incorrectly show the same author regardless of which post.

If you did it in the site editor or template editor, then your block would show the author of the template, not the post.

7
  • Ok so how do I call the current author using core/data ?
    – Astrid
    Jul 4, 2019 at 14:34
  • Eh I'm not sure how to do that off the top of my head, but it shouldn't be needed to get it functional on the frontend
    – Tom J Nowell
    Jul 4, 2019 at 14:52
  • @Astrid Came across this question while working on something similiar and for anyone else who finds this, you can get current author via wp.data.select('core/editor').getCurrentPostAttribute('author');
    – S.Walsh
    Sep 19, 2021 at 4:21
  • wp.data.select('core/editor').getCurrentPostAttribute('author') => undefined for me 6.2.2
    – Kropotkin
    Jul 17, 2023 at 9:17
  • note that those snippets will not work if the block is in the site editor, inside a query loop block, if the author is changed using quick edit, if the block is inside a reusable block/synced pattern, or in a theme template
    – Tom J Nowell
    Jul 17, 2023 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.