Having default content saves you time and creates consistency through your site.
After working with WordPress for 13+ years, I finally figured out how to add default content to the post editor. I use this technique on my site to create those polished intros. When I start a new post, the intro is already there, ready to go.
function add_default_post_content($content, $post) {
if ($post->post_type === 'post' && empty($content)) {
return '<!-- wp:pattern {"slug":"ch/post-intro"} /-->';
}
return $content;
}
add_filter('default_content', 'add_default_post_content', 10, 2);In this example I am loading a pattern called “post-intro” at the top and it is only on posts that do not have content already.
If you want to instead include blocks directly:
function add_default_post_content($content, $post) {
if ($post->post_type === 'post' && empty($content)) {
return '<!-- wp:heading {"level":2} -→
<h2>Your Default Heading</h2>
<!-- /wp:heading -→
<!-- wp:paragraph -→
<p>This is your default post content.</p>
<!-- /wp:paragraph -→';
}
return $content;
}
add_filter('default_content', 'add_default_post_content', 10, 2);AI to the rescue
This wasn’t my first attempt at solving this problem. I’d tried something similar before but hadn’t been Googling the right terms.
This time, I decided to try a different approach: I asked AI to help me code a custom solution.
Imagine my surprise when it came back with just a few lines of code as the answer.
The AI took the context I provided and found exactly what I needed. I wish I could remember what search terms I’d been using before, it would make this story even better but I can’t remember.
AI for learning
One thing I truly love about working with AI coding tools is that you can ask questions naturally, using your current level of knowledge. When you Google something but you’re missing the proper technical terminology, you might never find your answer.
AI is different. It looks for context. It can extract meaning from your description, understand what you’re actually trying to accomplish, and then provide the right solution.
AI tools can help you bridge gaps you didn’t even know existed.
Get Nerd Notes
My wins, flops, & any XP earned.
No Spam. Exit anytime. See you there!