请求修改Zajax WordPress插件以支持根相对URL
Got it, let's get your Zajax plugin working with root-relative URLs so you don't have to manually swap to absolute URLs every time. Since the plugin is no longer updated, we'll modify its core JavaScript to recognize and handle root-relative paths correctly. Here's how to do it step by step:
Step 1: Locate the plugin's main JavaScript file
Navigate to your WordPress plugins directory (wp-content/plugins/zajax/) and look for ajsfolder. Inside, you'll likely find a file likezajax.js,main.js, orfrontend.js—this is where the AJAX URL handling logic lives. Make a backup of this file before editing, just in case.Step 2: Find the URL validation logic
Open the JS file and search for code that checks if a URL should be loaded via AJAX. You'll probably see something like this (or a variation using regex):// Example original code checking for absolute URLs if (url.startsWith('http') || url.startsWith('//')) { // Handle AJAX load } else { // Fallback to full page reload }The issue here is it only triggers AJAX for absolute URLs. We need to add root-relative URLs (those starting with
/) to this condition.Step 3: Update the URL check to include root-relative paths
Modify the condition to include URLs that start with/:// Updated condition supporting root-relative URLs if (url.startsWith('http') || url.startsWith('//') || url.startsWith('/')) { // Handle AJAX load } else { // Fallback to full page reload }Step 4: Convert root-relative URLs to absolute for AJAX requests (if needed)
Some AJAX implementations require absolute URLs to work correctly. If the plugin's AJAX call fails with root-relative paths, add a quick conversion step right before the AJAX request:// Convert root-relative URL to absolute if needed if (url.startsWith('/')) { // Use your site's base URL (window.location.origin gets http(s)://yourdomain.com) const siteBase = window.location.origin; url = siteBase + url; }Alternatively, if WordPress has already defined a global site URL variable (like
window.site_url), use that instead ofwindow.location.originfor better accuracy.Step 5: Test thoroughly
Save your changes, then visit your site. Click on links using root-relative URLs (like/aboutor/contact)—they should load via AJAX now, and your streaming audio should keep playing without interruption. If something breaks, revert to your backup file and double-check the code edits.
Since the plugin is no longer maintained, your changes won't be overwritten by updates. Just keep a copy of the modified JS file somewhere safe in case you ever need to re-install the plugin.
内容的提问来源于stack exchange,提问作者gulliver

