How to hook into parent functions.php using child theme - php

A function in the parent functions.php is requiring an include. I have editted this file its calling, but obviously its still in the parent directory.
require get_template_directory() . '/inc/widgets/team-member.php';
How can i ammend the file url in the child functions.php?

Add this code in parent theme functions.php
add_action( 'wp_loaded', 'parent_prefix_load_classes', 10 );
function parent_prefix_load_classes()
{
define('MEMBER_FILE_URL', get_template_directory(). '/inc/widgets/team-member.php');
}
Use this code in chile theme
// Priority 11 to run after the parent theme's loader.
add_action( 'wp_loaded', 'child_prefix_create_objects', 11 );
function child_prefix_create_objects()
{
echo MEMBER_FILE_URL;
}

Related

Wordpress child theme can't prevent parent theme error

Trying to update a Wordpress instance to PHP 7.2 and I've got a parent theme that uses the deprecated $this in its functions.php.
function paperback_filter_page_templates( $page_templates, $this, $post ) {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) :
unset( $page_templates['inc/edd/home-downloads.php'] );
endif;
return $page_templates;
}
add_filter( 'theme_page_templates', 'paperback_filter_page_templates', 10, 3 );
I've been unsuccessful...
1: Overriding the function in the child theme's functions.php:
(removing $this from the param and with higher priority add_filter)
function paperback_filter_page_templates( $page_templates, $post ) {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) :
unset( $page_templates['inc/edd/home-downloads.php'] );
endif;
return $page_templates;
}
add_filter( 'theme_page_templates', 'paperback_filter_page_templates', 15, 2 );
2: Using a function that removes the filter in the child theme's functions.php:
function rm_paperback_filter_page_templates() {
remove_filter( 'theme_page_templates', 'paperback_filter_page_templates', 10);
}
add_action( 'wp_loaded', 'rm_paperback_filter_page_templates', 15 );
(have tried hooking this action into init, setup_theme, after_setup_theme...)
I can remove the offending function from the parent theme to avoid a catastrophic php 7.2 upgrade for now (parent theme authors/company have been acquired so an update isn't coming) and I know to look for parent themes with pluggable functions from now on... but it'd be nice to know if a real fix is possible!

Remove an action from init in wordpress

I have a theme that defines a new post type but I want to remove that function in the child theme:
PARENT THEME FUNCTION:
add_action('init', 'portfolio_register');
function portfolio_register()
{
//post type definition
}
CHILD THEME FUNCTION:
This is what I tried but it is not working.
function child_remove_parent_function() {
remove_action( 'init', 'portfolio_register' );
}
add_action( 'init', 'child_remove_parent_function', 15 );
Anyone has an idea of what am I doing wrong?
Thanks in advance.
try this code before add action in child theme function.php
<?php
remove_action( 'init', 'portfolio_register' );
?>
or
<?php
add_action( 'after_setup_theme','remove_portfolio', 100 );
function remove_portfolio() {
remove_action( 'init', 'portfolio_register');
}
?>

Enqueing a jQuery script in Wordpress

I am trying to enqueue a jQuery script to my Wordpress site but for some reason it is not adding. Below is my code I've added to my child theme's functions.php file.
<?php
function itm_adding_scripts() {
wp_register_script('my_amazing_script', '/wp-content/themes/italic-child/js/jQuery.equalHeights.js/js/jQuery.equalHeights.js', array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
add_action( '_enqueue_scripts', 'itm_adding_scripts' );
?>
You have a typo in the action missing wp prefix
Change
add_action( '_enqueue_scripts', 'itm_adding_scripts' );
To
add_action( 'wp_enqueue_scripts', 'itm_adding_scripts' );
Reference: wp_enqueue_scripts docs
You have a typo in your action. It should be wp_enqueue_scripts
add_action( 'wp_enqueue_scripts', 'itm_adding_scripts' );

Child theme enqueuing with dependencies

I'm trying to create a child theme. The parent theme has a style.css and all, and I was looking at wp_enqueue_style() function, and it says that you can inlude dependencies. So that means that the themes own style.css can be active, and in my child theme if I specify the same rule in my style.css, it should overwrite it.
But the dependency is an array of handles. How do I find those handles?
wp_enqueue_style( 'mytheme-style', get_stylesheet_directory_uri().'/style.css', array('main_css') );
I tried with the above, but it only loads the style.css from child theme, not the parent.
Where can I find these handles?
EDIT:
I found the code for reproducing the handles and scripts:
function wpa54064_inspect_scripts() {
global $wp_scripts;
foreach( $wp_scripts->queue as $handle ) :
echo $handle,' ';
endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_scripts' );
function wpa54064_inspect_style() {
global $wp_styles;
foreach( $wp_styles->queue as $handle ) :
echo $handle,' ';
endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_style' );
But it still doesn't work the way I thought it does.
get_stylesheet_directory_uri() will return the child themes URL if active.
Since you're trying to load the style.css file inside your parent theme you can use get_template_directory_uri() instead.
E.g:
wp_enqueue_style( 'mytheme-style', get_template_directory_uri() . '/style.css', array('main_css') );
My suggestion would be to load your stylesheets like this (code goes inside your child theme's functions.php):
function wpse_load_styles() {
wp_enqueue_style( 'parent-styles', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'mytheme', get_stylesheet_uri(), array( 'parent-styles' ) );
}
add_action( 'wp_enqueue_scripts', 'wpse_load_styles' );

Wordpress child theme: override functions

In my parent Theme I've a function without the initial statement:
if (!function_exists(... etc...
How can I replace it with a function with the same name in my child theme?
If I create the function into my functions.php it gives me an error due to the fact that there are two functions with the same name.
Thank you in advance for your answers.
This seems to be working for me:
function fileExistsInChildTheme($file_path){
$file_directory = get_template_directory();
if(file_exists($file_directory . "-child" . $file_path)){
return $file_directory .= "-child" . $file_path;
}
return $file_directory .= $file_path;
}
require ( fileExistsInChildTheme('/includes/functions.php') );
require ( fileExistsInChildTheme('/includes/theme-options.php') );
require ( fileExistsInChildTheme('/includes/hooks.php') );
require ( fileExistsInChildTheme('/includes/version.php') );
Child theme function.php file is loaded before the parent theme functions file, therefore you shouldn't get fatal error for re-declaring the function in the child theme. That's why your parent theme is using the function_exists check.
Maybe you're declaring the function in the child theme after a hook(e.g. init)?
Here is the codex documentation about this: http://codex.wordpress.org/Child_Themes#Referencing_.2F_Including_Files_in_Your_Child_Theme
i think if you add same name of function then it take from child theme function then it take from parent.
For ex.
Child Theme
if ( ! function_exists( 'function_name' ) ) {
function function_name(){
echo 'This is child theme';
}
}
Parent Theme
if ( ! function_exists( 'function_name' ) ) {
function function_name(){
echo 'This is parent theme';
}
}

Categories