Jump to content

Native Way to Adjust Settings for URL/Slug Generation?


Recommended Posts

Hi there,

Is there a native way to adjust how a page's name/url/slug is generated as you type? I'd like it to function similarly to WordPress, which filters out stop words like, "a", "to", "the", etc.

If there's not a way to do it natively, would you recommend a module that hooks into the event of when a new page is added?

A page's name seems like a pretty vital part of ProcessWire. So, I'm a bit hesitant about messing with it at all! What do the seasoned PW developers out there think? I'm I asking for problems by messing with the page name?



  • Like 1
Link to comment
Share on other sites

Hi Lauren,

You'll need to hook into ProcessPageAdd::buildForm and potentially ProcessPageEdit::buildForm if you also want to change the name when the title is changed during a later edit.

You can get an idea of how to do this from my Page Rename Options module (https://github.com/adrianbj/PageRenameOptions/blob/master/PageRenameOptions.module#L65)

You can see that I have added some JS to override the native functionality when it comes to naming the pages.

You will be looking to add to/override the functionality in these files:



Hope that helps to get you started.

There is nothing wrong with messing with the page name - in fact it is possible to manually edit it by hand. The one thing that some don't agree on is whether it should ever be changed after it is originally created. I would personally rather the name matched the title, but others think it should never change due to SEO and broken links. I make use of the Page Path History module to deal with these - maybe not perfect, but typically the only time the titles will change is during development, so I am ok with it.

  • Like 6
Link to comment
Share on other sites

Hi Lauren,

I have thrown something together for you. It isn't well tested yet, but seems to be working fine. You can edit the StopWords.js file if you want to adjust the words that are removed. If there is general interest in this module, I might consider making it configurable.

Let me know how it goes for you.


Edited by adrian
New version
  • Like 7
Link to comment
Share on other sites

This is interesting because I was approaching some advnced automatic naming behind the scenes only last night during the page save process (so ignoring giving the visual indicator this will on the edit page) but in my case that was find because those pages needed no user control over the page names.

Very nice idea and keeps URLs shorter but I'm not sure it makes a difference to SEO any more to be honest, though I am no expert. Still, ProcessWire is all about providing the tools that in turn can provide more options so good work :)

  • Like 2
Link to comment
Share on other sites

Thanks Pete - I am not sure about the effect of removing StopWords on SEO either, but seemed like a nice quick module to whip up so thought I'd help out :)

Actually I have just revised the module. The first iteration completely overrode the InputfieldPageName.sanitize method. This new version just adds and applies a new method for removing the stopwords. This should be much safer in that we don't need to worry about overriding any updates that are made to that method in the PW core.

New version attached in my post above.

Link to comment
Share on other sites

Good work on helping out of course - I was more thinking out loud.

I think having just read up on it quickly it doesn't seem to matter much - see the final reply to the chosen answer here (which disagrees with that answer): http://stackoverflow.com/questions/9734970/better-seo-to-remove-stop-words-from-an-articles-url-slug as well as the comment further down about the fact that StackOverflow don't do it. Having checked that, nor do Slashdot and some other big sites.

In fact most search results about stopwords seem to relate to Wordpress plugins rather than anything official from Google saying it makes any difference.

I think it's one of those things that may have mattered in the past but not so much now. There are certainly some respectable SEO companies out there who aren't removing the stopwords from their own website URLs either.

But please don't take my word for it - as I say I was just thinking out loud and know very little about the subject so if someone finds a definitive answer somewhere from Google themselves then please do share as the short research I did wasn't really conclusive.

  • Like 4
Link to comment
Share on other sites

I have always tended to look at these as more of a problem in titles and so on than in URLs  - if you have a lot of "of" "in" "at" and so on, your titles are going to be waffly and probably too long for good SEO. Making the Title of the page neat and sensible means the resulting name will be the same - it is a copy writing problem.

Over use can also make bad copy - when being attentive to SEO, you should first and foremost be attentive to the audience. If removing all stop words from a URL or title turns it into gibberish, you have not done yourself any favours from either the SEO or readability point of view.

From the little I know, it seems like these days Google et al do not just remove all stop words - they have lists of phrases where stop words should be left alone and generally seem to be growing a more pragmatic approach to everything.

In these sorts of circumstances human editing is much better than automation. 

  • Like 3
Link to comment
Share on other sites

 If removing all stop words from a URL or title turns it into gibberish, you have not done yourself any favours from either the SEO or readability point of view. 

That sounds like the key issue in all this to me - very good point!

And Pete - thanks for those links.

Lauren will have some thinking to do when she finally gets back to this thread :)

  • Like 1
Link to comment
Share on other sites


Lauren will have some thinking to do when she finally gets back to this thread :)

Indeed! Wow, such insightful, helpful answers. Thank you everyone!

Adrian, the module you whipped up...so cool and super simple too. Love it. I installed it and tested it out; it's exactly what I was hoping for when I first started thinking about how to accomplish it. I was hoping you could modify the built-in JS behavior like that. Really nifty.

Also, really excellent points about the SEO aspect. The main reason behind wanting to remove stop words was more so for the benefits that come along with having shorter URLs. All though, in the StackOverflow question that Pete referenced, I thought the following was a really good point:

"Keep them in your URL. Even though Google may ignore them in normal search they do not when someone does an exact match search (i.e. using quotes)."

It's so worthwhile posting questions on this forum, because I always walk away with way more knowledge about PW than I expected. Thank you everyone :biggrin:

  • Like 3
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By celfred
      I can't understand how this destructive filter() work.
      Here's my code (with annotations to explain my issue) :
      $teamPlayers = $pages->find("parent.name=players, team=$team"); bd($monster->name.':'.$teamPlayers->count()); // There should be 24 players in my team (which is the case on first call) foreach ($teamPlayers as $p) { $result = $p->child("name=tmp")->tmpMonstersActivity->get("monster=$monster, bestTime>0"); if ($result) { $p->bestTime = $result->bestTime; } else { $p->bestTime = 0; } } $teamPlayers->filter("bestTime>0")->sort("bestTime"); // I want to return only the list of players having a best time on this monster return $teamPlayers;  
      My problem is that after several calls (for all monsters) the number of players in the team is not 24 but 1… I guess the $teamPlayers never resets and I can't understand why line 1 doesn't start with 24 again…
      If I code this for the 2 last lines, it works :
      $teamMates = $teamPlayers->find("bestTime>0")->sort("bestTime"); return $teamMates; But I would like to understand what's going on with my 1st version (I guess it is the destructive aspect of filter()) 😞
      So if someone could take a little time to explain, I would appreciate 🙂
    • By Liam88

      After years of just playing around with Processwire I have asked 3 q's in the same week. It's all about working with forms, parameters etc and so I'm hoping this ordeal is nearly over!
      I currently have a checkbox filter:
      <form id="abFilter" method="get" role="form" action="'.$page->url().'"> <div class="list-group"> <h3>Content Type</h3>'; $cont = $fields->get('ab_content'); $contents = $cont->type->getOptions($cont); foreach($contents as $ab_cont){ echo' <div class="list-group-item checkbox"> <input type="checkbox" class="" id="'.$ab_cont->title.'" name="content" value="'.$ab_cont->title.'"'; if (in_array($ab_cont->title, $contArray)){ echo "checked"; } echo'> <label for="'.$ab_cont->title.'">'.$ab_cont->title.'</label> </div>'; } echo' </div>'; //end of filter 1 //start of filter 2 echo' <div class="list-group"> <h3>Channels</h3>'; $chan = $fields->get('ab_channels'); $channel = $chan->type->getOptions($chan); foreach($channel as $ab_chan){ echo' <div class="list-group-item checkbox"> <input type="checkbox" class="" id="'.$ab_chan->title.'" name="channel" value="'.$ab_chan->title.'"'; if (in_array($ab_chan->title, $chanArray)){ echo "checked"; } echo'> <label for="'.$ab_chan->title.'">'.$ab_chan->title.'</label> </div>'; } echo' </div>'; ?> <button id="select">Get Checked Checkboxes</button> </form><!-- end of form --> I also have a piece of script which selects all the checkboxes and then outputs them into readable parameters for the URL which then passes into the $inputs. The reason for the script is to not have duplicate filters like ?ab=1&ab=2 and the script changes it to ab=1_2 which on the input gets exploded into an array. 
      document.querySelector("form").onsubmit=ev=>{ ev.preventDefault(); let o={}; ev.target.querySelectorAll("[name]:checked").forEach(el=>{ (o[el.name]=o[el.name]||[]).push(el.value)}) console.log(location.pathname+"?"+Object.entries(o).map(([v,f])=>v+"="+f.join("_")).join("&")); document.location.href = location.pathname+"?"+Object.entries(o).map(([v,f])=>v+"="+f.join("_")).join("&"); } Here is $inputs and so on on the page:
      //Default selector to get ALL products $baseSelector = "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2"; $selector = "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2"; $input->whitelist('channel',explode("_", $channel)); // Use this to append to the $items filter if($channel){ $chanArray = explode("_", $channel); $chan = $channel = str_replace('_', '|', $channel); $selector = $selector .= ",ab_channels=$chan"; } $test = $pages->find($selector); // This is just testing if the $selector choise returns and if not use page filter without filters. if(count($test) > 0){ $items = $pages->find($selector); // $items with the parameter filter // Example - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2,ab_channels=facebook-ads" // Example (multi choice) - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2,ab_channels=facebook-ads|instagram-ads" // Example (with other filters) - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2,ab_channels=facebook-ads,ab_content=video|static" }else{ $items = $pages->find($baseSelector); // Example - "template='adbank_pages',sort=published,include=all,status!=hidden,limit=2" } $total = $items->getTotal(); I have stripped out a few of the other filters from the above to try keep it a little more concise (haha). Now I appreciate the post may be long but here we are at the end!
      The URL I get on page 1 of the filter results would look like: example.com/blog/?channel=facebook-ads_instagram-ads
      If I click page 2 the url changes to - example.com/blog/page2/?channel=
      If I then click back to page 1 it changes to - example.com/blog/?channel=
      So I'm hoping you can see my problem and hoping someone can assist. I need to work out how to keep the parameters in the url but also if I remove that filter for that parameter to remove.
      This whole process works without pagination but with pagination it has a different behaviour.
      Thank you in advance
    • By Saleena Jhon
      Hello There, I have saw a post that was covering event-calendar with php, ajax and js. That was showing a monthly overview when I click on a "month" button or when I switch the month. And show the events on one particular date when I pick a day. Also, most events are kind of exhibitions and so they have a start date and an end date much later, and occur on each day in-between as well. So on the template I put two date picking fileds date_start and date_end. Is there an elegant way to select the events using the API? If yes, kindly help me out.
      Thanks in Advance
    • By humanafterall
      I have a URL field that will sometimes have relative/local URLs on a multilingual site, for example /contact/ 

      However the URL field does not seem to pick up when I'm on another language, for example /fr/ so I'm taken to the default language page for /contact/ rather than /fr/contact/
      Is there a way to make the URL fields play well with a multi-language site?
    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Note: Custom Paths is incompatible with the core LanguageSupportPageNames module. I have no experience working with LanguageSupportPageNames or multi-language sites in general so I'm not in a position to work out if a fix is possible. If anyone with multi-language experience can contribute a fix it would be much appreciated!

      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Install the Custom Paths module.
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
  • Create New...