Custom Page Titles
Posted: Mon May 14, 2012 7:55 pm
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. -
First in index.php we add a function to get custom tags from the content (TXT) files -
Now look for the line that says -
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
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) -
Then in editText.php add <wcms_title> to the list of allowed HTML tags -
And finally, in default.php (the site template) use $page_title variable to display the title, e.g. between TITLE tags in the header -
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 -
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 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
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>
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];
}
}
Code: Select all
$content[0] = @file_get_contents("files/$contentfile.txt");
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]);
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]; }
}
Code: Select all
$content = strip_tags($content, "<wcms_title><audio><embed><p>...
Code: Select all
echo "<title>$page_title</title>\n\n";
Code: Select all
<wcms_title>Unique page title comes here</wcms_title>
Enter your regular text content here...
P.S. i know, referencing line numbers would have made things simpler