1

I'm using a child theme. I want to make a change to a file which currently resides in:

mybasetheme>includes>sf-content-display>sf-post-layouts.php

So I copied the exact same file structure into my child theme and copied the file over:

mychildtheme>includes>sf-content-display>sf-post-layouts.php

But it still defaults to the base theme version?

Because it's in includes I wondered if it was to do with the functions.php file calling it. So I created a new functions.php file in my child theme as follows:

<?php

// Functions specific to child-theme

require_once( get_stylesheet_directory() . '/includes/sf-content-display/sf-post-formats.php' );

But now I see that will never work as the base theme functions.php already calls it:

>include(SF_INCLUDES_PATH . '/sf-content-display/sf-post-formats.php');

So I get this error:

Cannot redeclare sf_get_post_media() (previously declared in /Applications/MAMP/htdocs/syyco/wp-content/themes/flexform-child/includes/sf-content-display/sf-post-formats.php:15) in /Applications/MAMP/htdocs/syyco/wp-content/themes/flexform/includes/sf-content-display/sf-post-formats.php on line 36

So what do I do? I can't make the child theme include my new file, without editing the base theme, which ruins the point of having a child theme at all

  • What exactly does the function do that you need to change. Does the function needs to be called back – Pieter Goosen Aug 28 '14 at 10:44
1

When it comes to the template hierarchy, WordPress will indeed look in the child theme directory before loading from the main theme.

For all other non-specific WordPress files (like functions and 3rd party includes), it's entirely up to the main theme to decide how to load them.

Unless the theme is using locate_template to load these includes, or is checking the stylesheet (child theme) directory first, your file overrides in the child theme will have no effect.

If the functions are wrapped in if ( ! function_exists( ..., then the author has allowed for overriding, and you can simply write your own version of the function in your child theme's functions.php.

Failing that, look for any do_action( ... ) or apply_filters( ... ) around the code you want to alter - this is your last port of call for overriding default behaviour in your child theme (check out the codex on hooks).

If none of the above has been implemented by the original author, you have one of two options - edit the original theme, or contact the author and make a request to implement an API for child themes.

  • OK so this is something that requires editing the base theme? I really want to avoid that, so it can still be updated. Guess I'll have to try some CSS trickery instead. – Francesca Aug 28 '14 at 10:29
1

To explain in short, many theme authors (I do this as well) tend to organise their functions in smaller, more manageble files outside of the normal functions.php file. It makes locating a certain function easier, and it also organise and group functions together which relates to a certain aspect regarding the site

Quick example:

In my theme, I have a file called comments-functions.php. In this file I add all the functions that relates to the comment section of my theme.

Like I said, this is all done to organise code, at the end of the day, who want to look through 2000 lines of code for a specific function.

All these smaller functions files are then called into the main functions.php to register them for use.

When creating a child theme, you do not need to keep to this file structure, you can simply just add/remove/modify functions in your child theme's functions.php. If you are really just creating a child theme that has one function, then just create a functions.php file and add your function into there

A child theme's functions.php is loaded before the parent, so all functions that are wrapped in a if(!function_exists('function_name')) conditional tag can just be copied and modified in a child theme. This modified function keeps the name of the parent function. The parent function will be ignored when it is supposed to load. This is the ONLY time a function can be redeclared, that is, two functions have the same name.

If a parent function is not wrapped, you have the following choices

  • if the function is stand-alone, copy it to the child theme, rename it and modify as needed. This type of function will have a callback somewhere, so look for that function in the parent templates, copy that specific template to your child theme and replace the parent function name with your new child function. No need to worry about removing or doing anything with the parent function. Example, if the parent theme name is parent_function() and it is called in index.php, copy index.php to your child theme, and replace parent_function() with your child function, say for instance child_function()

  • If a function is hooked to a specific action or filter, you'll need to remove that function once it is registered. See Removing Actions and Filters. You can now register a new function and hook that to the same hook that the parent theme was hooked to. Just remember, priority is very important here, executing the wrong order will fail.

0

Just change only function that you want to edit. See this post

1)Copy (in full) the function you want to override from the parent theme.

2) Paste it into functions.php in the root of your child theme’s folder. If functions.php doesn’t exist, create it.

3) Rename the function from parent_theme_function to child_theme_function.

4) Deactivate the parent function.

5) Activate the child function.

  • 1
    How do I deactivate the base theme function without editing the functions file on the base theme? – Francesca Aug 28 '14 at 10:26
  • Also there is no function name. It is just this line: include(SF_INCLUDES_PATH . '/sf-content-display/sf-post-formats.php'); – Francesca Aug 28 '14 at 10:27
  • I am talking about function or any other code in this file... – Shail Aug 28 '14 at 10:30
  • How to override function? See link: php.net/manual/en/function.override-function.php – Shail Aug 28 '14 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.