Skip to content

Forum in maintenance, we will back soon 🙂

Creating WordPress ...
 
Notifications
Clear all

Creating WordPress AI with Gemini API

4 Posts
3 Users
0 Reactions
48 Views
(@google-hakeemseriki)
Posts: 2
New Member
Topic starter
 

I have been trying to get the following code working in WordPress Code Snippet and elemetor but its not working. I also copy and paste Hassan's code directly without editing yet the wa no positive result.

 

In summary, i want the AI to generate Visa Requirement using Gemini API. 

 

PHP: function generate_response_with_gemini($prompt) {
$api_key = setApiKey();
$api_url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $api_key;

// Headers for the Gemini API
$headers = [
'Content-Type' => 'application/json'
];

// Body for the Gemini API
$body = [
'contents' => [
[
'parts' => [
['text' => $prompt]
]
]
]
];

// Args for the WordPress HTTP API
$args = [
'method' => 'POST',
'headers' => $headers,
'body' => json_encode($body),
'timeout' => 120
];

// Send the request
$response = wp_remote_request($api_url, $args);

// Check for request errors
if (is_wp_error($response)) {
return 'Error: ' . $response->get_error_message();
}

// Extract the body from the response
$responseBody = wp_remote_retrieve_body($response);

// Log the raw response body for debugging
error_log(print_r($responseBody, true));

// Decode the JSON response body
$decoded = json_decode($responseBody, true);

// Check for API error in response
if (isset($decoded['error'])) {
return 'Error: ' . $decoded['error']['message'];
}

// Extract the text from the response
if (isset($decoded['candidates'][0]['content']['parts'][0]['text'])) {
return $decoded['candidates'][0]['content']['parts'][0]['text'];
} else {
return 'Error: Failed to retrieve response from Gemini API.';
}
}

 

 

 

 

HTML: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visa Requirements Checker</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f4;
color: #333;
line-height: 1.6;
padding: 20px;
}

.container {
max-width: 600px;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
text-align: center;
}

.input-group {
display: flex;
flex-direction: column;
margin-bottom: 20px;
}

select, button {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}

button {
background: #007bff;
color: #fff;
border: none;
cursor: pointer;
}

button:hover {
background: #0056b3;
}

.results {
margin-top: 20px;
background-color: #f9f9f9;
padding: 20px;
border-radius: 8px;
}

.loading {
display: none;
margin-top: 20px;
}

.hidden {
display: none;
}
</style>
</head>
<body>

<div class="container">
<h2>Visa Requirements Checker</h2>

<div class="input-group">
<label for="country">Select Country:</label>
<select id="country">
<option value="">-- Select Country --</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Antigua and Barbuda">Antigua and Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Azerbaijan">Azerbaijan</option>
<option value="Bahamas">Bahamas</option>
<option value="Bahrain">Bahrain</option>
<option value="Bangladesh">Bangladesh</option>
<option value="Barbados">Barbados</option>
<option value="Belarus">Belarus</option>
<option value="Belgium">Belgium</option>
<option value="Belize">Belize</option>
<option value="Benin">Benin</option>
<option value="Bhutan">Bhutan</option>
<option value="Bolivia">Bolivia</option>
<option value="Bosnia and Herzegovina">Bosnia and Herzegovina</option>
<option value="Botswana">Botswana</option>
<option value="Brazil">Brazil</option>
<option value="Brunei">Brunei</option>
<option value="Bulgaria">Bulgaria</option>
<option value="Burkina Faso">Burkina Faso</option>
<option value="Burundi">Burundi</option>
<option value="Cabo Verde">Cabo Verde</option>
<option value="Cambodia">Cambodia</option>
<option value="Cameroon">Cameroon</option>
<option value="Canada">Canada</option>
<option value="Central African Republic">Central African Republic</option>
<option value="Chad">Chad</option>
<option value="Chile">Chile</option>
<option value="China">China</option>
<option value="Colombia">Colombia</option>
<option value="Comoros">Comoros</option>
<option value="Congo, Democratic Republic of the">Congo, Democratic Republic of the</option>
<option value="Congo, Republic of the">Congo, Republic of the</option>
<option value="Costa Rica">Costa Rica</option>
<option value="Croatia">Croatia</option>
<option value="Cuba">Cuba</option>
<option value="Cyprus">Cyprus</option>
<option value="Czech Republic">Czech Republic</option>
<option value="Denmark">Denmark</option>
<option value="Djibouti">Djibouti</option>
<option value="Dominica">Dominica</option>
<option value="Dominican Republic">Dominican Republic</option>
<option value="Ecuador">Ecuador</option>
<option value="Egypt">Egypt</option>
<option value="El Salvador">El Salvador</option>
<option value="Equatorial Guinea">Equatorial Guinea</option>
<option value="Eritrea">Eritrea</option>
<option value="Estonia">Estonia</option>
<option value="Eswatini">Eswatini</option>
<option value="Ethiopia">Ethiopia</option>
<option value="Fiji">Fiji</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="Gabon">Gabon</option>
<option value="Gambia">Gambia</option>
<option value="Georgia">Georgia</option>
<option value="Germany">Germany</option>
<option value="Ghana">Ghana</option>
<option value="Greece">Greece</option>
<option value="Grenada">Grenada</option>
<option value="Guatemala">Guatemala</option>
<option value="Guinea">Guinea</option>
<option value="Guinea-Bissau">Guinea-Bissau</option>
<option value="Guyana">Guyana</option>
<option value="Haiti">Haiti</option>
<option value="Honduras">Honduras</option>
<option value="Hungary">Hungary</option>
<option value="Iceland">Iceland</option>
<option value="India">India</option>
<option value="Indonesia">Indonesia</option>
<option value="Iran">Iran</option>
<option value="Iraq">Iraq</option>
<option value="Ireland">Ireland</option>
<option value="Israel">Israel</option>
<option value="Italy">Italy</option>
<option value="Jamaica">Jamaica</option>
<option value="Japan">Japan</option>
<option value="Jordan">Jordan</option>
<option value="Kazakhstan">Kazakhstan</option>
<option value="Kenya">Kenya</option>
<option value="Kiribati">Kiribati</option>
<option value="Kuwait">Kuwait</option>
<option value="Kyrgyzstan">Kyrgyzstan</option>
<option value="Laos">Laos</option>
<option value="Latvia">Latvia</option>
<option value="Lebanon">Lebanon</option>
<option value="Lesotho">Lesotho</option>
<option value="Liberia">Liberia</option>
<option value="Libya">Libya</option>
<option value="Liechtenstein">Liechtenstein</option>
<option value="Lithuania">Lithuania</option>
<option value="Luxembourg">Luxembourg</option>
<option value="Madagascar">Madagascar</option>
<option value="Malawi">Malawi</option>
<option value="Malaysia">Malaysia</option>
<option value="Maldives">Maldives</option>
<option value="Mali">Mali</option>
<option value="Malta">Malta</option>
<option value="Marshall Islands">Marshall Islands</option>
<option value="Mauritania">Mauritania</option>
<option value="Mauritius">Mauritius</option>
<option value="Mexico">Mexico</option>
<option value="Micronesia">Micronesia</option>
<option value="Moldova">Moldova</option>
<option value="Monaco">Monaco</option>
<option value="Mongolia">Mongolia</option>
<option value="Montenegro">Montenegro</option>
<option value="Morocco">Morocco</option>
<option value="Mozambique">Mozambique</option>
<option value="Myanmar">Myanmar</option>
<option value="Namibia">Namibia</option>
<option value="Nauru">Nauru</option>
<option value="Nepal">Nepal</option>
<option value="Netherlands">Netherlands</option>
<option value="New Zealand">New Zealand</option>
<option value="Nicaragua">Nicaragua</option>
<option value="Niger">Niger</option>
<option value="Nigeria">Nigeria</option>
<option value="North Macedonia">North Macedonia</option>
<option value="Norway">Norway</option>
<option value="Oman">Oman</option>
<option value="Pakistan">Pakistan</option>
<option value="Palau">Palau</option>
<option value="Palestine">Palestine</option>
<option value="Panama">Panama</option>
<option value="Papua New Guinea">Papua New Guinea</option>
<option value="Paraguay">Paraguay</option>
<option value="Peru">Peru</option>
<option value="Philippines">Philippines</option>
<option value="Poland">Poland</option>
<option value="Portugal">Portugal</option>
<option value="Qatar">Qatar</option>
<option value="Romania">Romania</option>
<option value="Russia">Russia</option>
<option value="Rwanda">Rwanda</option>
<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option>
<option value="Saint Lucia">Saint Lucia</option>
<option value="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option>
<option value="Samoa">Samoa</option>
<option value="San Marino">San Marino</option>
<option value="Sao Tome and Principe">Sao Tome and Principe</option>
<option value="Saudi Arabia">Saudi Arabia</option>
<option value="Senegal">Senegal</option>
<option value="Serbia">Serbia</option>
<option value="Seychelles">Seychelles</option>
<option value="Sierra Leone">Sierra Leone</option>
<option value="Singapore">Singapore</option>
<option value="Slovakia">Slovakia</option>
<option value="Slovenia">Slovenia</option>
<option value="Solomon Islands">Solomon Islands</option>
<option value="Somalia">Somalia</option>
<option value="South Africa">South Africa</option>
<option value="South Korea">South Korea</option>
<option value="South Sudan">South Sudan</option>
<option value="Spain">Spain</option>
<option value="Sri Lanka">Sri Lanka</option>
<option value="Sudan">Sudan</option>
<option value="Suriname">Suriname</option>
<option value="Sweden">Sweden</option>
<option value="Switzerland">Switzerland</option>
<option value="Syria">Syria</option>
<option value="Tajikistan">Tajikistan</option>
<option value="Tanzania">Tanzania</option>
<option value="Thailand">Thailand</option>
<option value="Togo">Togo</option>
<option value="Tonga">Tonga</option>
<option value="Trinidad and Tobago">Trinidad and Tobago</option>
<option value="Tunisia">Tunisia</option>
<option value="Turkey">Turkey</option>
<option value="Turkmenistan">Turkmenistan</option>
<option value="Tuvalu">Tuvalu</option>
<option value="Uganda">Uganda</option>
<option value="Ukraine">Ukraine</option>
<option value="United Arab Emirates">United Arab Emirates</option>
<option value="United Kingdom">United Kingdom</option>
<option value="United States">United States</option>
<option value="Uruguay">Uruguay</option>
<option value="Uzbekistan">Uzbekistan</option>
<option value="Vanuatu">Vanuatu</option>
<option value="Vatican City">Vatican City</option>
<option value="Venezuela">Venezuela</option>
<option value="Vietnam">Vietnam</option>
<option value="Yemen">Yemen</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option>
</select>
</div>

<div class="input-group">
<label for="visa-type">Select Visa Type:</label>
<select id="visa-type">
<option value="">-- Select Visa Type --</option>
<option value="tourist">Tourist</option>
<option value="student">Student</option>
<option value="work">Work</option>
<option value="business">Business</option>
</select>
</div>

<button id="check-visa-requirements">Check Visa Requirements</button>

<div class="loading hidden">Loading...</div>

<div class="results hidden" id="results"></div>
</div>

<script>
document.getElementById('check-visa-requirements').addEventListener('click', function() {
const country = document.getElementById('country').value;
const visaType = document.getElementById('visa-type').value;

if (!country || !visaType) {
alert('Please select both country and visa type.');
return;
}

document.querySelector('.loading').classList.remove('hidden');
document.getElementById('results').classList.add('hidden');

fetch('/wp-admin/admin-ajax.php?action=visa_requirements_check&country=' + encodeURIComponent(country) + '&visa_type=' + encodeURIComponent(visaType))
.then(response => response.json())
.then(data => {
document.querySelector('.loading').classList.add('hidden');
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = `<h3>Visa Requirements for ${country} (${visaType})</h3><p>${data.message}</p>`;
resultsDiv.classList.remove('hidden');
})
.catch(error => {
console.error('Error fetching visa requirements:', error);
document.querySelector('.loading').classList.add('hidden');
alert('An error occurred while fetching visa requirements. Please try again later.');
});
});
</script>

</body>
</html>

Screenshot for the code above and Hassan's Code

image
image

Pls I Need Your Assistance!!!

 

 
Posted : 10/11/2024 12:11 am
(@husein)
Posts: 531
Member Moderator
 

Please dont paste the code like this when you want to attach codes to show us use the "Attach FIles" button below.

In addition, when building AI tools copying and pasting the same code as Hasan wont work you need to edit by either adding your API key, replacing your website's link with Hasan's in some places of the codes etc...

Can you please share each code you're using so that I can try it from my end.

 
Posted : 10/12/2024 7:17 pm
(@google-hakeemseriki)
Posts: 2
New Member
Topic starter
 

My apologies, here are the files. Thanks you for your time and effort, I really appreciate.

 
Posted : 10/12/2024 8:17 pm
(@google-deborahpretty)
Posts: 15
Active Member
 

Hi, it's me again, Deborah, I'm still trying to create my first generator. 
First, let me thank those who tried to help. I've been trying for a month.

Just a few days ago, I signed up on SkillShare and followed the system there, but I'm still getting errors.

I test the script using Postman and ThurderClient, sometimes I get the OK green,

but when I test it using the UI, there is no return.

I've been on chat almost 24/7 because I can't believe everything is failing. 
I contacted my hosting, to have them check whatever, forgot, Chat said so much. 

OK, they deactivated all the plugins, etc., and there is still nothing.

Who is willing to help an old lady ... I'm willing to pay, but the code isn't the problem, it's something else maybe capability or something, so the person will have to use my computer login info. because I've had the code work, but not the complete process.

I don't know what else to do.
Please help!!!

 
Posted : 10/12/2024 10:12 pm
Share: