Since Flarum SEO version 1.3 it's possible to use the SEO functionality and trigger specific functions, like changing the page title, page description, page type, social media image and more. It's a powerfull feature for extension developers.
How does it work?
The plugin has a SeoProperties class that can be used by extension developers on their own pages triggered by the page. This class contains multiple functionalities that can be called whenever you need them. Your provided data will override the default SEO data from that page.
Function list
A few functions are explained on this page. When you'd like to check the exact parameter options, check out the IndexPage class on GitHub.
Updating the page or social media title
You can use the following code to override the page title and update the social media meta tags.
It automatically sets the page <title> tag, twitter:title meta tag, og:title meta tag.
$properties->setTitle("Example title");
Updating the page title is optional. Adding a false parameter will only update the social media meta tags.
$properties->setTitle("Social media title only", false);
Example rendered result:
<title>Example title</title>
<meta name="twitter:title" content="Example title">
<meta property="og:title" content="Example title">
<script type="application/ld+json">
[{ "@context":"http:\/\/schema.org", "...", "title": "Example title" }]
</script>
Updating the page description
You can use the following code to override the page description. It will automatically stripped down to 157 characters if a large text has been provided.
It automatically sets the description meta tag, twitter:description meta tag, og:description meta tag.
$properties->setDescription("Example description");
Example rendered result:
<meta name="description" content="Example description">
<meta name="twitter:description" content="Example description">
<meta property="og:description" content="Example description">
<script type="application/ld+json">
[{ "@context":"http:\/\/schema.org", "...", "description": "Example description" }]
</script>
Updating the page URL
You can use the following code to override the page URL that's being showed on social media.
It automatically sets the twitter:url meta tag, og:url meta tag, url parameter in the JSON (application/ld+json).
$properties->setUrl("/d/5-example-topic");
You can also provide a full URL. In this case you need to add the false parameter.
$properties->setUrl("https://flarum.local/d/5-example-topic", false);
Example rendered result:
<meta name="twitter:url" content="https://flarum.local/d/5-example-topic">
<meta name="og:url" content="https://flarum.local/d/5-example-topic">
<script type="application/ld+json">
[{ "@context":"http:\/\/schema.org", "...", "url": "https://flarum.local/d/5-example-topic" }]
</script>
Updating the canonical URL
You can use the following code to override the canonical URL. This is needed if you have duplicate pages and need to tell search engine what's the original page to index.
It automatically adds or updates the canonical <link> tag.
$properties->setCanonicalUrl("/d/5-example-topic");
Example rendered result:
<link rel="canonical" href="https://flarum.local/d/5-example-topic">
Updating the page keywords
You can use the following code to provide keywords of the page.
It automatically adds or overrides the keywords meta tag.
// An array of keywords that describes the page
$properties->setKeywords(["keyword 1", "flarum", "site", "blog"]);
Example rendered result:
<meta name="keywords" content="keyword 1, flarum, site, blog">
Updating the page social media image
You can use the following code to provide a social media image for this page.
It automatically adds or overrides the twitter:image meta tag, og:image meta tag, image parameter in the JSON (application/ld+json).
$properties->setImage("https://www.devnl.nl/assets/favicon-djp573o3.png");
Example rendered result:
<meta name="twitter:image" content="https://www.devnl.nl/assets/favicon-djp573o3.png">
<meta name="og:image" content="https://www.devnl.nl/assets/favicon-djp573o3.png">
<script type="application/ld+json">
[{ "@context":"http:\/\/schema.org", "...", "image": "https://www.devnl.nl/assets/favicon-djp573o3.png" }]
</script>
Updating the page 'published' date and time
You can use the following code to provide a published on date-timestamp.
It automatically adds or overrides the article:published_time meta tag, datePublished parameter in the JSON (application/ld+json).
$properties->setPublishedOn("2020-08-22 14:14:00");
Example rendered result:
<meta name="article:published_time" content="2020-08-22T14:14:00+00:00">
<script type="application/ld+json">
[{ "@context":"http:\/\/schema.org", "...", "datePublished": "2020-08-22T14:14:00+00:00" }]
</script>
Updating the page 'updated on' date and time
You can use the following code to provide a updated on date-timestamp.
It automatically adds or overrides the article:updated_time meta tag, dateModified parameter in the JSON (application/ld+json).
$properties->setUpdatedOn("2020-08-25 18:55:00");
Example rendered result:
<meta name="article:updated_time" content="2020-08-25T18:55:00+00:00">
<script type="application/ld+json">
[{ "@context":"http:\/\/schema.org", "...", "dateModified": "2020-08-25T18:55:00+00:00" }]
</script>
Generate page description
Long content will automatically be stripped.
$description = $properties->generateDescriptionFromContent("Some content");
Image from content
The content will be analized and checked if there's an image inside that can be used.
$image = $properties->getImageFromContent("Some content with an https://example.org/test.png");