
We’ve all been there. You copy a snippet of code, tweak it, upload it to your WordPress site, and… nothing. Or worse, the site breaks.
I recently tried to build a simple “Scroll to Top” button for my website. It seemed straightforward enough: a bit of PHP, some CSS for styling, and a dash of jQuery for the smooth scroll. But when I tried to run it, the styling was broken, and the logic fell flat. Instead of spending hours hunting down syntax errors and missing file paths, I turned to AI.
Here is exactly how I used Gemini AI to turn a broken script into a lightweight, fully functional WordPress plugin in less than a minute.
- **Syntax Errors & Invalid Functions:** Fixing issues like the `darken()` Sass function being used in plain CSS/PHP.
- **Missing Logic:** Identifying and implementing missing steps, like applying stored user settings (e.g., button position).
- **File Path & Dependency Issues:** Streamlining multi-file projects (CSS/JS) into single, reliable files for web applications.
- **Runtime Logic Flaws:** Correcting conditional statements, loops, and function calls across languages (PHP, JavaScript, Python, etc.).
- **Generating Code Snippets:** Creating full, runnable solutions in Python, PHP, JavaScript, React, Angular, and more, complete with best practices.
- **Code Optimization:** Analyzing existing code for inefficiencies and suggesting faster, cleaner, and more robust alternatives.
- **Converting Formats:** Translating code between languages or upgrading legacy syntax to modern standards.
- **Writing Technical Documentation:** Generating clear comments, function headers, and step-by-step guides for complex code.
- **SEO Optimization:** Crafting titles, meta descriptions, focus keyphrases, and image alt text for blog content and technical articles.
Table of Contents
- The Problem: When Code Looks Right but Fails
- The Broken Code (The "Before")
- The Solution: A Step-by-Step AI Debugging Process
- Step 1: Diagnosing the Error
- Step 2: Consolidating the Structure
- Step 3: Connecting the Logic
- The Result: A Lightweight, Working Plugin
- The Working Code (The "After")
- Why Developers Should Pair Program with AI
- Conclusion
The Problem: When Code Looks Right but Fails
I started with a concept for a “Responsive Scroll to Top” plugin. The code looked okay at first glance, but it had three major fatal flaws that were preventing it from working:
- The “Sass” Mistake: I was trying to use a Sass function (
darken()) inside a standard CSS string. Browsers don’t understand Sass natively, so the styles broke. - The File Trap: The code referenced external
.cssand.jsfolders that didn’t exist, meaning the plugin loaded… nothing. - Missing Logic: I had a settings page to choose “Left” or “Right” alignment, but I forgot to write the logic to actually apply that choice to the CSS.
I pasted the broken code into my AI assistant and asked for help.
The Broken Code (The “Before”)
This is the exact code that failed to run properly on the website, causing styling issues and lack of responsiveness:
<?php
/*
Plugin Name: Responsive Scroll to Top Button
Plugin URI: [https://blogofanup.com](https://blogofanup.com)
Description: A lightweight, responsive Scroll to Top button for WordPress that is compatible with all themes.
Version: 1.1
Author: Anup Ain
Author URI: [https://blogofanup.com](https://blogofanup.com)
License: GPL2
*/
// Exit if accessed directly
if (!defined('ABSPATH')) exit;
// Enqueue scripts and styles
function scroll_to_top_enqueue_scripts() {
wp_enqueue_style('scroll-to-top-css', plugin_dir_url(__FILE__) . 'css/scroll-to-top.css');
wp_enqueue_script('scroll-to-top-js', plugin_dir_url(__FILE__) . 'js/scroll-to-top.js', array('jquery'), null, true);
wp_enqueue_style('font-awesome', '[https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css](https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css)');
}
add_action('wp_enqueue_scripts', 'scroll_to_top_enqueue_scripts');
// Add the Scroll to Top button
function scroll_to_top_button() {
echo '<button id="scroll-to-top" title="Back to Top"><i class="fas fa-chevron-up"></i></button>';
}
add_action('wp_footer', 'scroll_to_top_button');
// Add plugin settings menu
function scroll_to_top_settings_menu() {
add_options_page('Scroll to Top Settings', 'Scroll to Top', 'manage_options', 'scroll-to-top-settings', 'scroll_to_top_settings_page');
}
add_action('admin_menu', 'scroll_to_top_settings_menu');
// Register settings
function scroll_to_top_register_settings() {
register_setting('scroll_to_top_options_group', 'scroll_to_top_position');
register_setting('scroll_to_top_options_group', 'scroll_to_top_color');
}
add_action('admin_init', 'scroll_to_top_register_settings');
// Function to apply custom styles to the Scroll to Top button dynamically
function scroll_to_top_custom_styles() {
// Get the saved button color from the settings, default to '#ff7e5f' if not set
$button_color = esc_attr(get_option('scroll_to_top_color', '#ff7e5f'));
// Output inline CSS to apply the selected color to the button
echo "<style>
#scroll-to-top {
background: $button_color !important;
}
#scroll-to-top:hover {
background: darken($button_color, 10%) !important;
}
</style>";
}
// Hook the function into wp_head to insert the styles in the <head> section
add_action('wp_head', 'scroll_to_top_custom_styles');
// Create settings page
function scroll_to_top_settings_page() {
?>
<div class="wrap">
<h1>Scroll to Top Button Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('scroll_to_top_options_group'); ?>
<?php do_settings_sections('scroll_to_top_options_group'); ?>
<table class="form-table">
<tr>
<th><label for="scroll_to_top_position">Button Position</label></th>
<td>
<select name="scroll_to_top_position" id="scroll_to_top_position">
<option value="right" <?php selected(get_option('scroll_to_top_position'), 'right'); ?>>Right</option>
<option value="left" <?php selected(get_option('scroll_to_top_position'), 'left'); ?>>Left</option>
</select>
</td>
</tr>
<tr>
<th><label for="scroll_to_top_color">Button Color</label></th>
<td>
<input type="color" name="scroll_to_top_color" id="scroll_to_top_color" value="<?php echo esc_attr(get_option('scroll_to_top_color', '#ff7e5f')); ?>">
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
The Solution: A Step-by-Step AI Debugging Process
Here is the exact workflow I used to fix the plugin.
Step 1: Diagnosing the Error
I didn’t just ask “fix this.” I looked at the output the AI gave me. The AI immediately flagged that darken($button_color, 10%) is valid in a pre-processor like SCSS, but invalid in vanilla CSS/PHP.
The Fix: The AI suggested replacing the complex Sass function with a simple CSS filter: filter: brightness(85%);. This achieves the same hover effect without needing a compiler.
Step 2: Consolidating the Structure
My original code tried to load scroll-to-top.css and scroll-to-top.js. For a massive plugin, that’s fine. But for a simple button? It’s overkill and prone to path errors.
The Fix: The AI refactored the code to place the CSS and JS directly into the PHP file using the wp_footer hook. This turned a multi-file headache into a single-file plugin that I can just drop into the wp-content/plugins folder.
Step 3: Connecting the Logic
I had a database option for positioning (left vs right), but the button was stuck on the right.
The Fix: The AI added a conditional check:
$position_css = ($position === 'left') ? 'left: 20px;' : 'right: 20px;';
This small line of code dynamically injects the correct CSS based on my settings page.
The Result: A Lightweight, Working Plugin
In one interaction, I went from a broken script to this fully functional, single-file plugin:
- Zero external dependencies (except Font Awesome for the icon).
- 100% Valid CSS using modern filters.
- Dynamic Settings that actually work.
The Working Code (The “After”)
This is the full, clean, and functioning plugin code provided by the AI:
Here is the cleaned-up logic the AI provided:
<?php
/*
Plugin Name: Responsive Scroll to Top Button (Fixed)
Plugin URI: [https://blogofanup.com](https://blogofanup.com)
Description: A lightweight, responsive Scroll to Top button for WordPress.
Version: 1.2
Author: Anup Ain
Author URI: [https://blogofanup.com](https://blogofanup.com)
License: GPL2
*/
// Exit if accessed directly
if (!defined('ABSPATH')) exit;
// 1. Enqueue Font Awesome (Only external asset needed)
function scroll_to_top_assets() {
wp_enqueue_style('font-awesome', '[https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css](https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css)');
wp_enqueue_script('jquery'); // Ensure jQuery is loaded
}
add_action('wp_enqueue_scripts', 'scroll_to_top_assets');
// 2. Add the Button HTML to Footer
function scroll_to_top_button_html() {
echo '<button id="scroll-to-top" title="Back to Top" style="display:none;"><i class="fas fa-chevron-up"></i></button>';
}
add_action('wp_footer', 'scroll_to_top_button_html');
// 3. Add CSS and JS (Inline to keep it in one file)
function scroll_to_top_inline_code() {
// Get Settings
$position = get_option('scroll_to_top_position', 'right');
$color = esc_attr(get_option('scroll_to_top_color', '#ff7e5f'));
// Determine CSS for position (Fixed Logic!)
$position_css = ($position === 'left') ? 'left: 20px;' : 'right: 20px;';
?>
<style>
#scroll-to-top {
position: fixed;
bottom: 20px;
<?php echo $position_css; ?> /* Dynamic Positioning Fix */
z-index: 9999;
background: <?php echo $color; ?>;
color: #ffffff;
border: none;
outline: none;
cursor: pointer;
padding: 15px;
border-radius: 4px;
font-size: 18px;
transition: 0.3s;
box-shadow: 0px 2px 5px rgba(0,0,0,0.3);
}
/* Fixed: Used CSS filter brightness instead of Sass darken() */
#scroll-to-top:hover {
filter: brightness(85%);
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Show button when scrolling down 100px
$(window).scroll(function() {
if ($(this).scrollTop() > 100) {
$('#scroll-to-top').fadeIn();
} else {
$('#scroll-to-top').fadeOut();
}
});
// Scroll to top on click
$('#scroll-to-top').click(function() {
$('html, body').animate({scrollTop : 0}, 800);
return false;
});
});
</script>
<?php
}
add_action('wp_footer', 'scroll_to_top_inline_code', 100);
// 4. Admin Menu Settings
function scroll_to_top_settings_menu() {
add_options_page('Scroll to Top Settings', 'Scroll to Top', 'manage_options', 'scroll-to-top-settings', 'scroll_to_top_settings_page');
}
add_action('admin_menu', 'scroll_to_top_settings_menu');
// 5. Register Settings
function scroll_to_top_register_settings() {
register_setting('scroll_to_top_options_group', 'scroll_to_top_position');
register_setting('scroll_to_top_options_group', 'scroll_to_top_color');
}
add_action('admin_init', 'scroll_to_top_register_settings');
// 6. Settings Page HTML (Unchanged from original as it was correct)
function scroll_to_top_settings_page() {
?>
<div class="wrap">
<h1>Scroll to Top Button Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('scroll_to_top_options_group'); ?>
<?php do_settings_sections('scroll_to_top_options_group'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="scroll_to_top_position">Button Position</label></th>
<td>
<select name="scroll_to_top_position" id="scroll_to_top_position">
<option value="right" <?php selected(get_option('scroll_to_top_position'), 'right'); ?>>Right</option>
<option value="left" <?php selected(get_option('scroll_to_top_position'), 'left'); ?>>Left</option>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="scroll_to_top_color">Button Color</label></th>
<td>
<input type="color" name="scroll_to_top_color" id="scroll_to_top_color" value="<?php echo esc_attr(get_option('scroll_to_top_color', '#ff7e5f')); ?>">
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}Why Developers Should Pair Program with AI
This experience reinforced why AI is such a powerful tool for WordPress developers. It didn’t just “write code” for me; it acted as a senior developer looking over my shoulder.
- It catches syntax nuances: Like the Sass vs. CSS issue.
- It suggests architecture improvements: Merging files for simplicity.
- It saves time: What would have been 30 minutes of debugging took 30 seconds.
Conclusion
You don’t need to be a coding wizard to build custom features for your WordPress site. You just need a good idea and the willingness to ask the right questions. By using AI to debug and refactor, I built a cleaner, faster tool than I could have on my own.
Have you used AI to fix a WordPress bug? Let me know in the comments!
Custom Header Footer WordPress Plugin
Anup Ain
Hey, my name is Anup Ain. I am a blogger and a digital marketing intern. I enjoy sharing my knowledge and experiences with others.