How to add_action to Woocommerce hook? - php

I simply want to add an action to an existing Woocommerce hook (woocommerce_after_cart in this case). After adding a function with add_action() and loading the page back up, my function doesn't run. Not until I manually do_action() does my function finally run. I just want to know why when I add the action originally, it's not being acknowledged.
I've already checked out Wordpress codex for hooks and seems like I'm doing everything correct. I'm adding the action in a custom theme, not functions.php but I've tried that too.
Full thing looks like this:
function abc() {
print_r("hello world");
}
add_action('woocommerce_after_cart','abc');
Hello world does not print on page load, only after I call do_action(), why?

In order for any add_action() to work a do_action() must be called.
The woocommerce_after_cart action is called in the template "/cart/cart.php"
https://github.com/woocommerce/woocommerce/search?utf8=%E2%9C%93&q=woocommerce_after_cart
In your theme or child theme do you have the file "/woocommerce/cart/cart.php"?
It could be that the WooCommerce cart template is being overridden and that the template does not have the <?php do_action( 'woocommerce_after_cart' ); ?> call.
I have tested your abc function and it works in a vanilla out of the box install of Woo and WP.
You need to add the <?php do_action( 'woocommerce_after_cart' ); ?> action to the cart.php template file.
Hope this helps :)

Related

Parent (functions.php) executes but actions doesn't seem to work

I'm reading this WordPress Theme flow chart.
I've setup a basic parent/child themes in my wordpress.
Child Theme (functions.php)
<?php
echo "child theme";
Parent Theme (functions.php)
<?php
echo "top";
function coffee_enqueue_styles() {
echo "parent theme";
}
add_action('wp_enqueue_scripts', 'coffee_enqueue_styles');
echo "bottom";
So after I activated my child theme and it loads and executes it's functions.php file and print child theme. Then WordPress checks if this theme has a parent, it executes it's functions.php file and in my case it does and prints top and bottom but for some reason it doesn't executes the add_action hook where I'm printing enqueued from parent.
This is the output I get.
Am I missing something? Please guide me. Thanks you.
UPDATE
I changed my action to init and it prints parent theme at the end of bottom. So my action hook is working fine.
Please, Can you please tell me why wp_enqueue_scripts hook is not executing or there is any order behind it?
After checking the core files. I found that in wp-includes/default-filters.php. WordPress is binding wp_enqueue_scripts with wp_head.
Which means wp_enqueue_scripts hook is only going to work when the wp_head hook executes.
So, I was missing wp_head function from the template.

Can't find where the do_action("This function") leads to in wordpress theme

I have this in my index.php file. It adds the home banner image in WordPress. I know that it is mostly generated in WordPress customizer, but I need to add an anchor tag in this section. I can't find it anywhere in the file structure.
<?php do_action('cleanblog_index_top'); ?>
I'm not able to find where cleanblog_index_top leads to. Any help would be great. Thank you!
I stumbled on this old one while looking up the docs for do_action(). The answers are brutal so I decided to provide a better answer in case anyone else stumbles here.
If a WordPress theme has something like do_action( 'example_action_hook_tag' ) somewhere in one of the template files (such as index.php, page.php or whatever) the purpose is to provide theme or plugin authors with a way to write their own custom function that they can then "hook" onto the action with the function add_action().
WordPress would then call this function any time and anywhere do_action( 'example_action_hook_tag' ) is called.
The creators of commercial themes will often litter their template files with hooks declared with do_action() to make it easier for their customers to customize their themes via functions.php or by writing a site-specific plugin.
It looks to me that this is the likely scenario that is impacting the OP. This also explains why the OP was unsuccessful in finding where this "leads to".
It would only "lead somewhere" if the OP wrote a function in the theme/child-theme functions.php or in a plugin and added the line do_action( 'cleanblog_index_top', 'name_of_ops_function' ) to hook their function onto the cleanblog_index_top. WordPress would then call their function when do_action( 'cleanblog_index_top' ) was called in index.php.
From the name of the OP's hook, cleanblog_index_top, it sounds like the theme author intended to provide a way for others to inject output at the top of the index page template.
Suppose the OP wanted <h1>Hello World</h1> to appear there.
In functions.php of a theme/child-theme the OP could add a function that echo's this out:
function op_customization() {
echo '<h1>Hello World</h1>';
}
And hook their function onto cleanblog_index_top:
add_action( 'cleanblog_index_top', 'op_customization' );
Cheers!
You should never edit the index.php file directly, for the same reason you should never edit core Wordpress files directly - the next time WP pushes an update, your changes will be overwritten (and that assumes you don't break anything). Never edit plugin files directly, same reason.
You need to look in your theme, you should only make changes to the functions.php and style.css files in your theme - unless you create a child theme and that is a topic you should Google.

Add code to StudioPress Genesis Child Theme loop

I am using the theme Magazine Pro for the Genesis Framework. I have a plugin that in order for it to be displayed I must add the following in the loop.
<?php wptopc($format="select", $prepend="<div class='toc'>", $append=""); ?>
<?php wptopc_pagination_links($prepend="", $append="</div>"); ?>
There is no single.php file or anything like that. I also don't want to change the loop entirely, just add the above code to it. How can I do this for only single_posts?
So I figured it out. There is a plugin from StudioPress called Genesis Simple Hooks. It allows you to execute shortcode, html and php into various hook elements in the framework.
For my purpose I just added the php code to genesis_before_entry_content
Another solution is to add a custom function to your child themes functions file with one of the genesis hooks that executes within the loop.
Example:
add_action( 'genesis_before_entry', 'hook_after_header' );
function hook_after_header() {
if ( is_single() ) {
wptopc($format="select", $prepend="<div class='toc'>", $append="");
wptopc_pagination_links($prepend="", $append="</div>");
}
}

How to remove a php hook from a Wordpress theme

I'm trying to remove some hooks so that product categories don't show up on the homepage (of a Wordpress Storefront child theme).
I have the following code in my functions.php, which isn't working:
/**
* REMOVE SECTIONS ON HOMEPAGE
*/
add_action( 'init', 'remove_storefront_on_sale_products', 10 );
function remove_storefront_on_sale_products () {
?>
remove_action( 'homepage', 'storefront_on_sale_products', 60 );
<?php
}
Your code aint working because your line remove_action() is located outside of the php-tags.
Remove ?> and <?php in your above code and you should be fine.
First of all wordpress has not any action like "homepage" so you need to verify which action you are calling so if you want to remove function output only from homepage then you can do it by conditionally and secondly you didn't write proper php code, To achieve this you can write following in method.
if(!is_home()){
//Do stuff here if it is not homepage
}
OR
if(get_the_ID()!=101){ //101 your page id in which you don't want to show this
//Do stuff here if it is not homepage
}
I hope this may help you.

program custom wordpress plugin to output only to a specific page

I need to know how to take the output of a custom built Wordpress plugin and output it onto a specific page.
I know that I need to use the add_action() / add_filter() functions to call the function which outputs the plugins output when a wordpress hook function runs.
Currently I am using the 'the_content' hook.
This outputs my plugins output to all pages in my theme which call the the_content() function.
Which hook can I use to make the output only appear on a specific page.
Also
It would be useful to know how to create a page using my plugin.
Checkout is_page()
if (is_page(123)) {
// put your hooks here
}
put your code in a file inside plugin directory
then
use this
function page_directory()
{
if(is_page('123')){
$dir = plugin_dir_path( __FILE__ );
include($dir."custom/page.php");
die();
}
}
add_action( 'wp', 'page_directory' );

Categories