Support » Fixing WordPress » Getting one child function to supercede only the one parent function

  • Resolved rccadmin


    I want to have a child theme supersede a parent function without replicating the entire parent php file and all the other functions it contains.

    Parent has the function that needs modification in /inc/template-tags.php
    Template-tags.php contains many functions. Only one (I’ll call it “post_date_and_byline”) needs a tweak (trying not to show the date and author of a post).

    If the theme is updated, I want my tweak to affect only that particular function in the template-tags.php file and none of the others that might be updated.

    Right now, the child functions.php has
    require_once( get_stylesheet_directory(). ‘/inc/template-tags.php’ );

    The child’s template-tags.php is identical to the parent except the change in the one function.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Just create a template-tags.php file in the child folder (use the same folder structure as in parent., eg. it needs to be in “inc” folder in child dir) and in that new file, write the code for the changed function.

    It depends on how the function is setup in the template-tags.php file. If it has apply_filters in it, you can use the add_filter function to modify it

    If the parent theme is using get_theme_file_uri() to require the template-tags.php file, you would need to supply all of the functions in it. Hopefully, this is not how the parent is coded.
    If the parent theme makes its functions pluggable (by putting if function_exists around it), then you can define that function in the child functions.php and that’s it. The if statement keeps the parent function from being defined.

    Otherwise, the parent will likely use get_template_directory_uri() to require the file and not put an if statement to check if it exists. This is when you use a filter, if there is one, or clone the template-part where the function is called, if it’s called directly.

    Thank you, all, especially Joy for the expanded set of circumstances.

    Indeed, the parent had if’s wrapping the functions and did not use “uri”.

    I had the structure replicated, but now tried deleting everything in the .php file except the function of interest, which was modified. I removed the wrapped “if”.

    It seems to be working!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.