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

Override a functions.php file in plugin using child functions.php WordPress

  • SOLVED

HI there
I need instructions on how to override a functions.php file within a plugin using functions file in my child theme.

The file below contains functions i want to customise using a child functions file.

/wp-content/plugins/wp-job-manager-field-editor/includes/functions.php

Below is a sample of the code in the plugin functions.php file.

>>>

if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! function_exists( 'get_job_field' ) ){

/**
* Get Job Field Value
*
* Will return any default or custom job field values
* from specific job if post ID is included, otherwise
* will return from current job posting.
*
* @since 1.1.9
*
* @param $field_slug Meta key from job posting
* @param null $job_id Optional, Post ID to get value from
* @param array $args
*
* @return mixed|null
*/
function get_job_field( $field_slug, $job_id = null, $args = array() ){

if( ! $job_id ) $job_id = get_the_ID();

$field_value = get_custom_field_listing_meta( $field_slug, $job_id, $args );

return $field_value;

}

}

if ( ! function_exists( 'the_job_field' ) ) {

/**
* Echo Job Field Value
*
* Same as get_job_field except will echo out the value
*
* @since 1.1.8
*
* @param $field_slug Meta key from job posting
* @param null $job_id Optional, Post ID to get value from
* @param array $args
*
* @internal param null $output_as
* @internal param null $caption
* @internal param null $extra_classes
*/
function the_job_field( $field_slug, $job_id = null, $args = array() ) {

...etc

Answers (3)

2015-12-31

dimadin answers:

You can't override this file with child theme. Reason for this is simple: this file is loaded before theme's functions.php file (no matter parent or child), since that is hardcoded in main plugin's file, and this file only has plain functions, there is no any hook.

You can override using different way. There is a folder /mu-plugins in /wp-content folder where all files are loaded before any other plugin or theme. Since before functions are created in this file first it checks if they exist, this means that you can safely create them in /mu-plugins file and original functions won't be created.

So, what you need to do, copy file

/wp-content/plugins/wp-job-manager-field-editor/includes/functions.php

to a new file located in /mu-plugins, for example

/wp-content/mu-plugins/wp-job-manager-field-editor-functions.php

You can include only those functions that you edit, removing those that are not edited for simplicity.

So you need just to make sure that you create folder /mu-plugins in /wp-content folder (if it doesn't already exist), and place edited functions in top-level file.

2015-12-31

Ian Lincicome answers:

Well, first of all, you'll have to make an edit the parent theme's functions.php file first if the functions exist there. This is because functions.php files do not work like other theme files, they are not overridden by child theme functions.php functions. In fact, the opposite is true, parent theme functions.php file overrides the child theme's functnions.php....therefore the only way to make it work as you need it to would be to edit the parent theme's functions.php file by making the functions inside of the parent theme functions.php plugable. To make functions plugable, you need to do this:

to make a parent theme's function in functions.php file pluggable, enclose it in a conditional tag to check if a function with that name has already been run:


if ( ! function_exists ( 'function_name' ) ) {
function function_name() {
// Contents of function_name.
}
}

So you have to wrap functions in the parent theme's functions.php in this manner for any functions you wish to override in the child functions.php. I hope this clarifies. Feel free to ask question is this isn't clear enough to you.
Ian


UPDATE: you later ask if a plugin's functions.php will override the main theme's functions.php and the answer is the same and so is the solution. I am sorry I didn't explain clear enough because I didn't notice you mentioned the functions were going to be in a plugin, but nonetheless, this answer still applies. The best way to solve this is to edit the main theme's functions.php by making the original functions pluggable. It could be a hassle when it comes time to update....basically if you update the theme, you'd have to make sure your changes stay in place within the theme's functions.php file, but you could always just not update the theme or manually update the functions.php file if you do update the theme. There is another way to override a function, but I would have to see the entire code to tell you if it would work in your case, basically you have to research the filters and hooks involved and see if you can unhook them, then override them effectively in your plugin. So, let me know if this solution won't work for you and I can help you explore the other, more complex way of doing it if necessary. Hope this helps!
Ian


Also, from looking at the portion of the code you posted, some of the function or maybe all of the function are already pluggable, so all you'd have to do in that case is copy them over to your plugin's functions.php file and edit them according to your needs and everything should be fine. I was thinking you might have been dealing with some functions that were not already pluggable(contain the if statement), but it appears as if your functions may already be pluggable, so I'm sorry if that confused you any. In short, copy the functions to yoru plugin and be sure to keep the if statements because that way if another plugin has to override them, it won't cause an error. Let me know if that clears things up for you. Happy New Year!
Ian


evosoon comments:

When you say parent theme's functions.php are not overriden by child functions.php, does this rule also apply to a Plugin's functions.php? Does the parent theme functions.php get run last or the plugins?

If i want a function to be the overriden, how do i include this function in the child theme's functions.php?

2015-12-31

Rempty answers:

All functions wrapped by and If, can be overriden
You can create a functions.php in your child theme and copy the functions you want override.