Textpattern tips, tutorials and code snippets

Automated custom titles for your articles

Did you ever wish your clients will not need to write the same content twice, once in the title field, and another time in the custom fields?

Take a look at a video below which explains more, or continue the tutorial.

For example:
Let’s say your client is a record label owner, who need to publish articles of all the releases to the “Discography” section, now currently the client will probably need to fill the artist-name and the release-title of each release in both the article-title field and in custom-fields, which is absurd.

So in this tip I will show you how to create the articles titles from the data in the custom-fields and others.

First step

Install the bot_write_tab_customize plugin, and jump into its UI (Extensions > bot_wtc) then click on Additional js code

Inside the small window that just opened and paste the following code:

<script type="text/javascript">
	$(document).ready(function() {

		// Auto custom title	
		$('#page-article input.publish').click(function(){
			var section = $('select#section.list option:selected').val();
			var category = $('select#category-1.list option:selected').val();
			var title = $('#title').val();


			// Custom fields
			var cf1_Artist_Name   = $('input[name=custom_1]').val();
			var cf2_Release_Title = $('input[name=custom_2]').val();
			var cf3_Record_Label  = $('input[name=custom_3]').val();
			var cf4 = $('input[name=custom_4]').val();
			var cf5 = $('input[name=custom_5]').val();
			var cf6 = $('input[name=custom_6]').val();
			var cf7 = $('input[name=custom_7]').val();
			var cf8 = $('input[name=custom_8]').val();
			var cf9 = $('input[name=custom_9]').val();
			var cf10 = $('input[name=custom_10]').val();


			// Date fields
			var day = $('input[name="day"]').val();
			var month = $('input[name="month"]').val();
			var year = $('input[name="year"]').val();


			// Build the new title value base on sections and/or categories
			var custom_title = '';

			if (section == 'music' && category == 'chiptune'){
				custom_title = cf1_Artist_Name +' - '+ cf2_Release_Title +' ['+category+'/8bit]';
			}

			if (section == 'music' && category == 'trance'){
				custom_title = cf2_Release_Title +' - '+ cf1_Artist_Name;
			}

			if (section == 'music' && category == 'techno'){
				custom_title = cf2_Release_Title +' ['+cf3_Record_Label+']';
			}

			if (section == 'news'){
				custom_title = title +' ['+ month +'/'+ year +']';
			}


			// Set the new custom Title and/or URL-only titie
			if (section != 'videos'){
				$('#title').val(custom_title);
				//$('#url-title').val('');
			}
		});

	});
</script>

Second step

Edit the code:
Set the custom fields and others that you are going to be using to build the new title, in the next code you can see that I already set all the 10 custom fields as variables, and name 3 of them as just as I wanted to + added the date fields too:

			// Custom fields
			var cf1_Artist_Name   = $('input[name=custom_1]').val();
			var cf2_Release_Title = $('input[name=custom_2]').val();
			var cf3_Record_Label  = $('input[name=custom_3]').val();
			var cf4 = $('input[name=custom_4]').val();
			var cf5 = $('input[name=custom_5]').val();
			var cf6 = $('input[name=custom_6]').val();
			var cf7 = $('input[name=custom_7]').val();
			var cf8 = $('input[name=custom_8]').val();
			var cf9 = $('input[name=custom_9]').val();
			var cf10 = $('input[name=custom_10]').val();

			// Date fields
			var day = $('input[name="day"]').val();
			var month = $('input[name="month"]').val();
			var year = $('input[name="year"]').val();

Third step

Build the title value from the custom-fields that we had just set in the previous step, you can build it base on section or base on category or both just like in the next code:

if (section == 'music' && category == 'chiptune'){
				custom_title = cf1_Artist_Name +' - '+ cf2_Release_Title +' ['+category+'/8bit]';
			}

			if (section == 'music' && category == 'trance'){
				custom_title = cf2_Release_Title +' - '+ cf1_Artist_Name;
			}

			if (section == 'music' && category == 'techno'){
				custom_title = cf2_Release_Title +' ['+cf3_Record_Label+']';
			}

			if (section == 'news'){
				custom_title = title +' ['+ month +'/'+ year +']';
			}

Fourth step

The last part of the code that:

  1. Exclude the sections which you don’t want to create a custom title for their articles.
  2. Set the new title value we created in it’s place.
  3. Optionally – also reset the URL-only titie to the new title (you will need to un-comment it to work)
// Set the new custom Title and/or URL-only titie
			if ( section != 'videos' || section != 'events' ){
				$('#title').val(custom_title);
				//$('#url-title').val('');
			}

Fifth step (optional)

Hide the title field in the write tab – at the top of bot_write_tab_customize UI, below “Item” in the select-box select the “Title” field, and below “Hide in:” click on “Section List” then select the sections you want to hide the title field in them.

That’s all :)

Special thanks
Special thanks goes to redbot for helping me in this forum thread.

Video tutorial

The YouTube video accompanying this tutorial may be found below. Or, if you prefer Facebook, here is the video hosted there.

Video: Automated custom titles for your articles
Automated custom titles for your articles
Clicking play connects you with youtube.com

9 Comments Comment feed

Thanks Gil for this Tips but,
really not smart to host a video demonstration on a private server on which it is necessary to register to view it. Unless you have an economic interest to do so …
TxpTips could not offer a hosting solution for the demonstration videos or tutorials videos?

@sacripant – we do offer video tutorials here on TXP Tips, although Gil’s latest tutorial would not fall under this category – the video serves only to demonstrate the final result of the tutorial.

I myself do not have a Facebook account – but I prefer that authors choose a video hosting service that works for them. I’d be quite happy to display an accompanying video if an author requests/requires it.

@Sacripant – I was sure that Jonathan is the only creature on earth that doesn’t have a Facebook account lol ;)

Nevermind here’s a link to the video on Youtube that I just uploaded:
Link to the video

I also upload the video of the previous tip:
Link to the video

Use the “Expand” button in the Youtube player (near the “Full screen” button) to see the video in it’s full size (640×480).

@Gil – you are probably right! And I don’t ever plan to have FB, can’t stand it.

Would you like me to place the videos at the end of each article, along with a link to the YouTube/FB versions?

Yea, why not :)
Thanks!

Video has now been added to this tutorial. Thanks to Gil for posting the video.

  • Mave
  • 22 August 2011

Why not just use the title field for this? Why do you need a custom field at all for title?

@Mave – You probably misunderstand the principle of this tip,
(And it’s fine, the name of this tip indeed can be misleading)

It’s not really how to create “automatic”-titles (even that it does to that),
It’s for preventing the need to type the same content in both places in the title and in custom-fields.

When you need to type content in custom-fields, for example I will continue with the music releases which you need to type in custom fields all the next info:
> artist-name
> release-title
> record-label
> release-date
> …

So in this case the title of the article will probably be the artist-name + the release-title,
If so there is no good reason to tell the client (or yourself) to type the same content in both places – in the title and in the custom-fields.
And this is where this tip come handy and help you to prevent the need of typing in both places and only to type it in the custom-fields.

Remember that clients are lazy, and in this case they are totally right,
so we need to set-up the CMS to be as simple and comfortable as possible for them :)

This looks like a great script. I might use it on future website that I will build.

Add a comment

Use Textile help to style your comments