roshambo
roshambo

Reputation: 2794

Change a JS function within WP Plugin

Wondering how I can insert / change a JS function in a plugin in functions.php instead of modifying the plugin directly? This plugin is called max mega menu

I have read up about using add_action and remove_action and custom hooks. I cannot seem to find a good example to work with.

I want to wrap the inner content of the function with this

if (!plugin.isMobileView()) {
   //code here
}

UPDATE

See my answer below - I did not need the plugin script altogether, so I dequeued it. My question should really be now "How to disable a wordpress plugin script"

Upvotes: 1

Views: 204

Answers (3)

roshambo
roshambo

Reputation: 2794

I realised I did not need the whole plugin script and website was fine without it.

So I dequeued the script

function mmm_dequeue_script() {
    wp_dequeue_script( 'megamenu' );
}
add_action( 'wp_print_scripts', 'mmm_dequeue_script', 100 );

Sorry unrelated to the question now. Cheers hope this help someone trying to do the same thing.

Upvotes: 0

Sumon Sarker
Sumon Sarker

Reputation: 2795

You can enqueue your custom script by checking mobile device -

Example : In function.php file

function my_custom_enqueue_script() {
  if (wp_is_mobile()) { #If it is mobile devices
     wp_enqueue_script('my-js','for-mobile-devices.js',false);   #Load your mobile device scripts
  }else{
     wp_enqueue_script('my-js','for-others-devices.js', false ); #Load your other device scripts
  }
}

add_action( 'wp_enqueue_scripts', 'my_custom_enqueue_script' );

Here is the documentations for wp_is_mobile() and wp_enqueue_scripts() function reference

Note : Using above functions, You can separately load your custom script for mobile devices.

Upvotes: 1

ferry
ferry

Reputation: 64

Please read WP Plugin API official guide FIRSTLY(https://codex.wordpress.org/Plugin_API).

The plugin core —— how to add hooks? Two WP internal functions:

add_action ($hookname, $callbackfunction)
add_filter ($hookname,$callbackfunction)

if u dont understand , u can see the WP official plugin —— Hello Dolly!

Upvotes: 0

Related Questions