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.
Suggested Hosts
Forum rules
END OF SUPPORT: 1 January 2017 (announcement)
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 »

Hi Amigojack,

I tried using your code but with debug on i get the following when viewing a topic:
[phpBB Debug] PHP Notice: in file /viewtopic.php on line 1532: Undefined variable: sMetaDescription
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1024: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3715)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1024: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3715)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4606: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3715)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4608: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3715)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4609: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3715)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4610: Cannot modify header information - headers already sent by (output started at /includes/functions.php:3715)
EDIT: I think it may be because this find:
AmigoJack wrote: Find:

Code: Select all

// Output the page
page_header($user->lang['VIEW_TOPIC'] . ' - ' . $topic_data['topic_title'], true, $forum_id); 
In my viewtopic I have this as I got rid of the VIEWTOPIC in my forum titles when viewing topics, any way around it?

Code: Select all

// Output the page
page_header($topic_data['topic_title'], true, $forum_id);
Edit: Nope, I changed back to this and still get the errors after purging etc:

Code: Select all

// Output the page
page_header($user->lang['VIEW_TOPIC'] . ' - ' . $topic_data['topic_title'], true, $forum_id);
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 »

Volksdevil wrote:with debug on
That's what's causing it. However, to also get rid of notices, find:

Code: Select all

// Output the posts  
Before, add:

Code: Select all

/*** 2011-03-03 BEGIN AmigoJack
  Initializing ***/
$sMetaDescription= '';
/*** 2011-03-03 END ***/ 
  • 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 »

Spot on, that did the trick thanks :D

I just noticed in a post someone has made on my forum, that contains photobucket pics. This is in the description meta http://i137.photobucket.com/albums/q202/_Damo_BBG_/22042011094.jpg Is that ok? :)
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 »

That depends on what you want: should URIs be removed entirely or should they also be included (in a proper form)?
  • 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 »

I guess it would be better to have them removed, more use of descriptions rather than meaningless url's taking up space 8-)
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 »

Updated my post - you have to update the code block of the first instruction (the one which now contains):

Code: Select all

, '#[a-z]+(:|\\&\\#58;)\\S+#i'  // Strip URIs  
Tested. Also kills (of course) picture URLs.
  • 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 »

:D Thanks again Amigojack.
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
AcE-Hamerhead
Registered User
Posts: 13
Joined: Sun Jul 15, 2007 11:32 pm

Re: Dynamic Meta Descriptions

Post by AcE-Hamerhead »

AmigoJack,

I've used your viewtopic.php code and it works. However, the index page code you suggested:

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 ***/ 
did not work for me. Could you tell me what I'm doing wrong Sir? Thank You
Last edited by AcE-Hamerhead on Fri Aug 26, 2011 5:25 pm, edited 1 time in total.
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post by AmigoJack »

If the first (viewtopic) "works" then the second (index) must also. Can't reproduce your problem. Make sure you've edited the header template and purged its cache file.

My definition of "it works" is looking up the source of the displayed site and finding the metadata in its supposed position.
  • 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
AcE-Hamerhead
Registered User
Posts: 13
Joined: Sun Jul 15, 2007 11:32 pm

Re: Dynamic Meta Descriptions

Post by AcE-Hamerhead »

Thank you for your prompt reply!

I was confusing one issue with another. I simply had to edit the page title in the index.php to do what I wanted.

Thank you for your time.
User avatar
Walther
Registered User
Posts: 283
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: Dynamic Meta Descriptions

Post by Walther »

Is there a way to limit the number of keywords ?
According to a metatag tester, there are to many keywords in most topics. A maximum of 20 is recommended.

For now I shortened the keywords list (but still too many words) by increasing the word quality. Words must not be longer then 3, but 5 characters to get listed.
It's just not good enough, still too many words. :roll:

Secondly, there is a maximum of 1000 characters for keywords. But limiting it to 20 words will solve that, I guess.
User avatar
AmigoJack
Registered User
Posts: 5795
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Dynamic Meta Descriptions

Post by AmigoJack »

In my code, find:

Code: Select all

        foreach( $aKeyWord as $v1 ) if( !isset( $aDistinct[$v1] )&& utf8_strlen( $v1 )> 3 ) $aDistinct[$v1]= 0;
        $sKeyWord= implode( ', ', array_keys( $aDistinct ) ); 
Replace with:

Code: Select all

        foreach( $aKeyWord as $v1 ) if( !isset( $aDistinct[$v1] )&& utf8_strlen( $v1 )> 3&& utf8_strlen( $v1 )<= 1000 ) $aDistinct[$v1]= 0;  // Minimum 4 chars, maximum 1000
        array_splice( $aDistinct, 20 );  // First 20 entries only
        $sKeyWord= implode( ', ', array_keys( $aDistinct ) ); 
Last edited by AmigoJack on Mon Jan 09, 2012 12:25 pm, edited 1 time 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
Walther
Registered User
Posts: 283
Joined: Fri Jul 09, 2004 5:21 pm
Location: The Netherlands

Re: Dynamic Meta Descriptions

Post by Walther »

Perfect. :)
gery1977
Registered User
Posts: 20
Joined: Sun Sep 18, 2011 4:59 pm

Re: Dynamic Meta Descriptions

Post by gery1977 »

Hi there and great mod,
I installed this mod recently and it works like a charm but l would like it to extract the first post of the topic from the first page instead of the first post on each page ie (the second page and onwards). How could l implement this?

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

Re: Dynamic Meta Descriptions

Post by AmigoJack »

gery1977 wrote:the first post of the topic from the first page instead of the first post on each page
That requires a different approach, which (in contrast to everything else here) needs one edit only - open /viewtopic.php and find:

Code: Select all

// Output the posts       
Before, add:

Code: Select all

/*** 2011-10-04 BEGIN AmigoJack
    Dynamic meta description and meta keywords for topics, based on the first post.
    Always get first post of topic (including sorting and filters) ***/
$aFirstPost= array();

// Being on another than the first page needs us to query data separately
if( $start> 0 ) {
    // Select the sort order
    $direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');

    if (is_array($sort_by_sql[$sort_key]))
    {
        $sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
    }
    else
    {
        $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
    }

    $sql = 'SELECT p.post_text
        FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE . ' u': '') . "
        WHERE p.topic_id = $topic_id
            " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . "
            " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
            $limit_posts_time
        ORDER BY $sql_sort_order";
    $result = $db->sql_query_limit($sql, 1);
    while ($row = $db->sql_fetchrow($result)) $aFirstPost= $row;
    $db->sql_freeresult($result);
// Already got it
} else {
    for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
    {
        if (!isset($rowset[$post_list[$i]])) continue;

        $aFirstPost= &$rowset[$post_list[$i]];
        break;
    }
}

// We should have found at least one post
if( count( $aFirstPost ) ) {
    $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
        ( ''
        , ''
        , ''
        , ' '
        , ' '
        , ' '
        )
    , censor_text( $aFirstPost['post_text'] )
    );

    // 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&& utf8_strlen( $v1 )<= 1000 ) $aDistinct[$v1]= 0;  // Minimum 4 chars, maximum 1000
    array_splice( $aDistinct, 20 );  // First 20 entries only
    $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 );

    $template-> assign_vars
    ( array
        ( 'S_META_DESCRIPTION'=> $sMetaDescription
        , 'S_META_KEYWORDS'=> $sKeyWord
        ) 
    );
}
/*** 2011-10-04 END ***/  
Tested.
Last edited by AmigoJack on Mon Jan 09, 2012 12:27 pm, edited 5 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
Locked

Return to “[3.0.x] Support Forum”