How to make export script that renames page titles automatically?

Hi there,

since I am creating websites that are very similar, I am looking for a solution that can save me some time.

Here is my situation. Lets say i have these page titles:

Good page – About us
Good page – Contact
Good page – Products

And since I am creating page for another client, I have to manually change the names to:

Awesome page – About us
Awesome page – Contact
Awesome page – Products

I tried to solve it via javascript like this with replace function:

var originalTitle = document.title;

var newTitle = originalTitle.replace(“Good”, “Awesome”);

document.title = newTitle;

document.title;

In the browser, it works. But since Facebook do not support browser javascript, it is showing old name in page title when sharing on social.

I think the way is to write some export code, but I have no idea how to do that. Can somebody help me please?

Actually the javascript code was written by chatGPT. I and a designer, not coder:) I have a zero experience with JS code basically.

Thank you.

If you are not using the bss servers then you could do it with php.

something like:

<?php include 'config.php'; echo $sitename ?> | The Page Title

then add a config.php file along the lines of:

<?php 
$sitename = "Awesome";
?>

You could also add other variables such as domain name, email address etc.

Thank you. Most of the time, I am exporting via SFTP connections. But I need it for both: SFTP and file export on my drive.

I guess that export function is available only for file export on my drive?

It will work for both as long as your production server can handle php

Edit: You will have to tell the server to handle html files as php (usually 1 line in htaccess)

A html parser would work like Python Beautiful Soup. I usually just run the script after I export since the folder opens up I exported to.

from bs4 import BeautifulSoup
html_doc = '<title>My Title</title>'
soup = BeautifulSoup(html_doc, "lxml")
tag = soup.title
print("\nOriginal Markup:")
print(tag)
print("\nOriginal Markup with new text:")
tag.string = "Python Html Parser is my new Title"
print(tag)

Original Markup:
<title>My Title</title>

Original Markup with new text:
<title>Python Html Parser is my new Title</title>

You can test the above example at Write a Python program to change the tag’s contents and replace with the given string

Obviously you would write a single script and include opening each file with python, do the soup work and save the changes for each page. Larger projects get more complex with page opening loops and if statements to target your strings and anything else in the documents but learn your basics first. Once you reach a level of understanding the payoff is enormous.

1 Like