Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2012-10-18 21:25:39

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: Forcing the Article ID in the URL-only title field

uli wrote:

springworks, because this is a kind of publication here and you’ve already thought of using a prefix, I reserved it for you.

Oh that’s very decent of you, thanks!

Don’t go rushing to use the code I posted earlier though, I’ve spotted a few issues with it!

I’ll post a finished version up here when I’ve thought through it a bit more.

Offline

#14 2012-10-19 08:49:17

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: Forcing the Article ID in the URL-only title field

And here’s the final version:

new spr_custom_url_title();

class spr_custom_url_title {

	/**
	 * Constructor
	 */

	public function __construct() {
		register_callback(array($this, 'update'), 'article_saved');
		register_callback(array($this, 'update'), 'article_posted');
	}

	/**
	 * Updates URL title
	 * @param string $event
	 * @param string $step
	 * @param array $r
	 */

	public function update($event, $step, $r) {
		if ((!($r['custom_4']=="")) && ($r['Section']=="<selected_section>")) {
			$new_url_title = stripSpace($r['custom_4'], 1)."-".stripSpace($r['Title'], 1);
			safe_update('textpattern', "url_title = '$new_url_title'", 'ID = '.intval($r['ID']));
		}
	}
}

So every time you publish a new article or update an existing one in the selected section, the plugin creates a custom URL title by joining the contents of custom field 4 to the article title.

I’m really please I sorted this out. I can already think of another site where I could use a modified version of this to make my client’s life a lot easier.

It’s probably a bit too esoteric to consider releasing as a plugin. Also, I’m not sure what the best way of generalising it would be in terms of choosing what parameters to use to construct the custom URL title and under what conditions it should be done.

Still if there is demand for it, I’ll have a go and see what I can come up with.

Offline

#15 2013-02-07 18:05:03

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Forcing the Article ID in the URL-only title field

springworks coded:

<snip> ['Section']=="<selected_section>" </snip>

(from the public function update block, one post above)

I’m trying to understand this for potential future alterations. What condition on the Write panel does this describe, or, the other way around: When is this condition not met?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#16 2013-02-07 18:25:39

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: Forcing the Article ID in the URL-only title field

uli wrote:

springworks coded:

<snip> ['Section']=="<selected_section>" </snip>

(from the public function update block, one post above)

I’m trying to understand this for potential future alterations. What condition on the Write panel does this describe, or, the other way around: When is this condition not met?

Sorry, I should have made this a bit clearer!

<selected_section> is replaced with the section name where you want to customise the URL title. As I’ve only used this on one site, I hard-coded the section name in there. When new articles are created in that section, the URL title is created from a concatenation of the contents of custom field 4 and the article title.

Obviously to make this more useful, it would be good to have some way of choosing how the custom URL was created (i.e. what elements were used to make it) and what sections it was active in. (If I ever get around to doing that, it would be a handy plug-in to release, but until then, at least people can cut and paste my code and tweak it themselves!)

Hope that helps!

Offline

#17 2013-02-07 19:41:15

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Forcing the Article ID in the URL-only title field

Thanks, springworks. I guessed it had to be a placeholder but then I don’t know PHP syntax overly well.

springworks wrote:

Sorry, I should have made this a bit clearer!

Noone can figure a dodo like me would try to “read” that code without a coding background. But with a backup copy of everything at hand I outgrow myself ;)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB