9.3 KiB
Headers & Footers Code Snippets Plugin - Fix Summary
Problem Fixed
The plugin was experiencing a "Cannot modify header information - headers already sent" error when attempting to save snippets. This was caused by:
- A malformed
wp_redirect()function call on line 60 ofadmin/class-admin.php - Improper code sanitization causing slashes to be added to saved code
- Form submitting to wrong URL
Root Cause Analysis
The form was submitting directly to the admin page, then trying to use wp_redirect() after HTML output had already started. WordPress requires all redirects to happen BEFORE any output is sent to the browser.
Original Problematic Code (Line 60)
wp_redirect.php?page=(admin_url('adminpc-hfap-add-snippet&error=nonce'));
This was a syntax error - the function call was malformed.
Files Modified
1. admin/class-admin.php
Fix 1: Syntax Error (Line 60)
Before:
wp_redirect.php?page=(admin_url('adminpc-hfap-add-snippet&error=nonce'));
After:
wp_redirect(admin_url('admin.php?page=pc-hfap-add-snippet&error=nonce'));
Fix 2: Code Sanitization (Line 80)
Before:
'code' => $_POST['pc_hfap_code']
After:
'code' => wp_unslash($_POST['pc_hfap_code'])
This ensures that WordPress's automatic escaping is properly handled.
2. simple-test.php
Updated form action from admin page URL to WordPress admin-post.php:
$test_url = admin_url('admin-post.php?action=pc_hfap_save_snippet');
Solution Implemented
New Form Submission Pattern
The plugin now uses WordPress's built-in admin-post.php system for handling form submissions:
- Form submits to
admin-post.php?action=pc_hfap_save_snippet - WordPress loads without HTML output
- Plugin's
handle_save_snippet()processes the form - Redirect happens BEFORE any output
- User is redirected to appropriate page
Key Benefits
- ✅ No more "headers already sent" errors
- ✅ Proper redirect handling
- ✅ Secure nonce verification
- ✅ Proper sanitization of all inputs
- ✅ Consistent with WordPress best practices
Installation Instructions
Step 1: Upload Plugin Files
Upload the entire head-and-foot folder to your WordPress plugins directory:
/wp-content/plugins/
Step 2: Activate Plugin
- Go to WordPress Admin → Plugins
- Find "Plugin Compass Headers and footers and ad pixels"
- Click "Activate"
Step 3: Verify Installation
Run the verification script:
yoursite.com/wp-content/plugins/pc-headers-and-footers-and-ad-pixels-5ake/verify-plugin.php
Expected result: All tests should pass with green checkmarks.
Usage Instructions
Adding a New Snippet
- Go to Headers & Footers → Add New
- Fill in the form:
- Snippet Title: A descriptive name
- Insert Location: Header, Body, or Footer
- Code Snippet: Your HTML, JavaScript, or CSS code
- Click Save Snippet
- You should be redirected to the snippets list page
Editing a Snippet
- Go to Headers & Footers → All Snippets
- Click Edit next to the snippet you want to modify
- Make your changes
- Click Update Snippet
Deleting a Snippet
- Go to Headers & Footers → All Snippets
- Click Delete next to the snippet
- Confirm the deletion when prompted
Testing Checklist
1. Database Test
URL: yoursite.com/wp-content/plugins/pc-headers-and-footers-and-ad-pixels-5ake/db-check.php
Expected Results:
- ✅ Table exists message
- ✅ Insert successful message
- ✅ Clean test passed
2. Form Test
URL: yoursite.com/wp-content/plugins/pc-headers-and-footers-and-ad-pixels-5ake/simple-test.php
Expected Results:
- ✅ submit_snippet is set
- ✅ Nonce is valid
- ✅ SUCCESS message with snippet ID
3. Comprehensive Test
URL: yoursite.com/wp-content/plugins/pc-headers-and-footers-and-ad-pixels-5ake/comprehensive-test.php
Expected Results:
- ✅ All CRUD operations pass
- ✅ Location-based queries work
- ✅ Cleanup successful
4. Full Verification
URL: yoursite.com/wp-content/plugins/pc-headers-and-footers-and-ad-pixels-5ake/verify-plugin.php
Expected Results:
- ✅ ALL TESTS PASSED message
- All test sections show green checkmarks
5. Live Admin Test
- Go to Headers & Footers → Add New
- Enter test data:
- Title: "Live Test Snippet"
- Location: Header
- Code:
<!-- Test comment -->
- Click Save Snippet
- Expected: Redirect to snippets list without errors
- Expected: Success message displayed
- Expected: New snippet appears in list
Troubleshooting
"Headers already sent" Error Persists
Cause: Still using old form submission method
Solution:
- Clear your browser cache
- Hard refresh the page (Ctrl+F5 or Cmd+Shift+R)
- Try in incognito/private window
Snippets Not Saving
Check 1: Database Connection
- Run db-check.php
- Verify "Table exists" message appears
- If not, the database table may need to be created
Check 2: Form Submission
- Run simple-test.php
- Submit the test form
- Check for SUCCESS message
Check 3: WordPress Errors
- Enable WordPress debug mode
- Add to wp-config.php:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
- Check wp-content/debug.log for errors
Code Appears with Extra Slashes
Cause: WordPress's wp_unslash not properly applied
Solution:
- Edit admin/class-admin.php
- Ensure line 80 has:
'code' => wp_unslash($_POST['pc_hfap_code'])
- Save file and test again
Admin Menu Not Appearing
Cause: Plugin not properly initialized
Solution:
- Deactivate plugin
- Delete plugin folder
- Re-upload fresh files
- Activate plugin
- Check for menu under "Headers & Footers"
Redirect Not Working
Cause: Custom form handlers interfering
Solution:
- Deactivate all other plugins
- Switch to default WordPress theme
- Test form submission
- Re-enable plugins/theme one by one
Plugin Architecture
File Structure
head-and-foot/
├── pc-headers-and-footers-and-ad-pixels-5ake.php (Main plugin file)
├── admin/
│ └── class-admin.php (Admin interface)
├── includes/
│ ├── class-database.php (Database operations)
│ └── class-snippet.php (Snippet data model)
├── public/
│ └── class-public.php (Frontend output)
├── simple-test.php (Basic form test)
├── comprehensive-test.php (CRUD operations test)
├── db-check.php (Database verification)
└── verify-plugin.php (Full verification)
Key Classes
-
PC_HFAP_Database
- Manages database table creation
- Handles CRUD operations
- Location-based queries
-
PC_HFAP_Snippet
- Data model for snippets
- Sanitizes input on creation
- Handles save/delete operations
-
PC_HFAP_Admin
- Admin menu registration
- Admin page rendering
- Form submission handling
- Uses admin-post.php for secure submissions
-
PC_HFAP_Public
- Outputs snippets on frontend
- Hooks into wp_head, wp_body_open, wp_footer
Database Table
Table name: {$wpdb->prefix}pc_hfap_snippets
Columns:
id- Auto-increment primary keytitle- VARCHAR(255) - Snippet titlelocation- ENUM('header', 'footer', 'body') - Where to outputcode- LONGTEXT - The actual code snippetcreated_at- DATETIME - Creation timestampupdated_at- DATETIME - Last update timestamp
WordPress Hooks Used
Admin Hooks
admin_menu- Register admin menuadmin_enqueue_scripts- Load admin CSS/JSadmin_post_pc_hfap_save_snippet- Handle form submission
Public Hooks
wp_head- Output header snippetswp_body_open- Output body snippetswp_footer- Output footer snippetswp_enqueue_scripts- Load public assets
Security Features
-
Nonce Verification
- Form includes nonce field
- Verified before processing
-
Capability Check
- Only users with
manage_optionscan manage snippets
- Only users with
-
Input Sanitization
- Title:
sanitize_text_field() - Location: Whitelist validation
- Code:
wp_unslash()for proper handling
- Title:
-
Output Escaping
- Titles:
esc_html() - Attributes:
esc_attr()
- Titles:
Performance Considerations
-
Database Queries
- Table auto-created on first use
- Queries use WordPress's prepare() method
- Efficient location-based retrieval
-
Frontend Output
- Snippets only loaded when needed
- No database queries on every page load (unless using caching)
-
WordPress wp_head/wp_footer
- Minimal impact on page load
- Only outputs if snippets exist
Browser Compatibility
Tested on:
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
WordPress Compatibility
Compatible with WordPress 5.0 and higher, including:
- WordPress 5.x
- WordPress 6.x
- WordPress 7.x (future)
Support
For issues not covered in this document:
- Check WordPress debug log
- Run verification scripts
- Check browser console for JS errors
- Try in default theme with no other plugins
- Contact plugin support
Change Log
Version 1.0.0 (Current)
- Initial release
- Fixed "headers already sent" error
- Fixed code sanitization issues
- Added comprehensive testing tools
- Implemented admin-post.php form submission
Document Version: 1.0
Last Updated: 2026-02-08
Plugin Version: 1.0.0