SEO plugin: a simple idea

Creating and modifying plugins.
satollo
Regular
Posts: 13
Joined: Wed Jul 05, 2006 9:41 am
Contact:

SEO plugin: a simple idea

Post by satollo »

Search engine look for meta tag, even if some say no. Specially, they look for meta description, may be just to show a short description in the result page.

I'm not able to write a plugin, now, but in my site I use this simple logic: I extract the first document paragraph and I use it a meta description and, for keywords, I extract all the words marked as bold (with b o strong tags). But one can use the em tag to identify the keywords or others.

This help to have meta information without write it in special field (which is time consuming)

It's simple to create such a plugin?

Satollo
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: SEO plugin: a simple idea

Post by garvinhicking »

Hi!

I could write you such a plugin, if you give me your current code you use to extract the metafields!

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
satollo
Regular
Posts: 13
Joined: Wed Jul 05, 2006 9:41 am
Contact:

Re: SEO plugin: a simple idea

Post by satollo »

To extract the description this is a piece of code (very simple):

function extract_description($text)
{
$x = strpos($text, '<p>');
if ($x === false) return '';
$y = strpos($text, '</p>');
if ($y === false) return '';
$title = substr($text, $x+3, $y-($x+3));
$title = strip_tags($title);
return $title;
}

this is the code I use in my custom CMS. For the keywords:

function extract_keywords($text)
{
preg_match_all('/<strong>([^>]*)<\/strong>/si', $text, $match);
preg_match_all('/<b>([^>]*)<\/b>/si', $text, $match2);
return array_merge($match[1], $match2[1]);
}

this one return an array with all the "pieces of text" within b or strong tag. It's not perfect because it doesn't conseder tags with class or others attributes, but can be improved.
To join the keywords:

$keywords = join(",", extract_keywords($text));

code that can be added to the prevous function. Probably is better to apply an "strip_tags" to each keyword.

Satollo
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: SEO plugin: a simple idea

Post by garvinhicking »

Hi!

Great, that's a good suggestion. I just built a plugin to do this. It also allows you to define meta-tags per entry, and only if those are not set it will fetch them using your functions.

Have a try:

http://nopaste.php-q.net/223644

It's a lot more complex because of those overriding meta-tags per entry. if that wasn't required, the plugin would be reduced to half the size. ;)

Best regards
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Harald Weingaertner
Regular
Posts: 474
Joined: Mon Mar 27, 2006 12:32 am

Post by Harald Weingaertner »

Great idea! Can we all use this plugin somehow?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Sure, it got committed to Spartacus and will be there in a few days. I'll also announce it on the blog.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
satollo
Regular
Posts: 13
Joined: Wed Jul 05, 2006 9:41 am
Contact:

Re: SEO plugin: a simple idea

Post by satollo »

It works great!

Now we can add some configuration, like the maximum number of characters/words to use for a description, or the html tags to use to identify the keywords, and so on!

Thank you very much!

By Satollo.
satollo
Regular
Posts: 13
Joined: Wed Jul 05, 2006 9:41 am
Contact:

Re: SEO plugin: a simple idea

Post by satollo »

Just a note: I get this warning:

Warning: implode(): Bad arguments. in /home/mhd-01/www.naturalmentedonna.com/htdocs/diario ... tadesc.php on line 154

only on blog index (http://www.naturalmentedonna.com/diario). Can you check it?

Thank you, Satollo!
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: SEO plugin: a simple idea

Post by garvinhicking »

Hi!

The foreach bug I fixed here:

http://nopaste.php-q.net/223670

For the configuration: Maybe you can help me there? You could modify the code to use substitution placeholders and then I tell how you to make them configurable.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
satollo
Regular
Posts: 13
Joined: Wed Jul 05, 2006 9:41 am
Contact:

Re: SEO plugin: a simple idea

Post by satollo »

garvinhicking wrote:Hi!

The foreach bug I fixed here:

http://nopaste.php-q.net/223670

For the configuration: Maybe you can help me there? You could modify the code to use substitution placeholders and then I tell how you to make them configurable.

Best regards,
Garvin
ok, give me some time, i write down more generic functions. do i have to post them here?
Gamersea
Regular
Posts: 65
Joined: Thu Jun 15, 2006 8:52 am

Re: SEO plugin: a simple idea

Post by Gamersea »

garvinhicking wrote:Hi!

The foreach bug I fixed here:

http://nopaste.php-q.net/223670

For the configuration: Maybe you can help me there? You could modify the code to use substitution placeholders and then I tell how you to make them configurable.

Best regards,
Garvin
Will that also be fixed for the spartacus?
satollo
Regular
Posts: 13
Joined: Wed Jul 05, 2006 9:41 am
Contact:

Re: SEO plugin: a simple idea

Post by satollo »

This function

function extract_keywords($text, $tag_names="b,strong")
{
$tags = split(",", $tag_names);
$tags_count = count($tags);
$result = array();
for ($i=0; $i<$tags_count; $i++)
{
preg_match_all('/<' . $tags[$i] . '>([^>]*)<\/' . $tags[$i] . '>/si', $text, $match);
$results = array_merge($results, $match[1]);
}
return $results;
}

can be used to make the html tag to consider as a second parameter. I think it's easy to make the plugin configurable with a list of tag comma separated.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: SEO plugin: a simple idea

Post by garvinhicking »

Hi!

Thanks for that patch! I made it configurable and committed it to the plugin.

The new version will be found here in a few minutes:

http://php-blog.cvs.sourceforge.net/php ... t_metadesc

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Gamersea
Regular
Posts: 65
Joined: Thu Jun 15, 2006 8:52 am

Post by Gamersea »

It's not update in the spartacus yet... and I can't manually edit the file so my host have to do it.... :oops:
Gamersea
Regular
Posts: 65
Joined: Thu Jun 15, 2006 8:52 am

Post by Gamersea »

There is a small bug if you don't fill in any description or I have update the serendipity_event_metadesc.php file wrong.

bug:
Warning: implode(): Bad arguments. in /You/may/not/see this directory/html/plugins/serendipity_event_metadesc/serendipity_event_metadesc.php on line 109

Edit however I don't know that much of it (yet) but the problem is in this rule:

Code: Select all

        $meta_keywords = implode(',', $this->extract_keywords($GLOBALS['entry'][0]['body']));
Post Reply