Jump to content

Use just first keyword in route to page but not the rest of them


Master-Overlord

Recommended Posts

Hey there, I played little bit with routes of pages to get nicer urls and I think I found wrong logic with meta_keywords as if you decide to use keywords for page and if you have more than one your url will be destroyed.

So here is my custom override for Prestashop 8.2 to use just first keyword and ignore all others but not remove them from being used on page regarding seo aspect (<meta name="keywords" content="tag1, tag2, tag3,....etc.)

So far it is working perfectly on PS 8.2 and my site on 2 languages and it solved my issue with having to use content/category on my urls.

in Traffic & SEO route to page is set as:

{meta_keywords:/}{id}-{rewrite}

Override is made in: /override/classes/Link.php (created new file as Link.php I have not used.

The only thing I can not figure out is how to match url from browser and ones added to meta alternative one as they are reading all keywords as in example bellow.

<link rel="alternate" href="https://mhf.rs/en/sss-11111-11111111-8" hreflang="en-us">
<link rel="alternate" href="https://mhf.rs/sr/sss-11111-11111111-8" hreflang="sr-Latn-RS">

Tell me what you think should I add something else if there is a flaw I do not see now.

Thanks!

 

<?php

class Link extends LinkCore
{
    public function getCMSLink(
        $cms,
        $cmsCategory = null,
        $alias = null,
        $ssl = false,
        $idLang = null,
        $relativeProtocol = false
    ) {
        // If $cms is just an ID, create an object of the CMS class.
        if (is_numeric($cms)) {
            $cms = new CMS($cms, $idLang);
        }

        // Check if $cms is truly an instance of the CMS class.
        if (!($cms instanceof CMS)) {
            // Ako nije, vraćamo se na originalnu metodu
            return parent::getCMSLink($cms, $cmsCategory, $alias, $ssl, $idLang, $relativeProtocol);
        }

        // Check if the field meta_keywords exists at all.
        if (!property_exists($cms, 'meta_keywords')) {
            return parent::getCMSLink($cms, $cmsCategory, $alias, $ssl, $idLang, $relativeProtocol);
        }

        // Trim meta_keywords only if it is set and not empty.
        if (isset($cms->meta_keywords) && !empty($cms->meta_keywords)) {
            
            // If it is an array, take only the first element.
            if (is_array($cms->meta_keywords)) {
                $firstKeyword = reset($cms->meta_keywords); // reset() daje prvi element niza
                $cms->meta_keywords = is_string($firstKeyword) ? trim($firstKeyword) : '';
            }
            // If it is a string, split it by commas and take the first word.
            elseif (is_string($cms->meta_keywords)) {
                $parts = explode(',', $cms->meta_keywords);
                $cms->meta_keywords = trim($parts[0]);
            }
            // If it is neither an array nor a string, set the value to empty.
            else {
                $cms->meta_keywords = '';
            }

            // If the result is empty after everything, assign a default value.
            // depending on the language ID.
            if (empty($cms->meta_keywords)) {
            // Check the language ID and assign different values
            // Adjust these IDs to match the actual language IDs on your site.
                if ($idLang == 1) {
                    // in my case Serbian
                    $cms->meta_keywords = 'strana';
                } elseif ($idLang == 5) {
                    // npr. engleski
                    $cms->meta_keywords = 'page';
                } else {
                    // Default value for other languages.
                    $cms->meta_keywords = 'cms';
                }
            }
        }

        // 6. Return to the original method with potentially modified meta_keywords.
        return parent::getCMSLink($cms, $cmsCategory, $alias, $ssl, $idLang, $relativeProtocol);
    }
}

 

 

Edited by Master-Overlord
fixed wording (see edit history)
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...