How to Edit a Variable/Function in a Plugin Using WordPress Hooks via functions.php ⚒
Published: 2021-07-23 07:09:02


It is sometimes possible to modify plugin functions without editing the plugin file directly. It is poor practice to directly edit plugin files, because the edits are overwritten on plugin updates. 

If the plugin was written well using WordPress hooks, then we are in luck and can modify the functions. 

##First Example: A Variable in a Plugin

Let's take a look at an example. Below we have a variable `$message`, found in the Gravity View plugin. `$message` is defined using the WP hook `apply_filters()`: 

```php
$message = apply_filters( 'gravityview/edit_entry/success', $entry_updated_message , $this->view_id, $this->entry, $back_link );
```

`$message` is a variable that is defined using a function (the details of which doesn't matter), and the variable `$entry_updated_message`. `$entry_updated_message` is the variable that we want to change the value of.

### Arguments of `apply_filters()`

The first argument of `apply_filters()` is the name of this instance of this hook. We will use this name ('gravityview/edit_entry/success') to hook into this hook from functions.php. See the WordPress codex entry of `apply_filters()` to read more about this hook.
User Fuxia describes the `apply_filters()` hook well in this StackExchange post. She gives the following description of the arguments passed to `apply_filters()`: 

```php
apply_filters(
  $filter_name,     // used for add_filter( $filter_name, 'callback' );
  $value_to_change, // the only variable whose value you can change
  $context_1,       // context
  $context_2        // more context
);
```

As stated above, `apply_filters()` only lets us change the value of the second argument passed to it in it's declaration. In this case that's $entry_updated_message. 

###Hooking Into `apply_filters()` With  `add_filter()` in functions.php

Since `apply_filters()` was used here with `$entry_updated_message` as the second argument, we can add the hook `add_filter()` to our functions.php file to modify the variable `$entry_updated_message`. 

See the format below: 

```php
// in functions.php

function updateEntryUpdatedMessage(){
  $newMessage = 'Entry Updated.';
  return $newMessage;
}
add_filter('gravityview/edit_entry/success', 'updateEntryUpdatedMessage');
```

What we are doing here is creating a function, `updateEntryUpdatedMessage()`, which will return the new value we desire for `$entry_updated_message`. Using the name from `apply_filters()`, we can hook into the original function and change the variable using the above code in functions.php. 

##Second Example: WooCommerce Shop Page

Another example is using this code from the WooCommerce shop page template, found near the top of the code: 

```php
do_action( 'woocommerce_before_main_content' );
```

`do_action()` is another WordPress hook, and we can hook into it via functions.php as well. Examine the following code: 

```php
function add_sidebar_widget_left(){
  echo ' <div id="gaws_secondary" class="widget-area gaws-sidebar-right" role="complementary">';
  echo do_shortcode('[ca-sidebar id="1725"]'); 
  echo '</div>';
}
add_action('woocommerce_before_main_content', 'add_sidebar_widget_left', 1 );
```

In this case, we create the callback function `add_sidebar_widget_left()` to append a `<div>` element to the top of the shop page. We are using `add_action()` to echo this code. The code will not replace everything existing originally in the function that `add_action()` was used to declare. Rather, the code will be run before or after the original code, depending on the priority we passed to `add_action()`. The priority is the third argument, an integer. This argument determines in what order relative to the original function the callback function that we have created will be executed. 

If the priority of the original function is lower, then the original will execute first, and vice versa. I gave the priority of "1" here because I wanted the code to execute before the original function. I don't know what the original's priority is, but it was running after at the default value of "10", and changing to "1" fixed that (if no third argument is passed, default priority is "10"). 

That's It!
I hope this helps you fix your plugin issues! If your plugin doesn't include these hooks, then as far as I know your only choice is to modify the plugin files directly. Good luck & cheers! 🧿

//Omar 🧱

Sources:
Construction Site Photo by Scott Blake on Unsplash
WordPress filters StackExchange post - https://wordpress.stackexchange.com/questions/97356/trouble-understanding-apply-filters