Page 1 of 7

Dynamic Meta Descriptions

Posted: Wed Mar 02, 2011 10:31 pm
by mrgeeza
Hey There!

I'm just making my site appear a little better in Google, I've been told that Meta Tags are rarely used now due to some webmaster abusing them and just putting every tag under the sun down, however the Meta Description is still used and plays a part in showing snippets of an article in Google. Would I be right in thinking this?

If so, how can I get phpBB to automatically add a small snippet of the first thread into the meta data area of the code.

Would this work?

MrGeeza 8-)

Re: Dynamic Meta Descriptions

Posted: Thu Mar 03, 2011 7:45 am
by AmigoJack
This works topic-wise: when viewing a topic, meta description and meta keywords are dynamically generated based on the first post being displayed, based on the current sort order of the posts and the post being shown as first (in means of multiple pages).

Open /viewtopic.php and find:

Code: Select all

    // Parse the message and subject
    $message = censor_text($row['post_text']); 
After, add:

Code: Select all

    /*** 2011-03-03 BEGIN AmigoJack
        Dynamic meta description and meta keywords for topics, based on the first post ***/
    if( !$sMetaDescription ) {
        $sMetaDescription= preg_replace
                ( array
                        ( '#\\[([^/\\]=]+)[^\\]]*\\]#'  // Strip possible BBCode opening tags
                        , '#\\[/[^\\]]*\\]#'  // Closing tags
                        , '#<[^>]+>#'  // Strip possible HTML
                        , '#[\\n\\r\\t]#s'  // Strip linebreaks and tabs
                        , '# +#'  // Strip multiple spaces
                        , '#[a-z]+(:|\\&\\#58;)\\S+#i'  // Strip URIs
                        )
                , array
                        ( ''
                        , ''
                        , ''
                        , ' '
                        , ' '
                        , ' '
                        )
                , $message
                );

        // Keywords must be latin letters, ciphers and underscores only. Need to be more than 3 characters.
        $sKeyWord= ucwords( strtolower( preg_replace( '#[^a-zA-Z0-9_ ]+#', ' ', $sMetaDescription ) ) );
        $aKeyWord= explode( ' ', $sKeyWord );
        $aDistinct= array();
        foreach( $aKeyWord as $v1 ) if( !isset( $aDistinct[$v1] )&& utf8_strlen( $v1 )> 3 ) $aDistinct[$v1]= 0;
        $sKeyWord= implode( ', ', array_keys( $aDistinct ) );

        // Shorten description if it's longer than 150 characters
        if( ( utf8_strlen( $sMetaDescription )> 150 )&& ( $iSpace= utf8_strpos( $sMetaDescription, ' ', 150 ) )!== FALSE ) $sMetaDescription= substr( $sMetaDescription, 0, $iSpace );
    }
    /*** 2011-03-03 END ***/ 
Find:

Code: Select all

// Output the page
page_header($user->lang['VIEW_TOPIC'] . ' - ' . $topic_data['topic_title'], true, $forum_id); 
Before, add:

Code: Select all

    /*** 2011-03-03 BEGIN AmigoJack
        Assign possible meta data ***/
    $template-> assign_vars
    ( array
        ( 'S_META_DESCRIPTION'=> $sMetaDescription
        , 'S_META_KEYWORDS'=> $sKeyWord
        ) 
    );
    /*** 2011-03-03 END ***/ 
For every style you use open /styles/?/template/overall_header.html and find:

Code: Select all

<meta name="keywords" content="" />
<meta name="description" content="" />
Replace with:

Code: Select all

<meta name="keywords" content="{S_META_KEYWORDS}" />
<meta name="description" content="{S_META_DESCRIPTION}" />


For every style you use delete /cache/tpl_?_overall_header.html.php.

Tested.

Re: Dynamic Meta Descriptions

Posted: Thu Mar 03, 2011 11:34 am
by mrgeeza
Worked perfect first time! Thanks Mate! :D

Re: Dynamic Meta Descriptions

Posted: Thu Mar 03, 2011 12:25 pm
by mrgeeza
Sorry to double post, however is there any alterations that I can make to the code so that on the home page (which for me is a phpbb custom page) a custom description? :)

Re: Dynamic Meta Descriptions

Posted: Thu Mar 03, 2011 12:38 pm
by AmigoJack
For every file you want open /?.php and find:

Code: Select all

$template->assign_vars(array( 
Before, add:

Code: Select all

    /*** 2011-03-03 BEGIN AmigoJack
        Assign meta data ***/
    $template-> assign_vars
    ( array
        ( 'S_META_DESCRIPTION'=> 'well, here goes my desc'
        , 'S_META_KEYWORDS'=> 'word1, word2, word3'
        ) 
    );
    /*** 2011-03-03 END ***/ 
Do you now understand the logic?

Re: Dynamic Meta Descriptions

Posted: Thu Mar 03, 2011 12:50 pm
by mrgeeza
Yeah. I will put it into action and let you know :)

Re: Dynamic Meta Descriptions

Posted: Sun Mar 06, 2011 12:12 am
by mrgeeza
Worked perfect! Thanks!

SOLVED!

Re: Dynamic Meta Descriptions

Posted: Thu Jul 14, 2011 2:36 pm
by professor mauricio
AmigoJack, do you speak portuguese? Because "amigo" is "friend"

i'm looking for something like you wrote, but i'm not shure yet:

your code automaticaly generates the meta tags?

it just 'reads' the topic and 'choose' words for tagging?

or it works just do descriptions?

we're using a mod that allows us to write a tag to every topic and or post, but i want to merge with yours

thx

Re: Dynamic Meta Descriptions

Posted: Mon Jul 18, 2011 12:04 pm
by AmigoJack
professor mauricio wrote:AmigoJack, do you speak portuguese? Because "amigo" is "friend"
No. But I know. ;)
professor mauricio wrote:your code automaticaly generates the meta tags?
Yes.
professor mauricio wrote:it just 'reads' the topic and 'choose' words for tagging?
Yes:
AmigoJack wrote:when viewing a topic, meta description and meta keywords are dynamically generated based on the first post being displayed, based on the current sort order of the posts and the post being shown as first (in means of multiple pages).
In other words: if:
  • a topic has 300 posts and
  • the default post order is the posting time (ascending = from oldest to newest) and
  • the default post count per page is 15 (which makes 20 pages for 300 posts)
...then:
  • on page 1 the text from post 1 is used,
  • on page 2 the text from post 16 is used (which is the first post on that page),
  • on page 3 the text from post 31 is used...

Re: Dynamic Meta Descriptions

Posted: Mon Aug 01, 2011 4:45 am
by Alien_Time
HI AmigoJack,

I have been looking for a simple solution to add the first post as the Meta Descriptions for Viewtopics pages and I am very glad to find your code to modify this...Thank you for that..

I have made the changes and it works really fine...I also like the fact that the meta descriptions also changes for each page by taking the first post of that page..Its good since it avoids duplicate meta descriptions..

However, the one thing I have noticed so far is that, for some reason the "font color" bbcodes are not getting stripped. So for example, if the first post has a blue color like this:
Hi, how is phpbb?
Then the meta desciptions looks like:
[color=#0000BF:7v1alk7x]Hi, how is phpbb?.....
I did realise that you have already added a code to strip possible bbcodes but this font color code is still coming up ... How do we fix that??

Re: Dynamic Meta Descriptions

Posted: Mon Aug 01, 2011 7:12 am
by AmigoJack
I updated the code. In /viewtopic.php instead of:

Code: Select all

            ( '#\\[([^/\\]=]+)[^\\]]*\\](.*)\\[/\\1\\]#'  // Strip possible BBCode 
use this line:

Code: Select all

            ( '#\\[([^/\\]=]+)[^\\]]*(:[0-9a-z]{8})?\\](.*)\\[/\\1\\2\\]#'  // Strip possible BBCode 
Untested.

Re: Dynamic Meta Descriptions

Posted: Mon Aug 01, 2011 7:20 am
by Alien_Time
sweet....I will test it out and will let you know how it goes..

Re: Dynamic Meta Descriptions

Posted: Mon Aug 01, 2011 7:27 am
by Alien_Time
Hi AmigoJack,

I just tried it out and its still the same..I did the changes, refreshed templates and themes and also cleared the cache..The result is still the same and I can see still the "[color=#FF0080:zw72coec]" in the meta description..

Re: Dynamic Meta Descriptions

Posted: Mon Aug 01, 2011 7:56 am
by AmigoJack
Sorry, now I tested it. It would also not have worked on stacked BBCodes. The new branch in /viewtopic.php is (post updated again):

Code: Select all

                ( array
                        ( '#\\[([^/\\]=]+)[^\\]]*\\]#'  // Strip possible BBCode opening tags
                        , '#\\[/[^\\]]*\\]#'  // Closing tags
                        , '#<[^>]+>#'  // Strip possible HTML
                        , '#[\\n\\r\\t]#s'  // Strip linebreaks and tabs
                        , '# +#'  // Strip multiple spaces
                        )
                , array
                        ( ''
                        , ''
                        , ''
                        , ' '
                        , ' '
                        )
                , $message
                ); 

Re: Dynamic Meta Descriptions

Posted: Tue Aug 02, 2011 12:05 am
by Alien_Time
Thats it AmigoJack...

That worked! Thanks a lot man for taking your time to look into this issue...much appreciated...you are a good amigo! :D

Cheers...