Hacking SEO: Getting to the Top with AI-Generated Content

Matthew Keeley
August 6, 2023

Introduction

In the realm of digital marketing, SEO stands tall as the gatekeeper of online visibility. But what if there’s a way to ascend its ranks effortlessly? Enter AI-generated content. With the sheer prowess of artificial intelligence, businesses can generate captivating content that not only resonates with audiences but also appeases search engine algorithms. And the result? More clicks, leading to an increase in potential revenue. In fact, websites on the first page of Google receive almost 95% of web traffic, leaving only 5% for remaining search results pages. Imagine harnessing even a fraction of that for your website!

Prompt Generation Using GPT-4

First things first, we need to get a list of blog post titles that we can use as prompts. To do this we can use the ChatGPT-4 website and ask to “Generate 25 blog post titles on the subject of red teaming”. This is what it came back with:

"Red Teaming 101: The Ultimate Guide to Ethical Hacking"
"The Art and Science of Red Team Operations"
"Why Every Organization Needs Red Teaming: Uncovering Vulnerabilities"
"Top 10 Techniques Used by Red Team Experts"
"The Differences Between Red Teaming, Penetration Testing, and Vulnerability Assessment"
...truncated...

While these prompts are pretty good, we  can also instruct ChatGPT to craft titles that are SEO-optimized, increasing the likelihood of higher search engine rankings. Once generated, these titles can be stored in a rudimentary .txt file aptly named prompts.txt.

Crafting Comprehensive Blog Posts

At face value, the process for generating content seems pretty easy. However, there are some limitations with ChatGPT where the outputs of a prompt like “Generate a blog post on pentesting AWS” will only return a blog post with 500 words  that is lacking details. The ideal blog post word-count often ranges between 1000 to 3000 words, which will require a different approach in prompt generation.

To achieve the desired content length of roughly 1500 words, each prompt needs to be broken down into sections targeting approximately 500 words. Here’s a glimpse into the code that implements this strategy:

import openai

def generate_content(prompt):
    openai.api_key = os.environ.get('CHAT_GPT_API_KEY')
    completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", 
    messages=[{"role": "user", "content": f"{prompt}"}])

    response = completion.choices[0].message.content
    print(f'Word count: {len(response.split())}')
    return response

def make_blog_post(topic):
    sections = generate_content(
        f"create a bulleted list of section titles for a blog post on {topic}.
         Respond with nothing but the section titles and no greetings")
    content = ""
    for line in sections.split('\n'):
        section = line[2:]
        content = generate_content(
            f"I'm writing a blog post about {topic}. 
              I'd like content for a section titled {section}. 
              Kindly exclude the heading in your response.")
    return content

The code above has ChatGPT create a bulleted list of the sections headings given a blog post title such as “Python pickling exploitation”. Those are then parsed and stored as sections. Next another generate_content request is sent asking to write a blog post with the title we provided, but to specifically generate the content just for the section. Keeping the title in the prompt helps ChatGPT stay on track with the content that is being generated. Taking this approach not only achieves the word count desired, but also maintains a cohesive and engaging narrative throughout.

Using Wordpress API

The magic doesn’t just stop at content creation. WordPress offers an API that seamlessly integrates with our script, allowing automated publishing of these AI-generated posts. The caveat with Wordpress is that the blog posts being uploaded need to be in html tags, include a title, and each section to have a distinct header. To do this, the make_blog_post function was updated to the following:

def make_blog_post(topic):

    sections = generate_content(
        f"create a bulleted list of section titles for a blog post on {topic}.
         Respond with nothing but the section titles and no greetings")
    content = ""
    for line in sections.split('\n'):
        section = line[2:]
        content += f'<h3>{section}</h3>\n'
        paragraph = generate_content(
            f"Im writing a blog post about {topic}. 
            I want you to write the content for a section titled {section}. 
            Do not include a heading in your response.")
        content += f"<p>{paragraph}</p>\n"
    return content

Setting up this padding allows Wordpress to automatically format the blog post. After the padding was setup, all that is left to do is hook into the Wordpress API to publish the blog posts on a specific cadence. The code to do that is below:

def new_blog_post(title, content, formatted_publish_date):
    data = {
        'title': title,
        'content': content,
        'status': 'publish',
        'date': formatted_publish_date
    }
    username = os.environ.get('WORDPRESS_USERNAME')
    password = os.environ.get('WORDPRESS_PASSWORD')
    response = requests.post(
        site_url + 'posts', json=data, auth=(username, password))
    if response.status_code == 201:
        print(
            f'Blog post created successfully! Word count: {len(content.split())}')
    else:
        print('Failed to create the blog post.')

For the grand finale, the main function scans through all the manually generated prompts, feeds them to make_blog_post (which crafts the sections and generates the content), and then publishes the content on a Wordpress blog!

if __name__ == "__main__":
    with open('prompts.txt', 'r') as f:
        lines = f.readlines()

    publish_date = datetime.fromisoformat('2023-05-03T12:00:00')
    for topic in lines:
        topic = topic.strip()
        formatted_publish_date = publish_date.strftime('%Y-%m-%dT%H:%M:%S')
        content = make_blog_post(topic)
        new_blog_post(topic, content, formatted_publish_date)
        publish_date += timedelta(days=1)

And here’s a golden nugget for SEO enthusiasts: publishing content twice a day and promptly requesting Google to index them can significantly boost your rankings. However, remember to maintain an updated sitemap for Google to effectively navigate your posts.

Conclusion

In a world where content is king, AI proves to be the master craftsman. By blending the capabilities of GPT-3.5-Turbo with the versatility of WordPress, businesses stand on the precipice of an SEO revolution. Not only does this approach promise enhanced visibility, but it also paves the way for increased clicks, driving potential revenue. As the line between technology and content blurs, one thing is clear: AI is the future of SEO, and the future is now.

Subscribe to our blog

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Matthew Keeley
January 21, 2024

Check out our other posts