How To Create Google Text To Speech PHP Code For WordPress [ChatGPT]

If you want to build a dedicated PHP script file to run a Google text to speech app on a WordPress website. Here is how to do it.

If you are not a PHP developer, it's totally fine. In this article, we will discuss how to use OpenAI's ChatGPT to write the PHP code to create a text to speech user interface and load it onto a WordPress website page step by step. 

No high level coding skills required. Just follow the steps.

Step 1. Use ChatGPT To Create A Code

chatgpt interface

To use ChatGPT for free, Go here

Step 2. Enter Prompt Commands

Enter: "Write a php code that convert text to speech. Include user input text box. set input box to maximum 800 words"

ChatGPT will generate a list of codes to use. However, there are a few missing steps non PHP coders need to know to compile these codes together properly.

Please follow Steps 3 - 7 carefully.

Note: Different command phrasing lead to different results. Nail the perfect phrase for the AI to create the code use may take multiple attempts to get it right. 

Once you have the code you want, please save the prompt in a notepad verbatim for future use.

Step 3. Enable Google Cloud TTS API

Sign into your Google Cloud account and enable the text to speech API

enable google cloud text to speech api

After enabling the Cloud Text to Speech API, go ahead and create an API by going to

  1. Credentials
  2. Create crenditals
  3. Copy your API key and keep it secure
create google api

Next we need to install the client library. If you're new this can be very confusing. So here is what you need to do.

Step 4 - Install PHP To Windows System

Download the stable PHP here

Extract the downloaded file in a directory you can remember.

Go to the Windows search bar and find "Edit the system environment variables"

edit variable in window

Go to "environment variables"

edit environment variable

Select "Path" and hit edit

Select "New" and paste the directory of where the PHP folder is. In this example, it was C:\Users\Owner\Desktop\php-8.2.0

install php directory

Step 5 - Run Command Prompt 

php version verified in cmd

Open up Windows Command Prompt (CMD) and type in "php --version". This should display the version of the PHP and verify successful PHP installation.

Step 6 - Composer & Git Setup

download composer

Download the composer here , This is a preferred dependency PHP manager.

Note: if you do not have the PHP installed on Windows, you cannot run the composer

If you have multiple PHP version installed, please click on the drop down menu and select the one you want to work with.

No proxy server required

Follow the prompt and finish the installation. Now restart the command prompt and type "composer" to check proper installation

check composer proper installation on cmd

After successful installation of composer, install Git the same way, and add the Git fold into the "environment variables"

If git is not installed, you will need to install it. Git is a useful tool for version control and collaboration, and it is often required when working with projects that are stored in a git repository or when using a dependency manager.

Download Git here

Step 7 - Install Google Client Library 

After PHP and PHP composer has been properly installed on the computer, now we can proceed to install Google Client library.

install client library google

This part requires the user to enter the windows command prompt again to download the client library

composer require google/apiclient:^2.13.0
install client library google cmd

Be sure to enter the latest version

Nexts

run "composer require google/cloud-text-to-speech" inside the command prompt and hit "enter"

install google tts cmd

Step 8 - Upload The Code To WordPress 

Login to the hosting account and go to file manager. In this case we are using WPX hosting.

Go to file manager > locate the website domain > public_html 

wordpress php install

Create a text file under public_html, and name it to whatever you want with a php extension.  In this example case, it's googletts.php

Copy the code from ChatGPT into the text editor, and enter projectID and setAPIkey 

paste the code

Step 9 - Run The Code 

Open a new browser window and load the php file.

For example: domain.com/[name].php