Tagged: Code block, path
-
AuthorPosts
-
August 14, 2024 at 10:02 pm #1464632
Hi team,
I’m adding a code block to a page containing a fetch to a .json file. Where should I place that file? I’ve tried both the root directory and also a new folder in the uploads directory.The code is in private. Thanks for your help.
August 15, 2024 at 4:32 am #1464649Hey Julie,
Thank you for the inquiry.
Are you trying to add JavaScript code to a Code Block element? What should happen after fetching the content of the JSON file? You might need to place the JSON file in the wp-content directory or your child theme folder. Please provide the complete code so that we can review it. However, please note that this level of customization is beyond the scope of our support. You may need to hire a freelance developer or reach out to our partner, Codeable.
If you have any other questions or need further assistance, please don’t hesitate to let us know.
Best regards,
IsmaelAugust 15, 2024 at 2:08 pm #1464687Hi Ismael,
The code works well outside of Enfold. (see private) It works inside of Enfold as well but it can’t find the JSON file where I placed it. That’s why I’m asking where to put it. See private for all. I gave you login credentials as well above.
Thanks- This reply was modified 3 months ago by welswebmaster.
August 16, 2024 at 4:55 am #1464741Hi,
Thank you for the info.
We adjusted the script slightly and removed a few unnecessary elements, including some styles. You can place the zipcodes.json file in the wp-content > uploads folder and use the absolute URL (https://dev.site.com/wp-content/uploads/zipcodes.json) in the fetch function.
<script> var gform;gform||(document.addEventListener("gform_main_scripts_loaded",function(){gform.scriptsLoaded=!0}),window.addEventListener("DOMContentLoaded",function(){gform.domLoaded=!0}),gform={domLoaded:!1,scriptsLoaded:!1,initializeOnLoaded:function(o){gform.domLoaded&&gform.scriptsLoaded?o():!gform.domLoaded&&gform.scriptsLoaded?window.addEventListener("DOMContentLoaded",o):document.addEventListener("gform_main_scripts_loaded",o)},hooks:{action:{},filter:{}},addAction:function(o,n,r,t){gform.addHook("action",o,n,r,t)},addFilter:function(o,n,r,t){gform.addHook("filter",o,n,r,t)},doAction:function(o){gform.doHook("action",o,arguments)},applyFilters:function(o){return gform.doHook("filter",o,arguments)},removeAction:function(o,n){gform.removeHook("action",o,n)},removeFilter:function(o,n,r){gform.removeHook("filter",o,n,r)},addHook:function(o,n,r,t,i){null==gform.hooks[o][n]&&(gform.hooks[o][n]=[]);var e=gform.hooks[o][n];null==i&&(i=n+"_"+e.length),gform.hooks[o][n].push({tag:i,callable:r,priority:t=null==t?10:t})},doHook:function(n,o,r){var t;if(r=Array.prototype.slice.call(r,1),null!=gform.hooks[n][o]&&((o=gform.hooks[n][o]).sort(function(o,n){return o.priority-n.priority}),o.forEach(function(o){"function"!=typeof(t=o.callable)&&(t=window[t]),"action"==n?t.apply(null,r):r[0]=t.apply(null,r)})),"filter"==n)return r[0]},removeHook:function(o,n,t,i){var r;null!=gform.hooks[o][n]&&(r=(r=gform.hooks[o][n]).filter(function(o,n,r){return!!(null!=i&&i!=o.tag||null!=t&&t!=o.priority)}),gform.hooks[o][n]=r)}}); </script> <div class="container"> <h3>Zipcode Redirect</h3> <form id="zipcodeForm"> <input type="text" id="zipcodeInput" placeholder="Enter zipcode" required> <button type="submit">Go</button> </form> <p id="errorMessage" class="error"></p> </div> <script> document.addEventListener('DOMContentLoaded', () => { const form = document.getElementById('zipcodeForm'); const input = document.getElementById('zipcodeInput'); const errorMessage = document.getElementById('errorMessage'); fetch('https://welstestdev.wpenginepowered.com/wp-content/uploads/zipcodes.json') .then(response => response.json()) .then(zipcodeData => { form.addEventListener('submit', (e) => { e.preventDefault(); const zipcode = input.value.trim(); if (zipcodeData[zipcode]) { window.location.href = zipcodeData[zipcode]; } else { errorMessage.textContent = 'Zipcode not found. Please try again.'; } }); }) .catch(error => { console.error('Error fetching zipcode data:', error); errorMessage.textContent = 'An error occurred. Please try again later.'; }); }); </script>
Best regards,
IsmaelAugust 16, 2024 at 4:57 am #1464742Hi,
UPDATE: Please make sure to move the Code Block element inside a Color Section element. Let us know of the result.
Best regards,
IsmaelAugust 16, 2024 at 11:47 am #1464766This reply has been marked as private.August 17, 2024 at 7:31 pm #1464834Hi,
As I understand your test site outer box shadow works good:
but your live site is not:
I first note that in your code block you have HEAD & BODY tags please don’t add these, I also note that your page is not published, the WordPress preview will not always display the page correctly, I also see that your HTML in the code block adds css for “.container” & “body” that targets the page and not just the code block element.
So I remove the HEAD & BODY tags, change “.container” to “.zip-container” and publish, and now it is mostly correct:
the corrected code:<script> var gform;gform||(document.addEventListener("gform_main_scripts_loaded",function(){gform.scriptsLoaded=!0}),window.addEventListener("DOMContentLoaded",function(){gform.domLoaded=!0}),gform={domLoaded:!1,scriptsLoaded:!1,initializeOnLoaded:function(o){gform.domLoaded&&gform.scriptsLoaded?o():!gform.domLoaded&&gform.scriptsLoaded?window.addEventListener("DOMContentLoaded",o):document.addEventListener("gform_main_scripts_loaded",o)},hooks:{action:{},filter:{}},addAction:function(o,n,r,t){gform.addHook("action",o,n,r,t)},addFilter:function(o,n,r,t){gform.addHook("filter",o,n,r,t)},doAction:function(o){gform.doHook("action",o,arguments)},applyFilters:function(o){return gform.doHook("filter",o,arguments)},removeAction:function(o,n){gform.removeHook("action",o,n)},removeFilter:function(o,n,r){gform.removeHook("filter",o,n,r)},addHook:function(o,n,r,t,i){null==gform.hooks[o][n]&&(gform.hooks[o][n]=[]);var e=gform.hooks[o][n];null==i&&(i=n+"_"+e.length),gform.hooks[o][n].push({tag:i,callable:r,priority:t=null==t?10:t})},doHook:function(n,o,r){var t;if(r=Array.prototype.slice.call(r,1),null!=gform.hooks[n][o]&&((o=gform.hooks[n][o]).sort(function(o,n){return o.priority-n.priority}),o.forEach(function(o){"function"!=typeof(t=o.callable)&&(t=window[t]),"action"==n?t.apply(null,r):r[0]=t.apply(null,r)})),"filter"==n)return r[0]},removeHook:function(o,n,t,i){var r;null!=gform.hooks[o][n]&&(r=(r=gform.hooks[o][n]).filter(function(o,n,r){return!!(null!=i&&i!=o.tag||null!=t&&t!=o.priority)}),gform.hooks[o][n]=r)}}); document.addEventListener('DOMContentLoaded', () => { const form = document.getElementById('zipcodeForm'); const input = document.getElementById('zipcodeInput'); const errorMessage = document.getElementById('errorMessage'); fetch('https://welsbpo.net/wp-content/uploads/zipcodes.json') .then(response => response.json()) .then(zipcodeData => { form.addEventListener('submit', (e) => { e.preventDefault(); const zipcode = input.value.trim(); if (zipcodeData[zipcode]) { window.location.href = zipcodeData[zipcode]; } else { errorMessage.textContent = 'Zipcode not found. Please try again.'; } }); }) .catch(error => { console.error('Error fetching zipcode data:', error); errorMessage.textContent = 'An error occurred. Please try again later.'; }); }); </script> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; margin: 0; background-color: #f0f0f0; } .zip-container { background-color: white; padding: 2rem !important; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); text-align: center; } h3 { margin-bottom: 1rem; } form { display: flex; justify-content: center; margin-bottom: 1rem; } input { padding: 0.5rem; font-size: 1rem; border: 1px solid #ccc; border-radius: 4px 0 0 4px; } button { padding: 0.5rem 1rem; font-size: 1rem; background-color: #007bff; color: white; border: none; border-radius: 0 4px 4px 0; cursor: pointer; } button:hover { background-color: #0056b3; } .error { color: red; } </style> <div class="zip-container"> <h3>Zipcode Redirect</h3> <form id="zipcodeForm"> <input type="text" id="zipcodeInput" placeholder="Enter zipcode" required> <button type="submit">Go</button> </form> <p id="errorMessage" class="error"></p> </div>
Best regards,
MikeAugust 19, 2024 at 2:47 pm #1464907Thank you Mike and Ismael for your help with this. It works well and looks good now too! You guys are the best.
Have a great week!August 19, 2024 at 4:50 pm #1464912Hi,
Glad we were able to help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.Best regards,
Mike -
AuthorPosts
- The topic ‘Fetch to JSON field path’ is closed to new replies.