Default end-of-post text in Gutenberg (how?)
-
?Before Gutenberg, I modified function my_editor_content($content) in ?functions.php to pre-fill a post with text I want at the bottom of each post, effectively creating a default template for new posts. (Invicta theme)
After Gutenberg, that doesn’t work so well, because that text is in the bottom of the first block. New blocks then come after the boilerplate, instead of before.
So when a post is created, I now need two blocks. One at the top with the summary, and one at the bottom with ending text.
How (or when) do I do that, with my existing theme and Gutenberg?
-
This topic was modified 6 years, 3 months ago by
Steven Stern (sterndata).
-
This topic was modified 6 years, 3 months ago by
-
In either editor, you would select where you input the post content (before the default template). The block inserter can insert before a block, can’t it?
And remember you can use the Classic Editor plugin if you prefer it.
I can, of course, avoid the block editor altogether, but my preference
would to be take advantage of the functionality it offers. The question
is to provide functionality I’m already taking advantage of with the
new tool.Here is the functions.php code:
function my_editor_content( $content ) {
$content = <<<EOD
[blockquote]…summary goes here…[/blockquote]
[!–more–]
[p]…content goes here…[/p]
[p]…Copyright notice, etc…[/p]
EOD;
return $content;
}The generated text goes into a single block. The “block inserter”
can be inserted before or after, but the goal is to split the
block in two, so new blocks can be added in the “content” area.That could be done with a manual “split block” operation, or better,
with code in the function that would generate two blocks instead of one.Nope. The new editor doesn’t change how content is output. You need to be more clear in your explanation, because
the_content
still behaves the same as before.The new editor saves HTML. So did the old editor. So, what exactly is different here?
I am not talking about how content is output.
Nothing about that has changed.My ISP upgraded to 5.0, which is fine. So I’m figuring it out now.
I do see that the generated text is shown in the classic editor,
when I edit — because it contains more than one “block”.In the “…” menu, there is an option to “Convert to Blocks”.
Doing that puts each section in its own block. That’s more than
I wanted, but it will work. It gives me the option of using blocks
when I went them.It would have been nice to generate content that was in two or
three main editor-blocks, though…-
This reply was modified 6 years, 3 months ago by
Eric Armstrong.
-
This reply was modified 6 years, 3 months ago by
Eric Armstrong.
You could try creating that template content in Gutenberg using blocks, then switch from the visual editor to the code editor (from the ‘three dots’ menu in the top right corner of the editor), and finally use that markup in your function. It should result in blocks being created in your new post (the comments in the markup delineate the blocks).
There’s also another feature in Gutenberg called Reusable Blocks which might be interesting to you. If you, again, create your template in Gutenberg using blocks, select the blocks, then from the block settings menu (‘three dots’ at the top of the first select block), choose ‘Add to reusable blocks’. Give it an easy to remember name (e.g. ‘New Post Template’). Now when you start a new post you can insert the reusable block (fastest way is using the slash inserter by typing ‘/New Post Template’ in a new paragraph and hitting enter). If you want to edit it, you can use ‘Convert to Blocks’ to make it editable. Reusable blocks are really like creating a stamp from content.
Granted, reusable blocks are not as automatic as your existing way, but it might also be useful for other content you want to reuse across posts.
-
This reply was modified 6 years, 3 months ago by
Daniel Richards.
-
This reply was modified 6 years, 3 months ago by
Daniel Richards.
AHA! There it IS.
The three dots at top right of the entire window do indeed reveal a “Code Editor” option. (I had only seen the three dots on each block.)
You’re right. I can create two blocks, then go to the code editor and use that code to prime the pump. (HTML comments delineate the start and end of blocks, so the process should work quite well.)
Thanks!
Huzzah! It worked.
It’s pretty brittle. Several options I tried either didn’t work,
hung when attempting to preview, or hung when attempting to save.This is the winning entry:
[blockquote]
[p]summary[/p]
[/blockquote]
[p][!–more–][/p][!– wp:paragraph {“className”:”copyr”} –]
[p]Copyright notice, etc.[/p]
[!– /wp:paragraph –]The key was to move class=”copyr” from the [p] tag to the wp:paragraph directive.
That produced the desired results in the editor.
Next: How does it work when generated in functions.php?Never mind. It fails.
It seems to work, at first.
The editor shows this:
[!– wp:paragraph {“className”:”copyr”} –]
[p class=”copyr”]…But the preview of that option simply hangs.
Removing the inner p-class designation sort of works:
[!– wp:paragraph {“className”:”copyr”} –]
[p]…It previews properly. But the next time you edit, the classname
has been removed, leaving this:
[!– wp:paragraph –]
[p]…So the text is there, but the styling is gone.
You can try to put it back:
[!– wp:paragraph –]
[p class=”copyr”]…But that just takes you around in circles. The next time you
look at it, you’re back to
[!– wp:paragraph {“className”:”copyr”} –]
[p class=”copyr”]…That’s where we came in…
There is another way to do this, which is probably the right way for blocks. Gutenberg has a concept of block templates. More about that here:
https://www.ads-software.com/gutenberg/handbook/designers-developers/developers/block-api/block-templates/You can assign a template to be the default for a particular post type, specifying the blocks you want within that template. The tricky bit is that you need to figure out what the attributes are for the blocks. Here’s what I attempted for your content (this should replace your existing function):
function my_add_template_to_posts() { $post_type_object = get_post_type_object( 'post' ); $post_type_object->template = array( array( 'core/quote', array( 'value' => '<p>...summary goes here</p>', ), ), array( 'core/more', ), array( 'core/paragraph', array( 'content' => '...content goes here', ), ), array( 'core/paragraph', array( 'className' => 'copyr', 'content' => '…Copyright notice, etc…', ), ), ); } add_action( 'init', 'my_add_template_to_posts' );
When I tried this, it seemed to work correctly and the class name was preserved.
-
This reply was modified 6 years, 3 months ago by
Daniel Richards.
-
This reply was modified 6 years, 3 months ago by
Daniel Richards.
Nice! Thanks. Dang, that’s clever.
Where did you find the API details?P.S.
MY existing posts appear to be completely un-editable in the “classic” editor.
I can see the first 50 lines or so, but I haven’t found a way to scroll beyond that.
With 600 articles, that’s going to force me to convert anything I want to edit into tiny, bite-sized blocks in order to do so. Ugh.That’s not the case. The Classic Editor didn’t change. You should still be able to scroll. If you really can’t, open a support topic on the plugin’s forum.
Joy, you are absolutely right. I was totally wrong.
It turns out that three of my plug-ins put option-blocks below what used to be the edit-window. In 5.0, those option blocks appear in the middle of the edit-block, after 50 lines or so.
I found that out when I converted to blocks, and still couldn’t see the remainder of my text.
When I minimized the Yoast SEO, Ad Inserter, and Table of Contents windows, they small windows were there in the middle of my post, but the remainder of my post was visible (and editable) below them!
Stumbled across the ability to create a new topic once again.
The key is to skip past the “Welcome to Support” link (so appealing, so right there…)
and go down to one of the “View Forum” links. That takes you to an actual forum, where you can review and create topics!(Writing these notes for myself, so I’ll remember them!)
-
This reply was modified 6 years, 3 months ago by
Eric Armstrong.
> Where did you find the API details?
My example was lifted pretty gratuitously from the one in the handbook:
For finding the block attributes I had to look through the Gutenberg codebase.
The block-library folder contains each of the blocks. Under the subfolders for each block there’s an index.js file that contains the specification for each block, part of that is the attributes object (sometimes also called schema). Some examples:
– https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/paragraph/index.js#L34-L73
– https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/quote/index.js#L20-L40
– https://github.com/WordPress/gutenberg/blob/master/packages/block-library/src/more/index.js#L41-L49There is an issue that tracks adding documentation for blocks, so hopefully that will be resolved at some point in the near future and it’ll make this easier:
https://github.com/WordPress/gutenberg/issues/6190-
This reply was modified 6 years, 3 months ago by
Daniel Richards.
-
This reply was modified 6 years, 3 months ago by
Daniel Richards.
Awesome. Thanks!
-
This reply was modified 6 years, 3 months ago by
- The topic ‘Default end-of-post text in Gutenberg (how?)’ is closed to new replies.