Custom Page Titles

Post Reply
jinesh
Posts: 3
Joined: Fri May 11, 2012 8:07 pm

Custom Page Titles

Post by jinesh »

Okay, so do you want unique titles for every page & are willing to modify core files knowing that future upgrades will definitely break things? Then read on for a temporary patch -

In a nutshell, this lets you specify page titles by wrapping text in a custom tag while editing that page, e.g. -

Code: Select all

<wcms_title>Your page title</wcms_title>
First in index.php we add a function to get custom tags from the content (TXT) files -

Code: Select all

// regEx courtesy http://bit.ly/tTTdP
function getSpecialTags($string, $tags, $option) {
    $pattern = "/<$tags ?.*>(.*)<\/$tags>/";
    preg_match($pattern, $string, $matches);
    if ($option == "withtags") {
    	return $matches[0];
    }
    if ($option == "withouttags") {
    	return $matches[1];
    }
}
Now look for the line that says -

Code: Select all

$content[0] = @file_get_contents("files/$contentfile.txt");
And under it copy paste this snippet which gets the page title from the content file, sets it as a variable available to the template then removes it from the content so visitors don't see it :?

Code: Select all

	$page_title = getSpecialTags($content[0], "wcms_title", "withouttags");
	$page_title_withtags = getSpecialTags($content[0], "wcms_title", "withtags");
	$content[0] = str_replace($page_title_withtags, "", $content[0]);
	$content[0] = trim($content[0]);
But now we need to ensure custom tags are visible when a logged-in user edits the page so we modify the existing displayMainContent() function (still in index.php) -

Code: Select all

   function displayMainContent() {
	   global $cookie, $content, $page, $page_title_withtags;
	   if($_COOKIE[$cookie])
	   {
	   	   $content[0] = $page_title_withtags."\n\n\n".$content[0];
		   echo "<div class='title'><div id='change'><span id='$page' class='editText'>$content[0]</span></div></div>";
	   }
	   else { echo $content[0]; }
   }
Then in editText.php add <wcms_title> to the list of allowed HTML tags -

Code: Select all

$content = strip_tags($content, "<wcms_title><audio><embed><p>...
And finally, in default.php (the site template) use $page_title variable to display the title, e.g. between TITLE tags in the header -

Code: Select all

echo "<title>$page_title</title>\n\n";
Now to test! Login to your WonderCMS site, double click to edit a page & use the <wcms_title> tag to give your page a custom title -

Code: Select all

<wcms_title>Unique page title comes here</wcms_title>

Enter your regular text content here...
WonderCMS is incredibly easy to customize which is why I couldn't resist putting this together. I'm not a PHP programmer (more of a UI guy) so feel free to suggest optimizations to the convoluted scripting above 8-) I started out trying to make generic custom tags but ended by hardcoding Page_Title instead...this should be fixed soon.

P.S. i know, referencing line numbers would have made things simpler :D
wiz
Admin
Posts: 481
Joined: Sat Oct 30, 2010 12:23 am

Re: Custom Page Titles

Post by wiz »

Excellent!

This is a feature we were planning on developing in the near future. Excited you did it and I'll be glad to test it out. Will confirm it and make this thread "sticky".

Great job!
Tim
Posts: 3
Joined: Tue Sep 18, 2012 3:30 am

Re: Custom Page Titles

Post by Tim »

I can see how that could be useful. On the other hand, I would just like for the page name to be included in the title. Fortunately, there is a very easy solution. Simply add your desired separator along with $page inside the <title> tag on line 8 of default.php

For instance, if your Title is "WonderCMS Demo" and the current page is "Example":

Result: <title>WonderCMS Demo: Example</title>

Code: Select all

 echo "<title>$title: $page</title>\n\n".
Result: <title>WonderCMS Demo - Example</title>

Code: Select all

 echo "<title>$title - $page</title>\n\n".
Result: <title>WonderCMS Demo » Example</title>

Code: Select all

 echo "<title>$title &raquo; $page</title>\n\n".
Result: <title>WonderCMS Demo • Example</title>

Code: Select all

 echo "<title>$title &bull; $page</title>\n\n".
wiz
Admin
Posts: 481
Joined: Sat Oct 30, 2010 12:23 am

Re: Custom Page Titles

Post by wiz »

Tim wrote:I can see how that could be useful. On the other hand, I would just like for the page name to be included in the title. Fortunately, there is a very easy solution. Simply add your desired separator along with $page inside the <title> tag on line 8 of default.php

For instance, if your Title is "WonderCMS Demo" and the current page is "Example":

Result: <title>WonderCMS Demo: Example</title>

Code: Select all

 echo "<title>$title: $page</title>\n\n".
Result: <title>WonderCMS Demo - Example</title>

Code: Select all

 echo "<title>$title - $page</title>\n\n".
Result: <title>WonderCMS Demo » Example</title>

Code: Select all

 echo "<title>$title &raquo; $page</title>\n\n".
Result: <title>WonderCMS Demo • Example</title>

Code: Select all

 echo "<title>$title &bull; $page</title>\n\n".
Thanks for your awesome input Tim!
Marking this thread as "sticky".
Post Reply