Home

Autofill MetaDescription and MetaKeywords on page save

What it Does

This addition will let you automaticly fill in the MetaDescription and MetaKeyWords fields based on the submitted content. The MetaKeyWords are selected and ordered by keyword density.

Requirements

Silverstripe v2.3.2

Attached Files


Metamanager Module(7.8 KB)

Page.php(1.5 KB)

Most of my customers don't want to add meta info manually, so I add an onBeforeWrite() function to update the Meta Description field and count the keyword density of the content with a separate function and add these keywords to the Meta Keywords fields when a page is saved.

In my humble SEO opinion every page needs an unique Meta Description. Keywords seems to be less important for search engines, but why not add them automatically anyway! The function counts how many times each word exist in the Content, order the words by occurrence and glues the words to a comma separated string. In this function only words with more then 4 characters are counted, but you can adjust that if you like.

To do this, add an onBeforeWrite() function in your Page class (mysite/Page.php).
This will fill in the Meta Description with the Content (The Meta Description Field is limited to 255 characters).

 

For the keywords we need two extra functions to calculate the keyword density (borrowed from Gabriel Comarita):
Add the following functions to your Page class (mysite/Page.php)

Now every time a Page (or an extended page type like the Blog Page) is saved, it updates the Meta Description or Meta Keywords fields.

Since the CMS forms are posted with Ajax, you need to refresh the page to see the results immediately. You can automate this by adding a onAfterWrite function to you Page class:

You could extend this by adding a check-box field, so you can choose if you want to update the meta fields or not each time you save a page.

With this idea I made a separate Decorator extension with its own _config.php file for the configuration options.

The code to update the meta fields is slightly different then this tutorial and I added some options to suppress the confirm dialog each time you change the page title and to hide some of the meta fields. Put the files in the Metamanager package to you siteroot so you get siteroot/metamanager/ . Change the settings in the config file to suit you needs and voila Automated meta info!

About the Author

Name: Martijn van Nieuwenhoven

Website: http://www.mediavacature.nl

Martijn van Nieuwenhoven runs the largest Dutch Jobboard for Media, Marcom and Graphic related jobs. He also develops websites and applications for small business companies based in The Netherlands.

Comments (10)

  • Just a suggestion. I didn't like the basics of the code, in that it saved *every* time, even if I go back and want to make changes, it would overwrite those changes. Also, it didn't do nearly enough clean up for my taste, so I added to that. Hopefully it will be useful to someone else:

    if($this->ID){
    // fill in MetaDescription without any tags
    if($this->record['Content'] && empty($this->record['MetaDescription'])){
    //$this->record['MetaDescription'] = preg_replace($p,$r,htmlspecialchars(strip_tags($this->record['Content'])));
    $this->record['MetaDescription']=preg_replace('/ (?= )/', '', trim(preg_replace('/[\\~`!\"$%^&*()+{}[\]<>?\/|=]/', '', trim(strip_tags(htmlspecialchars_decode($this->record['Content']))))));

    }
    // fill in MetaKeywords
    if($this->record['Content'] && empty($this->record['MetaKeywords'])){
    $this->record['MetaKeywords'] = self::calculateKeywords(preg_replace('/ (?= )/', '', trim(preg_replace('/[\\~`!\"$%^&*()+{}[\]<>?\/|=]/', '', trim(strip_tags(htmlspecialchars_decode($this->record['Content'], 4, 15)))))));
    }
    }
    }

    Posted by Jeremy, 09/07/2010 1:34am (20 days ago)

  • This is a very good example of how to use onBeforeWrite and onAfterWrite thanks.

    An alternative, so long as you don't want keywords (which as discussed are not actually useful), is to overload the MetaTags function in your Page.php and test if $this->MetaDecription is populated otherwise use $this->Content instead. Similar to how the title tag is populated. See SiteTree->MetaTags()

    This way you don't need to set a preference to auto populate in the CMS. It just happens if there is no MetaDescription entered.

    Posted by Richard, 22/06/2010 7:07pm (1 month ago)

  • There is an update that doesnt require editing your page.php... and works on 2.4

    http://www.kreationsbyran.se/blogg/update-to-metamanager-module-for-silverstripe/

    Posted by mike mcvey, 22/06/2010 5:25pm (1 month ago)

  • It's didn't work, i can't save a page, maybe it because of different from English language(Russian)?

    Posted by Arthur, 03/03/2010 1:27am (5 months ago)

  • Hi, it seems the stopwords-feature does not work that well... I tested with a Lorem ipsum-text with alot of 'sed's. However they weren't filtered (NB I set the char-limit to 3).

    Posted by theAlien, 01/03/2010 7:55am (5 months ago)

  • Posted by , 25/02/2010 11:55am (5 months ago)

  • Hi I am trying to implement this but whenever I save I get an error saying error saving content

    Posted by Rohan, 28/01/2010 10:54am (6 months ago)

  • How would I go about implementing this into a batch action?

    Posted by Chase Q. Aucoin, 17/11/2009 12:50pm (8 months ago)

  • This looks really great. Would definitely come in handy for clients who don't always bother with this, but should.

    Main thing I noticed was the description is limited by characters. It would be good if you could limit it by words. Right now the 255, or whatever limit you set, will cut off the last word unless it happens to be an exact fit, which is rare.

    Posted by Liam, 03/09/2009 4:02pm (11 months ago)

  • Hi Yakiv, with the files in the zipfile you can add a commaseparated string with words you want to exclude around r 71 in the config file:

    MetaManagerDataDecorator::set_exclude_words('');

    There are some problems with special chars like â, é ö. Im working on that. replacing r 296 with :
    $this->owner->MetaDescription = utf8_encode(html_entity_decode(strip_tags($this->owner->Content)));
    will solve this for the MeteDescription, but it has no effect on the MetaKeywords calculation.

    Posted by Martijn, 02/08/2009 7:51am (12 months ago)

RSS feed for comments on this page RSS feed for all comments

Post your comment