How to Access JavaScript Code within a Plugin, without editing its code

[ad_1]

Hey, all, I am needing to alter the behavior of a plugin to better fit my site's needs.

So, my thought was to create my own JS file which would contain the code for changing the plugin's default behavior. Ideally it would use functions and variables that are contained within the plugin's JS files. I thought that I could access them via my file, but that doesn't seem to be the case. So I have been troubleshooting and trying to find a way to accomplish my goals. Obviously I do not want to directly edit the plugin's code, as it will be overwritten when it is inevitably updated. I am interested in one of these possibilities:

  1. Insert my JavaScript code into the plugin's JS file by using a function such as wp_add_inline_script. Unfortunately, this doesn't seem to work, as the function seems to only be able to run while the script is in the queue, but not after it's loaded. The plugin's JS files are enqueued via the plugin files and not the functions.php file, so I am unable to run this function properly. Are there other methods of inserting my code?
  2. Find a way to access the JavaScript functions and variables within the plugin's JS files and use them in my own. I know that the plugin's code is wrapped in a jQuery document.ready function, and that might be part of the issue of me accessing what I need. I use that same function in my JS file and found that, that made my functions and variables inaccessible in my other JS files. By removing that document.ready function from my JS file, I was then able to access its functions and variables via another JS and the browser console. Additionally, I can keep the document.ready function and still access all the functions and variables if I add the 'window.' notation to my functions and variables.

So, I am not 100% sure how to proceed, as I really don't want to edit any of the plugin's code directly. I also don't want to make a copy of the plugin and edit its files as if it were my own plugin, as I will then be unable to update it in the future, missing out on potential security updates.

Has anyone encountered something similar before? I would appreciate the help if I can get it. Thank you!

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer