Dynamic Meta Descriptions

Get help with installation and running phpBB 3.0.x here. Please do not post bug reports, feature requests, or MOD-related questions here.
Ideas Centre
Forum rules
END OF SUPPORT: 1 January 2017 (announcement)
Locked
User avatar
mrgeeza
Registered User
Posts: 58
Joined: Wed Apr 14, 2010 12:56 pm
Location: UK
Name: Toby Lloyd
Contact:

Dynamic Meta Descriptions

Post 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-)
Last edited by Pit$Bull on Thu Mar 03, 2011 11:35 am, edited 1 time in total.
Reason: Topic icon changed
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post 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.
Last edited by AmigoJack on Mon Jan 09, 2012 12:26 pm, edited 4 times in total.
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
mrgeeza
Registered User
Posts: 58
Joined: Wed Apr 14, 2010 12:56 pm
Location: UK
Name: Toby Lloyd
Contact:

Re: Dynamic Meta Descriptions

Post by mrgeeza »

Worked perfect first time! Thanks Mate! :D
User avatar
mrgeeza
Registered User
Posts: 58
Joined: Wed Apr 14, 2010 12:56 pm
Location: UK
Name: Toby Lloyd
Contact:

Re: Dynamic Meta Descriptions

Post 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? :)
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post 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?
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
mrgeeza
Registered User
Posts: 58
Joined: Wed Apr 14, 2010 12:56 pm
Location: UK
Name: Toby Lloyd
Contact:

Re: Dynamic Meta Descriptions

Post by mrgeeza »

Yeah. I will put it into action and let you know :)
User avatar
mrgeeza
Registered User
Posts: 58
Joined: Wed Apr 14, 2010 12:56 pm
Location: UK
Name: Toby Lloyd
Contact:

Re: Dynamic Meta Descriptions

Post by mrgeeza »

Worked perfect! Thanks!

SOLVED!
professor mauricio
Registered User
Posts: 30
Joined: Tue Jul 12, 2011 4:54 pm

Re: Dynamic Meta Descriptions

Post 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
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post 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...
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Re: Dynamic Meta Descriptions

Post 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??
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post 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.
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Re: Dynamic Meta Descriptions

Post by Alien_Time »

sweet....I will test it out and will let you know how it goes..
User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Re: Dynamic Meta Descriptions

Post 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..
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post 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
                ); 
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
Alien_Time
Registered User
Posts: 389
Joined: Fri Dec 31, 2010 7:08 am

Re: Dynamic Meta Descriptions

Post 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...
Locked

Return to “[3.0.x] Support Forum”