• farmerisaac

    (@farmerisaac)


    Hey! I am loving the plugin and so far its been able to do most of what I need it to do!

    So, I am trying to alter the behavior of it slightly, without directly changing any of the plugin’s files (to protect it from future updates). I haven’t figured out how to access any of its functions or variables outside of the respective JS files (though it appears to be just ONE js.min file in the browser). Here’s an overview of my objective and the problems that I still need to solve to get it implemented properly.

    I have created different categories for the plugin’s custom post type (CPT), such as “Tow Truck” and “Repair Shop”. The way that my client’s business is setup, is that their Tow Truck service locations operate within a select group of bordering counties, within the same U.S. state, and each location will manage that entire group of counties. For example, County Group 01 includes three counties (let’s call them Johnston, McDonald, and Douglas) and County Group 02 serves three other bordering counties The Tow Truck service location that serves ALL the counties in County Group 01 is physically located in Johnston county. That one location provides tow truck services to ALL the counties listed in County Group 01, so when the user is searching for locations with this service, my custom script will check which county their searched address is in, and if it is in County Group 01, then it will display the location in the sidebar like normal, but it will NOT display other Tow Truck locations that are not part of that county group.

    I have more or less figured out the logic of coding that functionality, though I need to do a deeper dive into the plugin to make sure its all working. So, when a user inputs their address and selects the “Tow Truck” category from the filter dropdown, and then clicks the “Search” button, I need to be able to run one of my JavaScript files, which checks the following:

    • Check if the selected category is equal to “Tow Truck”.
    • If NO, then run the plugin’s default search behavior. Only the “Tow Truck” category gets special search functionality. All other categories can use the default search behavior of the plugin.
    • If YES, then run my custom JavaScript instead, which can potentially piggyback off of some of the plugin’s built-in variables and functions.
    • My JavaScript file will execute the filtering by county group and update the map as needed. I am planning on using various functions and variables from the plugin itself to accomplish this, but I cannot find a way to access any of them without directly editing the plugin files themselves (which I really don’t want to do).
    • To emphasize, I am confident in being able to logically solve the problem of filtering by county group, I simply am looking for a safe way to access the functions and variables in the plugin’s JavaScript file(s) and was unsure if there was a good way of doing that, which I am missing.

    I have gone through the documentation and looked through all the provided filters (which are great by the way! Thanks for those), but I don’t believe any of them can help with this issue (unless I missed something). Here’s some of the things I have tried:

    • I have tried using the wp_add_inline_script() function to add my own JavaScript to the plugin’s JS file, but that doesn’t work. I believe this is because the function needs to be run within the same function that enqueues the script (which is inside the plugin files, so I can’t directly alter it safely).
    • I have tried accessing the functions and variables found in the JavaScript file (both the min version and the regular versions) in the JavaScript console, but they’re “undefined”.
    • Additionally, I have also tried adding the plugin’s JS file as a dependency for my own JS files to load, meaning the plugin’s files would always load first. In my mind, this means I should be able to access their functions and variables, since they should already be declared by the time my JS files have loaded. But that doesn’t seem to be the case. I have tried accessing them in my own files after doing this (as well as in the console), but it doesn’t work. I did confirm that the plugin’s JS file DOES load first doing this.
    • To check my logic, I created a second JS file. Let’s say I have myFile01.js and myFile02.js. If myFile01.js has a function called myCustomFunction(), myFile02.js cannot access it unless I do ONE of the following:
      • 1) I remove the jQuery(document).ready(function($) that wraps around all my code in myFile01.js. Removing this function allows myFile02.js to access all the variables and functions in myFile01.js. I noticed that in the plugin files, its main functions and variables are wrapped in that same jQuery(document).ready(function($).
      • If I declare my function as window.myCustomFunction(), then I do NOT need to remove the jQuery(document).ready(function($) from my first file. My second file, myFile02.js, can now access all the variables and functions within the first JS file.

    In summary, I simply need to be able to either dynamically insert JS into the plugin’s JS file OR be able to override it and piggyback off of some of its variables and functions in my own file. I just don’t know how to get the two connected.

    So, I am feeling a bit lost now. I am sure I could get it to work if I had access to the necessary functions and variables, but I am just not sure how to safely do that. If anyone has tried this or something similar, I would really appreciate the help!

    Thank you!

Viewing 1 replies (of 1 total)
  • Plugin Author Tijmen Smit

    (@tijmensmit)

    Hi, sorry for the late response. I was travelling around a bit and forgot about your post.

    Maybe I misunderstand what you’re trying to do, but why not use https://wpstorelocator.co/document/wpsl_sql/ to check which category ID is passed, and based on that modify the SQL query that selects the locations and restrict the returned location data?

    When the search has returned the results you can also use
    https://api.jquery.com/ajaxComplete/ to run custom code from a custom JS file where you can do whatever you want.

    You can for example with https://wpstorelocator.co/document/wpsl_store_meta/ include the category ID in the returned data, set that in the template, and then in the ajaxComplete code check if the ID of the returned data matches with the ID of the selected category. Based on that you can hide / show items?

    With the 2.x code you will have to edit the core code to change the JS functions ( which is not recommended ), with the upcoming 3.x code you will have filters in the JS code. But this won’t help you at the moment. But maybe what I wrote above with the filters and ajaxComplete also fixes your issue.

    • This reply was modified 1 month, 3 weeks ago by Tijmen Smit.
    • This reply was modified 1 month, 3 weeks ago by Tijmen Smit.
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.