Skip to main content

A tool to assist with metadata for social media.

Project description

This plugin adds custom properties to your page models and then lets you output meta-attribute tags using the included template tag. These tags help with search engine optimisations and for creating nice shareable links for social media, mainly Facebook and Twitter.

Compatibility

Wagtail-metadata works with Wagtail v2.0 and upwards.

Installing

First, install using pip:

pip install wagtail-metadata

Then add wagtailmetadata to your project’s INSTALLED_APPS:

INSTALLED_APPS = [
    'home',
    'search',
    # etc...

    'wagtail.contrib.settings',
    'wagtail.contrib.modeladmin',
    # etc...

    # Add the following:
    'wagtailmetadata',

]

Using

Pages should inherit from wagtailmetadata.models.MetadataPageMixin. This provides a search_image field in the Wagtail interface for that page type. The description for the page will be pulled from the search_description page. Metadata for the page will then be built from the page details.

from wagtail.core.models import Page
from wagtailmetadata.models import MetadataPageMixin

class ContentPage(MetadataPageMixin, Page):
    pass

If you want to use this with a non-page model, or want to use a different implementation for the fields, you can inherit from wagtailmetadata.models.MetadataMixin. You will need to implement the following methods:

from wagtailmetadata.models import MetadataMixin

class CustomObject(MetadataMixin, object):

    def get_meta_title(self):
        """The title of this object"""
        return "My custom object"

    def get_meta_url(self):
        """The URL of this object, including protocol and domain"""
        return "http://example.com/my-custom-object/"

    def get_meta_description(self):
        """
        A short text description of this object.
        This should be plain text, not HTML.
        """
        return "This thing is really cool, you should totally check it out"

    def get_meta_image(self):
        """A relevant Wagtail Image to show. Optional."""
        return self.some_image

    def get_meta_twitter_card_type(self):
        """
        What kind of Twitter card to show this as.
        Defaults to ``summary_large_photo`` if there is a meta image,
        or ``summary`` if there is no image. Optional.
        """
        return "summary_large_photo"

Display

Django

To use this in a template, first load the template tag library, and then insert the metadata by placing {% meta_tags %} into the <head>:

{% load wagtailmetadata_tags %}
{% meta_tags %}

By default, this will look for a self object in the context to pull the metadata from. You can specify a different object to use if you need to:

{% load wagtailmetadata_tags %}
{% meta_tags my_custom_object %}

Jinja2

Add wagtailmetadata.jinja2tags.WagtailMetadataExtension to the template extensions in your settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'OPTIONS': {
            'extensions': [
                'wagtailmetadata.jinja2tags.WagtailMetadataExtension'
            ],
        },
    }
]

Insert the metadata by placing {{ meta_tags() }} into the <head>:

{{ meta_tags() }}

By default, this will look for a page object in the context to pull the metadata from. You can specify a different object to use if you need to:

{{ meta_tags(my_custom_object) }}

Troubleshooting

‘meta_tags’ missing request from context

The template that is trying to render the meta_tags tag does not have a request object in the context.

‘meta_tags’ tag is missing a model or object

There was no model passed to the template tag, or self is not found in the current context.

Adding extra tags

If you need to add extra meta tags, to add the twitter:site tag for example, you can extend the Wagtail Metadata template. First, create any models that you might need to hold the extra data:

from wagtail.contrib.settings.models import BaseSetting, register_setting

@register_setting
class TwitterName(BaseSetting):
    handle = models.CharField(max_length=20)

You could also add extra fields to a page model and output them as meta tags:

class MyPage(MetadataPageMixin, Page):
    body = RichTextField()
    author_twitter_handle = models.CharField(max_length=20)

Then, override the wagtailmetadata/parts/tags.html template and add your tags to the relevant blocks:

{% extends "wagtailmetadata/parts/tags.html" %}

{% block twitter %}
    {{ block.super }}
    <meta name="twitter:site" content="@{{ settings.myapp.TwitterName.twitter_handle }}" />
    <meta name="twitter:creator" content="@{{ model.author_twitter_handle }}" />
{% endblock %}

The wagtailmetadata/parts/tags.html template defines the following blocks you can override or extend:

{% block tags %}

This block surrounds the whole template. You can override this block to append extra tags before or after the standard tags.

{% block twitter %}

This block surrounds the Twitter card tags.

{% block opengraph %}

This block surrounds the Open Graph tags

{% block meta %}

This block surrounds the standard meta tags defined in HTML.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail-metadata-3.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

wagtail_metadata-3.1.0-py2.py3-none-any.whl (11.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file wagtail-metadata-3.1.0.tar.gz.

File metadata

  • Download URL: wagtail-metadata-3.1.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.3

File hashes

Hashes for wagtail-metadata-3.1.0.tar.gz
Algorithm Hash digest
SHA256 7af9d20b324f2769b24516f6206b183072a69fa5c8c239127072c747ba22bc88
MD5 cb57006c0f27e21a63c354abd39e52c2
BLAKE2b-256 3d6447a9cd55160d77841e699e1f205b58877a4b69e90fa1e39f5b271ade63bb

See more details on using hashes here.

File details

Details for the file wagtail_metadata-3.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: wagtail_metadata-3.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.5.3

File hashes

Hashes for wagtail_metadata-3.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7525a80ab2c0134a1597c2201506ffacfe1a59d8ef8355ed243b3fc072e115d5
MD5 86a3ed26946111a90ee0e732b5daecb4
BLAKE2b-256 5b8d4f1f5fb7d64aa7cc686318ebfcf3327bb2139eff6a0e824cf628228199ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page