Ask your WordPress questions! Pay money and get answers fast! (more info)

My Child theme not overriding a non-template file WordPress

  • SOLVED

I'm using Listify and I'm trying to override a default function with this code:

public function job_filter_tag_cloud( $atts ) {
$atts[ 'separator' ] = '';
$atts[ 'orderby' ] = 'name';
$atts[ 'order' ] = 'ASC';

return $atts;
}


Here's the entire code snippet for the function:
[[LINK href="http://pastebin.com/NpUU5XXM"]]http://pastebin.com/NpUU5XXM[[/LINK]]

I recreated the same folder structure and included the path in child's theme functions.php:

require_once( get_stylesheet_directory() . '/inc/integrations/wp-job-manager-tags/class-wp-job-manager-tags.php' );

However I get an error: Class "listify_Integration" not found.
Here's some info that might help:
[[LINK href="http://listify.astoundify.com/article/646-why-cant-i-override-this-file"]]http://listify.astoundify.com/article/646-why-cant-i-override-this-file[[/LINK]]

I appreciate your help on this.

Answers (3)

2015-03-12

Reigel Gallarde answers:

I don't understand this line:
"I recreated the same folder structure and included the path in child's theme functions.php"

did you mean you created also the same file and call it in functions.php?
if so, the fact that a child theme’s functions.php is loaded first than of the parent, this will not work. The function in the parent theme will override your $GLOBALS[ 'listify_job_manager_tags' ].

what you could do is override that function after the parent theme functions.php runs. But how could we do that if child runs first and our code is in the child? luckily we have after_setup_theme where we could hook our function.

add_action( 'after_setup_theme', function() {
//Do something
}, 42 );


next question now is how we could overwrite the function we want.
based on current code, my best guess (not tried, but might work) is:

class My_Listify_WP_Job_Manager_Tags extends Listify_WP_Job_Manager_Tags {

public function __construct() {
parent::__construct();
}

public function job_filter_tag_cloud( $atts ) {
$atts[ 'separator' ] = '';
$atts[ 'orderby' ] = 'name';
$atts[ 'order' ] = 'ASC';

return $atts;
}

}

$GLOBALS[ 'listify_job_manager_tags' ] = new My_Listify_WP_Job_Manager_Tags();


this code assumes that Listify_WP_Job_Manager_Tags class exists and we are to overwrite function job_filter_tag_cloud.

whole code would be

add_action( 'after_setup_theme', function() {
class My_Listify_WP_Job_Manager_Tags extends Listify_WP_Job_Manager_Tags {

public function __construct() {
parent::__construct();
}

public function job_filter_tag_cloud( $atts ) {
$atts[ 'separator' ] = '';
$atts[ 'orderby' ] = 'name';
$atts[ 'order' ] = 'ASC';

return $atts;
}

}

$GLOBALS[ 'listify_job_manager_tags' ] = new My_Listify_WP_Job_Manager_Tags();
}, 42 );


paste this in your child theme's functions.php
let me know if you have problem with this..


Reigel Gallarde comments:

I would like to add for clarification, Listify_WP_Job_Manager_Tags should exists in parent theme... if not, assuming you created it in child , then class would be: class Listify_WP_Job_Manager_Tags extends listify_Integration
and GLOBALS be:
$GLOBALS[ 'listify_job_manager_tags' ] = new Listify_WP_Job_Manager_Tags();


Lucian Florian comments:

Hi Reigel, thank you for your very elaborate response. It works!

One quick question. How do you think I should better organize this function override in my child theme?
You mentioned I should just add it in child's theme functions.php but wouldn't be better to actual recreate the same folder structure in the child like in parent (this is where the parent function is):
<strong>inc/integrations/wp-job-manager-tags/class-wp-job-manager-tags.php</strong>

Then in child's functions.php I can just include it there:

require_once( get_stylesheet_directory() . '/inc/integrations/wp-job-manager-tags/class-wp-job-manager-tags.php' );

This seems to work as well and it could better show what functions have been upgraded. Let me know what you think it's the best.

Thanks,
Lucian


Reigel Gallarde comments:

yes, you could do that too.. my way was just a personal preference...
the needed key idea was that child runs first then parent after.


Lucian Florian comments:

Thanks Reigel! Excellent work.

2015-03-11

Arnav Joy answers:

if you are using this in child theme's functions.php

require_once( get_stylesheet_directory() . '/inc/integrations/wp-job-manager-tags/class-wp-job-manager-tags.php' );

then you should use this

require_once( get_template_directory() . '/inc/integrations/wp-job-manager-tags/class-wp-job-manager-tags.php' );


Lucian Florian comments:

Arnav thanks, that's not the problem. Glenn is on the track. If you can contribute there it would be awesome.

2015-03-11

Glenn Tate answers:

You have to deactivate the parent function before you can over ride it with your own.

http://www.venutip.com/content/right-way-override-theme-functions


Lucian Florian comments:

Glenn, thanks for the link. How would I go about de-activating that function?