Jump to content

MarkupSEO - The all-in-one SEO solution for ProcessWire.


Nico Knoll

Recommended Posts

7 minutes ago, tpr said:

I guess you could check $page->seo->description and if it's empty, set it to your likings, eg. in ready.php or in your template php file (latter untested).


if($page->seo_description === "") {
	$page->seo_description = 'whatever';
}

 

Thank you for the answer.

The problem is, that i activated the "automatically" method to insert the code on my page. And the module inserts the empty description metatag anyway.

I don't know, if your code example will help me in this case?

Link to comment
Share on other sites

Just fast from mobile....

You have to use a hook on pagesave and check the SEO fields there....I ve some functions for creating keywords and description from some given fields... May tomorow I can post an example...

Link to comment
Share on other sites

On 31.7.2017 at 0:29 PM, tpr said:

It should.

YES! It works!
Thank you very much!!!

I unchecked the option "smart description" an add the code below in my template.

<?
    if($page->seo_description === "") {
       // Remove HTML tags
       $description = strip_tags($page->mytext);

       // Remove linebreaks
       $description = preg_replace( "/\r|\n/", " ", $description );

       // Shorten text
       $description = substr($description, 0, 160)." …";

       // Set new metadescription
       $page->seo_description = $description;
    }
    ?>

 

Link to comment
Share on other sites

Ok great that it works for you....a more complex solution with a hook on saveReady if SEO fields are empty put some own magic in there....if tey not empty do nothing...

functions that i use:

Spoiler

/**
 * Wordlimiter cuts a textarea only after complete words not between
 * used in admin.php for seo function and in some templates
 */
function wordLimiter($str = '', $limit = 200, $endstr = '...'){

	if($str == '') return '';

	if(strlen($str) <= $limit) return $str;

	$out = substr($str, 0, $limit);
	$pos = strrpos($out, " ");
	if ($pos>0) {
		$out = substr($out, 0, $pos);
	}
	$out .= $endstr;
	return $out;

}

/**
 * Alternative with regex for striptags function
 * used in admin.php for seo function and in some templates
 */
function ripTags($string) {

    // ----- remove HTML TAGs -----
    $string = preg_replace ('/<[^>]*>/', ' ', $string);

    // ----- remove control characters -----
    $string = str_replace("\r", '', $string);    // --- replace with empty space
    $string = str_replace("\n", ' ', $string);   // --- replace with space
    $string = str_replace("\t", ' ', $string);   // --- replace with space

    // ----- remove multiple spaces -----
    $string = trim(preg_replace('/ {2,}/', ' ', $string));

    return $string;

}
      
/**
 * Finds all of the keywords (words that appear most) on param $str
 * and return them in order of most occurrences to less occurrences.
 * @param string $str The string to search for the keywords.
 * @param int $minWordLen[optional] The minimun length (number of chars) of a word to be considered a keyword.
 * @param int $minWordOccurrences[optional] The minimun number of times a word has to appear
 * on param $str to be considered a keyword.
 * @param boolean $asArray[optional] Specifies if the function returns a string with the
 * keywords separated by a comma ($asArray = false) or a keywords array ($asArray = true).
 * @return mixed A string with keywords separated with commas if param $asArray is true,
 * an array with the keywords otherwise.
 */
function extractKeywords($str, $minWordLen = 5, $minWordOccurrences = 2, $asArray = false)
{
	function keyword_count_sort($first, $sec)
	{
		return $sec[1] - $first[1];
	}
	$str = preg_replace('/[^\p{L}0-9 ]/', ' ', $str);
	$str = trim(preg_replace('/\s+/', ' ', $str));

	$words = explode(' ', $str);
	$keywords = array();
	while(($c_word = array_shift($words)) !== null)
	{
		if(strlen($c_word) < $minWordLen) continue;

		$c_word = strtolower($c_word);
		if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++;
		else $keywords[$c_word] = array($c_word, 1);
	}
	usort($keywords, 'keyword_count_sort');

	$final_keywords = array();
	foreach($keywords as $keyword_det)
	{
		if($keyword_det[1] < $minWordOccurrences) break;
		array_push($final_keywords, $keyword_det[0]);
	}
	return $asArray ? $final_keywords : implode(', ', $final_keywords);
}

 

the hook itself works in admin.php under site/templates/:

Spoiler

/**
 * Hook for changing seo_* fields if empty on Page Publish
 */
$pages->addHookAfter('saveReady', null, 'changeSeoFields');
function changeSeoFields(HookEvent $event) {
    $page = $event->arguments[0];
    //for this template only
    if($page->template == 'artikel'|| $page->template == 'seite') {
        // page is about to be published
        if($page->isChanged('status') && !$page->is(Page::statusUnpublished)) {
            // check if seo_title fields are exist and if they are empty
            if (!$page->seo_title) {
                //get the default value that would used as fallback
                if ($page->headline) {
                    //Headline field is used for seo_title
                    $default_title = ripTags($page->headline);
                } else {
                    //Headline field empty - page title is used
                    $default_title = ripTags($page->title);
                }
                //set seo_title on publishing
                $page->set ('seo_title', $default_title);
            }
            // check if seo_description fields are exist and if they are empty
            if (!$page->seo_description) {
                //get the default value that would used as fallback
                if ($page->kurztext) {
                    $default_desc = ripTags($page->kurztext);
                } else {
                    //is there a main content block as pagetable field pt_inhalt?
                    if(count($page->pt_inhalt)>0) {
                        //get the first text field in the pagetable and use it for the seo description
                        $first_item = $page->pt_inhalt->get("template=part_text|part_text_image_left|part_text_image_right|part_text_spalten");
                        $first_content = ripTags($first_item->text);
                        $default_desc = wordLimiter($first_content, 160);
                    }
                }
                //set seo_description on publishing
                $page->set ('seo_description', $default_desc);
            }
            // check if seo_keywords fields are exist and if they are empty
            if (!$page->seo_keywords) {
                //get the string that should be used to generate automatical keywords for this page
                //we use all textfields in the pagetable from the main content and collect them
                $keyword_texts = '';
                //is there a main content block as pagetable field pt_inhalt?
                if(count($page->pt_inhalt)>0) {
                    foreach ($page->pt_inhalt as $keyword_text) {
                        if ($keyword_text->title) $keyword_texts .= $keyword_text->title;
                        if ($keyword_text->text) $keyword_texts .= $keyword_text->text;
                        if ($keyword_text->text2) $keyword_texts .= $keyword_text->text2;
                        if ($keyword_text->text3) $keyword_texts .= $keyword_text->text3;
                    }
                } else {
                    $keyword_texts = $page->headline.' '.$page->kurztext.' '.$page->title;
                }
                //rip tags
                $keyword_string = ripTags($keyword_texts);

                $default_keywords = extractKeywords($keyword_string);

                //set seo_title on publishing
                $page->set ('seo_keywords', $default_keywords);
            }
        }
    }
}

 

As always it is not the best PHP code since im no professional....but it works for me....if i somedays get some time i could add something like that with optionfields to the module itself. ;)

Best regards mr-fan

  • Like 1
Link to comment
Share on other sites

1 hour ago, mr-fan said:

Ok great that it works for you....a more complex solution with a hook on saveReady if SEO fields are empty put some own magic in there....if tey not empty do nothing...

functions that i use:

  Hide contents


/**
 * Wordlimiter cuts a textarea only after complete words not between
 * used in admin.php for seo function and in some templates
 */
function wordLimiter($str = '', $limit = 200, $endstr = '...'){

	if($str == '') return '';

	if(strlen($str) <= $limit) return $str;

	$out = substr($str, 0, $limit);
	$pos = strrpos($out, " ");
	if ($pos>0) {
		$out = substr($out, 0, $pos);
	}
	$out .= $endstr;
	return $out;

}

/**
 * Alternative with regex for striptags function
 * used in admin.php for seo function and in some templates
 */
function ripTags($string) {

    // ----- remove HTML TAGs -----
    $string = preg_replace ('/<[^>]*>/', ' ', $string);

    // ----- remove control characters -----
    $string = str_replace("\r", '', $string);    // --- replace with empty space
    $string = str_replace("\n", ' ', $string);   // --- replace with space
    $string = str_replace("\t", ' ', $string);   // --- replace with space

    // ----- remove multiple spaces -----
    $string = trim(preg_replace('/ {2,}/', ' ', $string));

    return $string;

}
      
/**
 * Finds all of the keywords (words that appear most) on param $str
 * and return them in order of most occurrences to less occurrences.
 * @param string $str The string to search for the keywords.
 * @param int $minWordLen[optional] The minimun length (number of chars) of a word to be considered a keyword.
 * @param int $minWordOccurrences[optional] The minimun number of times a word has to appear
 * on param $str to be considered a keyword.
 * @param boolean $asArray[optional] Specifies if the function returns a string with the
 * keywords separated by a comma ($asArray = false) or a keywords array ($asArray = true).
 * @return mixed A string with keywords separated with commas if param $asArray is true,
 * an array with the keywords otherwise.
 */
function extractKeywords($str, $minWordLen = 5, $minWordOccurrences = 2, $asArray = false)
{
	function keyword_count_sort($first, $sec)
	{
		return $sec[1] - $first[1];
	}
	$str = preg_replace('/[^\p{L}0-9 ]/', ' ', $str);
	$str = trim(preg_replace('/\s+/', ' ', $str));

	$words = explode(' ', $str);
	$keywords = array();
	while(($c_word = array_shift($words)) !== null)
	{
		if(strlen($c_word) < $minWordLen) continue;

		$c_word = strtolower($c_word);
		if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++;
		else $keywords[$c_word] = array($c_word, 1);
	}
	usort($keywords, 'keyword_count_sort');

	$final_keywords = array();
	foreach($keywords as $keyword_det)
	{
		if($keyword_det[1] < $minWordOccurrences) break;
		array_push($final_keywords, $keyword_det[0]);
	}
	return $asArray ? $final_keywords : implode(', ', $final_keywords);
}

 

the hook itself works in admin.php under site/templates/:

  Hide contents

/**
 * Hook for changing seo_* fields if empty on Page Publish
 */
$pages->addHookAfter('saveReady', null, 'changeSeoFields');
function changeSeoFields(HookEvent $event) {
    $page = $event->arguments[0];
    //for this template only
    if($page->template == 'artikel'|| $page->template == 'seite') {
        // page is about to be published
        if($page->isChanged('status') && !$page->is(Page::statusUnpublished)) {
            // check if seo_title fields are exist and if they are empty
            if (!$page->seo_title) {
                //get the default value that would used as fallback
                if ($page->headline) {
                    //Headline field is used for seo_title
                    $default_title = ripTags($page->headline);
                } else {
                    //Headline field empty - page title is used
                    $default_title = ripTags($page->title);
                }
                //set seo_title on publishing
                $page->set ('seo_title', $default_title);
            }
            // check if seo_description fields are exist and if they are empty
            if (!$page->seo_description) {
                //get the default value that would used as fallback
                if ($page->kurztext) {
                    $default_desc = ripTags($page->kurztext);
                } else {
                    //is there a main content block as pagetable field pt_inhalt?
                    if(count($page->pt_inhalt)>0) {
                        //get the first text field in the pagetable and use it for the seo description
                        $first_item = $page->pt_inhalt->get("template=part_text|part_text_image_left|part_text_image_right|part_text_spalten");
                        $first_content = ripTags($first_item->text);
                        $default_desc = wordLimiter($first_content, 160);
                    }
                }
                //set seo_description on publishing
                $page->set ('seo_description', $default_desc);
            }
            // check if seo_keywords fields are exist and if they are empty
            if (!$page->seo_keywords) {
                //get the string that should be used to generate automatical keywords for this page
                //we use all textfields in the pagetable from the main content and collect them
                $keyword_texts = '';
                //is there a main content block as pagetable field pt_inhalt?
                if(count($page->pt_inhalt)>0) {
                    foreach ($page->pt_inhalt as $keyword_text) {
                        if ($keyword_text->title) $keyword_texts .= $keyword_text->title;
                        if ($keyword_text->text) $keyword_texts .= $keyword_text->text;
                        if ($keyword_text->text2) $keyword_texts .= $keyword_text->text2;
                        if ($keyword_text->text3) $keyword_texts .= $keyword_text->text3;
                    }
                } else {
                    $keyword_texts = $page->headline.' '.$page->kurztext.' '.$page->title;
                }
                //rip tags
                $keyword_string = ripTags($keyword_texts);

                $default_keywords = extractKeywords($keyword_string);

                //set seo_title on publishing
                $page->set ('seo_keywords', $default_keywords);
            }
        }
    }
}

 

As always it is not the best PHP code since im no professional....but it works for me....if i somedays get some time i could add something like that with optionfields to the module itself. ;)

Best regards mr-fan

WOW, that seems to be the sophisticated version ... :-)

An optionfiled in the module would be great!!!

Best regards!

 

Link to comment
Share on other sites

  • 2 months later...

Can I revive the subject ? :)

I want to use this module for a website I inherited, (I didn't use the processwire before)

There is one thing that makes me uncertain.

In:  Choose the templates which should get a SEO tab 
There is a note Be careful with this field. If you remove an entry all of it's "seo_*" fields get deleted (including the data).

Is there any risk there

An do I have to type in the Author and Sitename?

2017-10-25_102907.jpg

Link to comment
Share on other sites

4 hours ago, Marudor said:

Is there any risk there

This simply means that if you assign a template that can use the SEO fields and then you actually use them on a page and then remove the template from the list, the SEO fields will be deleted from the template and, therefore, the pages assigned to it.

4 hours ago, Marudor said:

An do I have to type in the Author and Sitename?

I believe the module's <head> markup might require it, so yes, I'd fill those out.

Link to comment
Share on other sites

2 hours ago, Marudor said:

OK thanks

Just to clarify - The author here - means the author of the module or the author of the website  or it doesn't matter what I type in there.

I don't want to blow up the site...:-X

:lol: Author of the site. I think you can exclude it as I'd imagine the module won't include the relevant tag if it hasn't been specified. You'd need to "view source" in your browser to check if everything is okay. As far as I'm concerned, only the site title is important in terms of meta data.

Link to comment
Share on other sites

Ok I have chosen the template to MarkupSEO module.  

I does change the meta description and meta keyword which is very handy.

But it didn't change the meta title tag.

It's still the one withe the suffix added that comes from the homepage title 

Eventhough i have type in the short one Employer branding - pracownik staje się klientem - Poznań, Warszawa

I still have Employer branding - pracownik staje się klientem - Agencja PR Q&A Communications – Poznań, Warszawa

Where is the problem ? Why id doesn't change the title tag????

Link to comment
Share on other sites

2 hours ago, Marudor said:

Ok I have chosen the template to MarkupSEO module.  

I does change the meta description and meta keyword which is very handy.

But it didn't change the meta title tag.

It's still the one withe the suffix added that comes from the homepage title 

Eventhough i have type in the short one Employer branding - pracownik staje się klientem - Poznań, Warszawa

I still have Employer branding - pracownik staje się klientem - Agencja PR Q&A Communications – Poznań, Warszawa

Where is the problem ? Why id doesn't change the title tag????

Changing the title in the SEO tab only affects the SEO title (<meta property="og:title" content="...SEO title...">). Your page title in the browser is the one you set in the <title> tag of your HTML. If you are referring to Google showing the old one, then you will need to wait a while for it to be re-indexed.

Edit: I apologise, it seems I'm incorrect. MarkupSEO will include a <title> tag for you if you fill out "Title Format" in that case, make sure you don't have your own <title> tag as well.

Link to comment
Share on other sites

I tough this is the module to change the meta title tag and meta description tag.

It changes the description and keyword all right...but the meta title  stays the same.

I  have the article title -  Employer branding - pracownik staje się klientem

But I want the meta title Employer branding - pracownik staje się klientem - Poznań, Warszawa

At the moment the meta title on every psgr is a mix of 2  - the article title and the suffix from the homepage title Agencja PR Q&A Communications – Poznań, Warszawa

I have even changed it in the code from 

<title><?php if($page->id > 1) { echo $page->title .' - '; } echo $home->title ?></title>
TO
<title><?php echo $page->title; ?></title>

BUT still the same. The mix of title with the suffix Agencja PR Q&A Communications – Poznań, Warszawa

added to all article titles makes the meta tag title too long. It Is more than 70 characters on every page. which is no good.

Link to comment
Share on other sites

21 hours ago, Marudor said:

<title><?php if($page->id > 1) { echo $page->title .' - '; } echo $home->title ?></title>
TO
<title><?php echo $page->title; ?></title>

BUT still the same.

It sounds like you need to change it somewhere else. Because ProcessWire based sites always depend on the developer to implement the frontend, it can be tricky to find the right piece of code. Have you tried something like  <title>AAAAAA<?php echo $page->title; ?></title> for example? As a last resort I always type in some fake string which can be easily searched. If it is not in the source code of the rendered page then that is not right part of the code to change it. You probably need to dig in more, unfortunately.

Link to comment
Share on other sites

  • 3 months later...

Hello, I found & fixed an issue wich gives you an illegal string offset error in the latest PHP version. (7.1). 

Around line 429 you declare $return = ''; and then fill it up as an array doing $return[$key] = $value; at line 435.

All you got to do is declare $return = array(); instead of $return = ''; and it should be fixed. Filling up an empty string as an array caused the errors. Can you fix this and push it to github? :)

Kind regards,
Jonathan

 

	private function parseCustom($custom) {
		if(trim($custom) == '') return;

		$return = array();
		$lines = explode(PHP_EOL, $custom);
		foreach($lines as $line) {
			list($key, $value) = explode(':=', $line);
			$key = preg_replace('%[^A-Za-z0-9\-\.\:\_]+%', '', str_replace(' ', '-', trim($key)));	
			$value = trim(wire('sanitizer')->text(html_entity_decode($value)));

			$return[$key] = $value;
		}
		
		return $return;
	}

 

  • Thanks 2
Link to comment
Share on other sites

  • 3 weeks later...

Hi,
I'm having a problem with this module. I't installed with Processwire ver3.
The problem is -> on localhost plugin is working fine, but on live server plugin is not working (strange is that it was working fine, but stopped without any reason ):
when i type title, keywords and description and click SAVE, page is reloaded without errors, but i see old entries, so cannot update existing entries.
I was able to edit Title and description, but next attempts fails. So it saves SEO daya randomly, sometimes im able to edit and save, and sometimes it will just return previous values.

Does anybody has this issue ? Thanks.

Actually im seeing this on edit page in SEO TAB

image.png.2c9ee50f8dfef7641101278db0765e35.png

Link to comment
Share on other sites

  • 3 weeks later...

If anyone is interested, I have a new fork of this module available here: https://github.com/adrianbj/MarkupSEO/commits/various-fixes-enhancements

It's very much a work in progress, but it includes several bug fixes and lots of new features. Please read the commit log to learn about what's fixed and what's new.

At the moment you should not upgrade an existing site (new fields won't be created) and probably don't use it on a live site just yet.

It's not well tested at all yet, so use at your own peril :) 

I'd really appreciate any feedback on it.

  • Like 14
  • Thanks 2
Link to comment
Share on other sites

@adrian

Hi Adrian, thank you for the awesome updates and improvements.

I would suggest to take a look on the Canonical link field and make it ready for multi language websites .

Meaning that he can detect if there are any other languages present and canonicalize them properly according to the languages. 

 

  • Like 1
Link to comment
Share on other sites

3 hours ago, B3ta said:

I would suggest to take a look on the Canonical link field and make it ready for multi language websites .

Hi @B3ta - I'm afraid I don't really have time to look into this at the moment. I am extending this module for my needs and I rarely have multi-language websites so it's not high on my list. I am happy to accept PRs though so together we can build a better version of this module. Once we are all happy with the stability/functionality of the new version I'll try contacting Nico to see what we can do about getting it into the main repo. Hope you understand where I am coming from.

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...
30 minutes ago, neosin said:

@Nico Knoll for the images, it requires a URL; I am curious if I change those to file upload field will it affect the module? just want to confirm before I try, thank you

In my fork (linked above), the field is an images field (for the page specific SEO tab, not the default image).

  • Like 1
Link to comment
Share on other sites

49 minutes ago, adrian said:

In my fork (linked above), the field is an images field (for the page specific SEO tab, not the default image).

thank you, I will check it out now

@adrian I've uninstalled the SEO and installed your version however the SEO image fields still show up as url text fields. Do I need to change the fields to images?

Link to comment
Share on other sites

  • 4 weeks later...

Hi,

EDIT: Found it, change it in the module configuration. Can't be overwritten by field definition.

 

i cannot change the length of the seo_title field. I changed the field max length to 100 but it is still 60 at the input field. i tried to remove the counter to check, does not work. Tried to change the title and description of the field, this works. I'm confused.

 

Thx for any help!

seotitle.jpg

Link to comment
Share on other sites

  • 2 weeks later...
On 3/25/2018 at 8:48 AM, neosin said:

thank you, I will check it out now

@adrian I've uninstalled the SEO and installed your version however the SEO image fields still show up as url text fields. Do I need to change the fields to images?

Sorry I missed this - not getting emails on @ pings anymore. If you upgrade from an old install of this module, you will need to manually change the field type.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...