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.
Anti-Spam Guide
Forum rules
END OF SUPPORT: 1 January 2017 (announcement)
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 this you need at least PHP 5.1.0:
find (in my code):

Code: Select all

// 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 ) ) ); 
replace with:

Code: Select all

// Keywords must be letters, ciphers and underscores only (needs PHP 5.1.0 at least). Need to be more than 3 characters.
        $sKeyWord= ucwords( utf8_strtolower( preg_replace( '#[^\\p{L}\\p{N}a-zA-Z0-9_ ]+#u', ' ', $sMetaDescription ) ) ); 
  • 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
haggisv
Registered User
Posts: 261
Joined: Wed Dec 20, 2006 3:31 am
Location: Adelaide, Australia
Contact:

Re: Dynamic Meta Descriptions

Post by haggisv »

This is a great mod, thanks so much AmigoJack!

Within hours of implimenting it, Google started using my descriptions in their search results!

What would make it complete, is the ability to add the viewforum descriptions for each forum as well. Each forum already has a description, which is what we would have typed in when we created each new forum, so that would seem appropriate the description. Would that be hard to impliment?
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post by AmigoJack »

Open /viewforum.php and find:

Code: Select all

// We need to readd the local announcements to the forums total topic count, otherwise the number is different from the one on the forum list  
Before, add:

Code: Select all

/*** 2012-02-02 BEGIN AmigoJack
  Adding meta descriptions and meta keywords from forum description ***/
$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
    ( ''
    , ''
    , ''
    , ' '
    , ' '
    , ' '
    )
, html_entity_decode( $forum_data['forum_desc'] )
);

// Keywords must be latin letters, ciphers and underscores only. Need to be more than 3 characters.
// If you want non-latin support and have PHP 5.1.0 or higher, use the second line instead
$sKeyWord= ucwords( strtolower( preg_replace( '#[^a-zA-Z0-9_ ]+#', ' ', $sMetaDescription ) ) );
//$sKeyWord= ucwords( utf8_strtolower( preg_replace( '#[^\\p{L}\\p{N}a-zA-Z0-9_ ]+#u', ' ', $sMetaDescription ) ) ); 

$aDistinctKeyword= array();
$aKeyWord= explode( ' ', $sKeyWord );
foreach( $aKeyWord as $v1 )
if( !isset( $aDistinctKeyword[$v1] )
 && utf8_strlen( $v1 )> 3  // Minimum 4 characters per keyword
 && utf8_strlen( $v1 )<= 1000  // Maximum 1000 characters per keyword
  ) $aDistinctKeyword[$v1]= 0;

$aDistinctKeyword= array_slice( $aDistinctKeyword, 0, 20, TRUE );  // First 20 keywords only
$sKeyWord= implode( ', ', array_keys( $aDistinctKeyword ) );

// Shorten description if it's longer than 150 characters
if( ( utf8_strlen( $sMetaDescription )> 150 )
 && ( $iSpace= utf8_strpos( $sMetaDescription, ' ', 150 ) )!== FALSE
  ) $sMetaDescription= utf8_substr( $sMetaDescription, 0, $iSpace );

$template-> assign_vars
( array 
    ( 'S_META_DESCRIPTION'=> $sMetaDescription
    , 'S_META_KEYWORDS'=> $sKeyWord
    )
);
/*** 2012-02-02 END ***/ 
Tested.
  • 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
haggisv
Registered User
Posts: 261
Joined: Wed Dec 20, 2006 3:31 am
Location: Adelaide, Australia
Contact:

Re: Dynamic Meta Descriptions

Post by haggisv »

Awesome, thank you so much AmigoJack!!!

I would like to try it, but unfortunately I can't find that line in my viewforum.php. Is that a line from a MOD perhaps?
User avatar
Oyabun1
Former Team Member
Posts: 23162
Joined: Sun May 17, 2009 1:05 pm
Location: Australia
Name: Bill

Re: Dynamic Meta Descriptions

Post by Oyabun1 »

It's line 551 in a standard 3.0.10 viewforum.php file.
                      Support Request Template
3.0.x: Knowledge Base Styles Support MOD Requests
3.1.x: Knowledge BaseStyles SupportExtension Requests
User avatar
haggisv
Registered User
Posts: 261
Joined: Wed Dec 20, 2006 3:31 am
Location: Adelaide, Australia
Contact:

Re: Dynamic Meta Descriptions

Post by haggisv »

Ahh I'm running still 3.0.9, and since I've got a lot of mods, I've not had the time to upgrade yet. Can you advice roughly where it should be related to another line in view_forum.php please?

Thanks a lot!
User avatar
haggisv
Registered User
Posts: 261
Joined: Wed Dec 20, 2006 3:31 am
Location: Adelaide, Australia
Contact:

Re: Dynamic Meta Descriptions

Post by haggisv »

I've figured it out. For the benefit of anyone else that does not have 3.0.10, you put in AmigoJack's code AFTER:

Code: Select all

// Ok, adjust topics count for active topics list
if ($s_display_active)
{
	$topics_count = 1;
}
Seems to work like a charm!!!
Methyus
Registered User
Posts: 132
Joined: Sat Mar 18, 2006 3:59 am

Re: Dynamic Meta Descriptions

Post by Methyus »

Hey there Amigo,
I used this mod for my index.php and I just wanted to make you and everyone else aware that you CANNOT use apostrophes in any of the words in the description. For instance; "A magician's forum"... It causes

Code: Select all

Parse error: syntax error, unexpected T_STRING, expecting ')' in /home/content/ETC...


Other than that it works beautifully! >8>)
AmigoJack wrote: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?
Also, I was wondering if there is any way to replace "Domain.com • Index Page" with a custom title in index.php.

Any suggestions you might have would be greatly appreciated.

Thanks.

~ Aedryan
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post by AmigoJack »

Methyus wrote:you CANNOT use apostrophes in any of the words in the description. For instance; "A magician's forum"
Look up the PHP manual on how to treat strings and escape literal delimiters.
Example: 'A magician\'s forum'
Methyus wrote:replace "Domain.com • Index Page" with a custom title in index.php
Open /index.php and find:

Code: Select all

page_header($user->lang['INDEX']);
Do you now get it? The title is always partly done when issuing page_header().
As for removing the board name completely, look your other topic.
  • 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
Methyus
Registered User
Posts: 132
Joined: Sat Mar 18, 2006 3:59 am

Re: Dynamic Meta Descriptions

Post by Methyus »

Yup! Got it! You da man, Amigo! >8>)

Thanks as always!
User avatar
Volksdevil
Registered User
Posts: 2414
Joined: Sun Oct 03, 2010 2:03 pm
Location: Lancashire, UK
Name: Neil
Contact:

Re: Dynamic Meta Descriptions

Post by Volksdevil »

Can we make this work for keywords and description when viewing a forum category?
My phpBB Extensions
Finally found great Website Hosting from Image KUALO!
Do NOT use 123-reg.co.uk - Incapable of running phpBB!
:ugeek: TekNeil - Streamer on Mixer | My Volkswagen Corrado G60
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post by AmigoJack »

Categories don't have descriptions (and their title is already the site title), so I don't understand your question.
  • 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
Volksdevil
Registered User
Posts: 2414
Joined: Sun Oct 03, 2010 2:03 pm
Location: Lancashire, UK
Name: Neil
Contact:

Re: Dynamic Meta Descriptions

Post by Volksdevil »

No matter, I forgot that I added category descriptions within my template myself.
My phpBB Extensions
Finally found great Website Hosting from Image KUALO!
Do NOT use 123-reg.co.uk - Incapable of running phpBB!
:ugeek: TekNeil - Streamer on Mixer | My Volkswagen Corrado G60
User avatar
gwmbox
Registered User
Posts: 62
Joined: Mon Feb 11, 2002 8:59 am

Re: Dynamic Meta Descriptions

Post by gwmbox »

Can I ask, is the code edits in the 2nd post the correct code to use still or has there been an update since that post?

Cheers
User avatar
gwmbox
Registered User
Posts: 62
Joined: Mon Feb 11, 2002 8:59 am

Re: Dynamic Meta Descriptions

Post by gwmbox »

Is it possible to create a list of words for it not to include in the keyword metadata? I.e. like where, when, that, then etc.

Thanks
Locked

Return to “[3.0.x] Support Forum”