Jump to content

Change auto-format of page 'title' field


Ryan Pierce
 Share

Recommended Posts

Hi Ryan,

I am not sure whether this is easily possible within PW or not - perhaps someone else will chime in on that front. However, and maybe I am telling you something you already know, and maybe you have a specific scenario and a good reason for wanting this, but dashes in SEO friendly URLs are important as they are treated as a word separator. The other alternative that is usually used is an underscore, but it seems the general consensus is that dashes/hyphens are better.

http://themetaq.com/articles/seo-under-scrutiny-are-hyphens-or-underscores-better-word-separators

Sorry if this is all terribly obvious to you already ;)

  • Like 1
Link to comment
Share on other sites

to answer your question, it shouldn't be difficult to override this behavior because it all happens wit javascript. I wrote these three lines very quickly in the browser console. It's not good code and not necessarily how you should do it, it's not removing url forbidden characters for instance, but it shows that it's possible to do what you want only by adding javascript to the admin page.

edit: With base on Ryan's post under, this code would break important functionality. I will leave it there, but don't use it.

function replaceVal(){$('#Inputfield_name').val($("#Inputfield_title").val().replace(/\s/g, "").toLowerCase());}

$("#Inputfield_title").keyup(function(){replaceVal()});

$("#submit_save").click(function(){replaceVal()});

Link to comment
Share on other sites

Never underestimate the importance of word separation. It's the difference in readability between "pen-is-broken" and "penisbroken". Beyond the potential misunderstandings is raw readability both to users and search engines. While I have a readability preference for underscores, my understanding is that hyphens are the best balance when all factors are taken into consideration. 

Lastly, this may be obvious, but what gets populated in that name box is only an auto-generated suggestion. After populating the title, one can go modify the name as they see fit (which I often do), before saving the page. 

  • Like 6
  • Haha 1
Link to comment
Share on other sites

Love the example :D

Lastly, this may be obvious, but what gets populated in that name box is only an auto-generated suggestion. After populating the title, one can go modify the name as they see fit (which I often do), before saving the page.

I forgot this detail and my example would certainly ruin any change...

Link to comment
Share on other sites

I'll ask here, think my question is similar.

I want to add ID to pages's name in 1 template (exactly like on this forum "/3276-change-auto-bla-bla-etc/").

So I need to write a module with hooks for this or what? Please, push me to the right direction.

upd:

made it this way:

<?php
class PrefixToPagename extends WireData implements Module {

    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo() {

        return array(
            'title' => 'Add prefix (id) to the page name',
            'version' => 100,
            'summary' => 'Add prefix (id) to the page name',
            'href' => '',
            'singular' => true,
            'autoload' => true,
            );
    }

    /**
     * Initialize the module
     *
     * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
     * when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
     *
     */
    public function init() {

        // add a hook before the $pages->save, to add prefix if needed
        $this->pages->addHookBefore('save', $this, 'addPrefix');
    }

    /**
     * Hooks into the pages->save method and sorts pages with a certain template based on certain criteria
     *
     */
    public function addPrefix($event) {
        $page = $event->arguments[0];
        
        // Only run if the page we just saved has the "vacancy" template
        if ($page->template == 'vacancy') {
			$oldname = explode ('-', $page->name);
			if ($oldname[0] != $page->id){
				$page->name = $page->id . '-' . $page->name;
			}
        }
    }
}

thanks guys from this thread.

Edited by k07n
  • Like 1
Link to comment
Share on other sites

I have an existing non PW site that I want to convert to PW. But even though the menu structure has depth, the URL structure is flat.

For example, what should be /about-us/our-team is simply /our-team

Rather than dealing with the 301 nightmare, it would be nice to change PW so that it wouldn't prepend the parent's URL.

And I don't want to "teach" the client to override the URL for every page that is created.

Any suggestions?

  • Like 1
Link to comment
Share on other sites

@jmart - if there are not a lot of pages, don't nest the pages in the admin, but then make a custom menu.

That's how it do it on 3 sites now. (in other words make all of the pages child of the root.)

As far as making the custom menu, i do it the way discussed in this thread, and works perfectly. You have to adjust the code to your specific menu needs..

http://processwire.com/talk/topic/2787-custom-menu-not-related-to-page-tree/

i also wanted to mention that once you have a lot of pages in the admin, you could use Soma's datatable to look for pages, instead of the tree; then you could filter/sort by some criteria (title, template); so in this sense you would using PW as more of a bucket system... which for some projects is good, and PW handles it fine...

Edited by Macrura
  • Like 2
Link to comment
Share on other sites

@k07n: The module looks good. But start your class with an uppercase character, otherwise it might confuse PW's module parser, which is expecting it for categorization purposes. 

@jmart: 301 would really be the right way to do it and would not be a nightmare. Use Apeisa's redirects module and life will be easy. But if you really need all pages to be accessible at root level (which is not something I'd recommend) you could accomplish it by editing your 'home' template, clicking to the 'URLs' tab and enabling URL segments. Then in your home.php template:

if($input->urlSegment1) {
  $p = $pages->find("name=" . $sanitizer->pageName($input->urlSegment1))->first(); 
  if($p) {
    $session->redirect($p->url); // 301 redirect to it 
    // echo $p->render(); // or output it here, no redirect
  } else {
    throw new Wire404Exception();
  }
} else {
  // render homepage
} 
  • 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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...