If you’re a web developer and you’re trying to modify the output of any plugin or theme without editing the plugin or theme itself, you can only if the plugin or theme developer allows you to.

In order to know if you can manipulate the output, you have to find out where a particular section is located or defined. Once you find the codes you’re looking for, determine the section where you want to insert codes and see if there is a do_action() in that area. If there is one, then you can definitely insert your codes on that section without having to edit that section.

do_action() should have two parameters, the tag and the arguments.

e.g. do_action(‘woocommerce_after_cart_table’)

The above action does not define any arguments. This action allows you to add content after the cart table in the woocommerce cart page.

Now that we know we can insert codes to the section and that the tag is ‘woommerce_after_cart_table’,  we need to call add_action() function so that we can connect to the do_action.

add_action(‘{tag}’, ‘{function_name}’);

The first parameter is the tag which is the first parameter in do_action (see above).

The second parameter is the function name for your function.

Now, let’s define a function that will simple display a test:

function insertThis() {

echo "This should be inserted after the cart page";

}

Now, let’s execute that function by adding it to the action ‘woocommerce_after_cart_table’.

add_action('woocommerce_after_cart_table', 'insertThis');

Save your work and go to the cart page and see if the test is added after at the bottom of the cart page.

This is only a simple demonstration of what the do_action and add_action can do. If the do_action provides arguments, you can make use of this so that your custom function can communicate with the action. You can visit here and here for more details about these functions.

Leave a Reply