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

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.

Related

Execute a shortcode inside a folder in a Child Theme

I'm confused on how to run my custom shortcode in my child theme. To my understanding, I should be only including/requiring the file in the functions.php in my child theme right?
My File Structure is like this
**Child Theme**
-functions.php
-deals
--deals.php
In my functions.php i just place the code
require_once('deals/deals.php' );
And in my deals.php, I have this shortcode
add_shortcode('show-deals','show_deals');
function show_deals() {
return 'Show Deals';
}
So it doesn't get rendered. Help

How to add_action to Woocommerce hook?

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

Wordpress child theme breaks site appearance

I am using Splash theme from StylemixThemes. I am trying to create a child theme in order to edit some things but when I am activating it I do not get the same result as the parent theme.
Inside the child theme folder I have the style.css and the functions.php. Here is the functions.php code
<?php
add_action('wp_enqueue_scripts', 'enqueue_parent_theme_styles'); function enqueue_parent_theme_styles() {
wp_enqueue_style('parent-style', get_template_directory_uri().'/style.css'); }
I am attaching a screenshot for both parent and child theme to show the difference.
parent
child
I am wondering if there is a solution or if there is something I am missing.
Thanks in advance!
You can create your custom child theme learning from wpbeginner.com

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>");
}
}

Parent Theme Style Loading After Bootstrap Style

I have created a child theme to my parent theme and am running into an issue. I am using the child theme functions.php file to call the parent theme .css. The issue with this is that the parent style.css is being called before the bootstrap.css only on my child theme. I have tried using the $dependencies argument but it still isn't working. Was looking for an easy correct solution.
wp_enqueue_style('bootstrap-style', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css');
wp_enqueue_style('alphamedia-style', get_stylesheet_uri(), 'bootstrap-style');
That is the code I am using in my parent functions.php file but it still isn't working.
I'm sure it is something simple I am overlooking. Thank you for the help.
Your on the right track with this but the wp_enqueue_style(); function expects an array to be passed as the $dependencies. Try this:
wp_enqueue_style('alphamedia-style', get_stylesheet_uri(), array('bootstrap-style'));

Categories