alt-design / alt-blog
The logic behind the Alt blog starter kit: feed, structured data, related posts, table of contents, social images and control panel help
Requires
- php: ^8.3
- statamic/cms: ^6.44
Requires (Dev)
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.1
- pestphp/pest: ^4.4
- pestphp/pest-plugin-laravel: ^4.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The logic behind the Alt blog starter kit for Statamic 6. It gives a blog an RSS feed, structured data, related posts, a table of contents, link preview images, sensible indexing for tag pages, a default author for new posts, dashboard widgets and an editor's guide in the control panel.
The kit's templates are copied into a site once. This addon is what keeps getting fixed afterwards: it updates with composer update, like our other addons.
Requirements
- Statamic 6.44 or later
- PHP 8.3 or later
- Alt SEO, for the link preview image and tag page indexing
Installation
The blog starter kit installs this addon for you. To add it to another site:
composer require alt-design/alt-blog
The addon expects the kit's content model: a dated posts collection, an authors collection, categories and tags taxonomies, and blog_settings and brand globals, with the field handles the kit's blueprints use. If your handles differ, publish the config:
php artisan vendor:publish --tag=alt-blog-config
In templates
Structured data
Put this once in the layout's <head>:
{{ alt_blog:schema }}
It works out what the page is and writes one JSON-LD script:
| Page | Structured data |
|---|---|
| A post | BlogPosting, with its authors as Person, the site as publisher, and breadcrumbs |
| An author | Person and breadcrumbs |
| The home page | WebSite and Organization |
| A category or tag | Breadcrumbs through the blog |
| Any other page | Breadcrumbs through its parent pages |
Anything a page does not have is left out rather than written empty. To turn it off on one template, add this anywhere in that template:
{{ section:alt_blog_schema }}off{{ /section:alt_blog_schema }}
Feed link
The feed is served at /feed. Link it from the layout's <head> so feed readers find it:
{{ alt_blog:feed }}
Table of contents
Add ids to the post's headings with the heading_ids modifier, and list them with the toc tag. Both work out the ids the same way, so the links always match.
{{ if show_toc }}
<nav aria-label="Contents">
<ol>
{{ alt_blog:toc :content="content" }}
<li class="level-{{ level }}"><a href="#{{ id }}">{{ text }}</a></li>
{{ /alt_blog:toc }}
</ol>
</nav>
{{ /if }}
{{ content | heading_ids }}
{{ if type == 'text' }}
{{ text }}
{{ else }}
{{ partial src="sets/{type}" }}
{{ /if }}
{{ /content }}
Each heading has id, text (plain text, already decoded) and level (2, 3 or 4). Statamic's own loop variables, such as count, first and last, work as usual. H1, H5 and H6 are left alone.
Run the modifier on the whole field, not on each block, so a heading repeated in two blocks still gets a unique id.
Related posts
{{ alt_blog:related }}
{{ if no_results }}
...
{{ else }}
<a href="{{ url }}">{{ title }}</a>
{{ /if }}
{{ /alt_blog:related }}
Posts the editor picked under Related posts come first, in their order. The rest are filled in automatically: posts in the same category, then posts sharing the most tags, newest first. Drafts, scheduled posts and the post itself are never included. The number shown is Related posts to show in Blog settings; limit="2" overrides it, and as="posts" gives you the list as one variable.
Link preview image and tag page indexing
Nothing to add. On a post, the featured image is cropped to 1200 by 630 pixels around its focal point and used for og:image and twitter:image. On a tag page with fewer published posts than "Posts a tag needs before search engines list its page" in Blog settings, the robots meta becomes noindex, follow. A threshold of 0 never hides a tag page.
Both are set through Alt SEO's template sections (alt_seo_og_image, alt_seo_twitter_image and alt_seo_robots) before the page renders. A post with its own social image in Search and sharing keeps it, and a template that writes one of those sections itself still wins.
Alt Sitemap still lists thin tag pages. Exclude them there if you need to.
In the control panel
Dashboard widgets
In config/statamic/cp.php:
'widgets' => [ ['type' => 'blog_welcome', 'width' => 100], ['type' => 'blog_posts', 'status' => 'draft', 'width' => 50], ['type' => 'blog_posts', 'status' => 'scheduled', 'width' => 50], ['type' => 'blog_posts', 'status' => 'published', 'width' => 100], ],
blog_welcome greets the editor with three first steps, a Write a post button for those allowed to create posts, and a link to the editor's guide. blog_posts lists drafts, scheduled posts or recently published posts; limit (default 5) and title can be set. People who cannot view posts do not see the post lists.
Editor's guide
A plain English guide to writing, scheduling and looking after the blog, at Tools, Editor's guide. It is written so an agency can hand it straight to a client, and nothing in it names Alt.
To move it into the kit's Blog section, add it to the section's items in resources/preferences.yaml:
'tools::editors_guide': '@move'
To show help links at the end of the guide, set guide.help_url and guide.support_url in the config. Only http, https and mailto links are shown, and nothing shows while both are empty.
Author of a new post
When someone writes a new post, Author is filled in with the author entry whose "Their control panel account" field is linked to them. It only happens on the create screen, so existing posts are never changed, and an author already passed in is kept. Posts made another way, such as an import, are not filled in.
Scheduled posts
Statamic 6 clears the static cache when a scheduled post goes live, so this addon does nothing extra. For that to happen on a live site:
- the Laravel scheduler has to run every minute (
php artisan schedule:runin cron) - the job is queued, so a site with a queue other than
syncneeds a queue worker running - the job only looks at the previous minute, so a post whose time passes while the scheduler is down waits until the next save
Static caching
The feed tells Statamic's static cache not to store it, because the half measure would replay it as HTML. With the full measure, also add /feed to the exclude list in config/statamic/static_caching.php, since the web server serves cached files before Statamic sees the request.
Multi-site
Version 1 is built for a blog on one site. Posts, related posts, structured data and tag page counts all keep to the current site, but there is one feed, for the site its address belongs to, rather than one per site.
Configuration
config/alt-blog.php:
| Key | Default | What it does |
|---|---|---|
collections.posts, collections.authors |
posts, authors |
The blog's collections |
taxonomies.categories, taxonomies.tags |
categories, tags |
The blog's taxonomies |
globals.settings, globals.brand |
blog_settings, brand |
Where Blog settings and the logo live |
feed.path |
feed |
Where the feed is served. null turns the feed off |
feed.title |
null |
The feed's title. null uses the site name |
feed.limit |
20 |
How many posts the feed lists |
guide.help_url, guide.support_url |
null |
Help links at the end of the editor's guide |
For a second kit
Pieces that do not depend on the blog live under AltDesign\AltBlog\Foundation: the JSON-LD graph and node builders, the link preview crop, and heading ids and the Bard reader. A test keeps blog-specific names out of it, so it can move into a shared addon later without changes.
Testing
composer test
Security
If you find a security problem, please report it privately as SECURITY.md describes, rather than opening an issue.
Licence
MIT. See LICENSE.