How to Edit a Variable/Function in a Plugin Using WordPress Hooks via functions.php ⚒

Construction site.

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. I was reading that in order to make use of the plugin’s hook, it was necessary to create a new plugin running alongside the existing plugin, and add the corresponding hook there. However this at least is not always necessary, and as we will see here, adding the hook in our theme’s functions.php can be sufficient.

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 function apply_filters():

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

apply_filters() is a WordPress function that takes an arbitrary name as its first argument. This first argument will be the name of our hook related to this function.

$message is a variable that is defined using some more undisplayed code, as well as the variable $entry_updated_message. $entry_updated_message is the variable that we want to change the value of.

Arguments of apply_filters()

apply_filters() is used to create our hook. The first argument of apply_filters() is the name of our hook. We will reference the hook name (‘gravityview/edit_entry/success’) from functions.php to be able to modify $entry_updated_message. 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():

  $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:

// 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.

Since this hook is a filter hook and not an action hook, the result of the callback function of the filter sends data back to $entry_updated_message, and modifies this variable accordingly. In the next example we will see an action hook. An action hook is declared, and when it is referenced, it does not send data back to the location of the action hook’s definition, but rather the action hook reference just uses the hook function to output data in the action hook reference’s local location.

Second Example: WooCommerce Shop Page

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

do_action( 'woocommerce_before_main_content' );

do_action() is another WordPress hook (which you can read about in the codex), and we can hook into it via functions.php as well. Examine the following code:

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”).

Explanation of add_action()

As stated above, the add_action() function does not return data to a variable in the original declaration of the hook, like the filter hook does. Instead, add_action() adds a function to be executed to a hook. To explain, let’s say that we have the functions:

function say_hello(){
  echo 'Hello. ';
function say_name(){
  echo 'My name is Omar. ';

Now let’s say we want to print “Hello. My name is Omar”. We can call these functions in succession by adding them to a hook that we define using the add_action() function. The hook name is the first argument passed to add action. So we’ll call it ‘omars_introduction’:

add_action('omars_introduction', 'say_hello', 1);

Here we are saying that we want to call the function say_hello() when the hook omars_introduction is invoked. We are also giving the say_hello() callback a priority of “1”, which means that it will run first. We then want to invoke say_name() with a priority of “2” to run afterwards:

add_action('omars_introduction', 'say_name', 2);

Now that we have our two callbacks set up to execute on our hook invocation, all we have to do is invoke the hook with the WordPress function do_action():

//expected output: "Hello. My name is Omar." 

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 ?


Construction Site Photo by Scott Blake on Unsplash

WordPress filters StackExchange post

WordPress Hooks (“filters” and “actions”) –

WordPress Action Hook –

Subscribe to new blog posts