Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to blocks in PHP #13163

Closed
fisicx opened this issue Jan 2, 2019 · 11 comments
Closed

Convert to blocks in PHP #13163

fisicx opened this issue Jan 2, 2019 · 11 comments
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@fisicx
Copy link

fisicx commented Jan 2, 2019

A client creates posts from a CSV using a plugin that calls the wp_insert_post function.

The posts are created but all the content is in a classic editor block.

Can we have a function that automatically converts classic content into blocks. Or if there is already a way to do this programmatically where can I find the documentation?

@swissspidy swissspidy changed the title wp_insert_post - building blocks Convert to blocks in PHP Jan 2, 2019
@swissspidy swissspidy added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Jan 2, 2019
@pbrocks
Copy link

pbrocks commented Jan 3, 2019

@fisicx Thanks for the question and the opportunity to reflect on this. I believe the key point here is that a Post is still a Post and a Block is a smaller entity that is a constituent of a Post. The automatic conversion that you are referring to represents the designation of a paragraph with the Classic Block. Make sense? That is, if a post that your client is importing has 5 paragraphs, then the editor should contain 5 classic blocks after import.

Having said that, once a post is imported and living in the WordPress database, you may choose to pull that post into a block that, for example, pulls in the latest posts for display in one area of a post or page. But going back to your original question about import, if you have a desired outcome, you could wrap the csv import in the appropriate html comments to render whatever blocks you like.

@swissspidy
Copy link
Member

If you have a desired outcome, you could wrap the csv import in the appropriate html comments to render whatever blocks you like.

I believe OP is asking for a the same "Convert to blocks" functionality we already have in JavaScript. In other words, a function that automatically adds these HTML comments.

At the moment such a function does not exist. Mainly because block types are only registered in JavaScript and on the server you have no idea which blocks exist.

However, server-side block registration is planned as per #2751. Once that is done, we will be able to provide such a function for developers.

@fisicx
Copy link
Author

fisicx commented Jan 3, 2019

I have looked at adding the html comments to the content but it means I've got to do this for all possible elements. And this seems redundant when there is already a function to convert classic to blocks. I can go to 'More Options > Convert to Blocks'. I just want to be able to call a function like this when the post is created. Rather than go through each of the 100+ custom post types they import from the CSV.

The people importing from a CSV are OK adding HTML tags and attributes, if I tell them they have got to learn a whole new way of doing things they won't be happy. It's this loss of simple content creation that is causing so many problems. Even more so for the thousands of plugins that use shortcodes attributes to manage content.

@swissspidy
Copy link
Member

And this seems redundant when there is already a function to convert classic to blocks. I can go to 'More Options > Convert to Blocks'.

That is a client-side JavaScript function, nothing that is available in PHP when importing from CSV. As I said, there is no such PHP function at the moment but this will likely be added in the future.

@fisicx
Copy link
Author

fisicx commented Jan 3, 2019

Thanks for that. I will be patient.

In the meantime I will have to do some basic str_replace on the more common elements.

@youknowriad
Copy link
Contributor

If you're adventurous, I think you can still use a node script running jsdom and use the wp.blocks.rawHandler function. Otherwise, you should wait for phase 2 work around server side awareness of blocks #2751

@youngmicroserf
Copy link

This might help. Came accross it while researching this issue.

https://wordpress.stackexchange.com/questions/343037/how-to-serialize-a-gutenberg-block-from-block-attributes-in-php

@micahjon
Copy link

micahjon commented Sep 5, 2019

Just wanted to voice my support for this issue!

In the meantime, I wrote a script that automatically opens every WP post and converts it to Gutenberg by clicking the buttons on the Edit Post page. Gross, I know, but it worked for the 900 posts on our WP site.
https://gist.github.com/micahjon/6573f3c8c2424556364250e04a249937

@chaselangr
Copy link

Just wanted to voice my support for this issue!

In the meantime, I wrote a script that automatically opens every WP post and converts it to Gutenberg by clicking the buttons on the Edit Post page. Gross, I know, but it worked for the 900 posts on our WP site.
https://gist.github.com/pranksinatra/6573f3c8c2424556364250e04a249937

is there a way to hook this action onto the publish / update action so that it just automatically does it for new posts?

@evrpress
Copy link
Contributor

Until this is possible in core you should check out convert-to-blocks by @10up

@danieliser
Copy link
Contributor

Why are these all closed, and after 5 years still no method to solve this, yet they still expect everybody to happily jump on the Gutenberg train like these major gaps in coverage don't exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

9 participants