Add custom php file in child theme WP

2.4k Views Asked by At

I would like to add cutom html code in a php file that is in the main WP theme I'm currently using. So I decided to do that using a child theme but I can't see where I'm wrong in my code and why this isn't working ?

Here is my functions.php code :

<?php
add_action( 'wp_enqueue_scripts', 'boo_child_theme_style', 99 );
add_action( 'wp_enqueue_scripts', 'boo_child_portfolio_style', 99 );

function boo_parent_theme_scripts() {
    wp_enqueue_style( 'base', get_template_directory_uri() . '/style.css' );
}
function boo_child_theme_style(){
    wp_enqueue_style( 'child-boo-style', get_stylesheet_directory_uri() . '/style.css' );   
}

function boo_parent_portfolio_scripts() {
    wp_enqueue_style( 'base', get_template_directory_uri() . '/templates/portfolio/tmpl-grid.php' );
}
function boo_child_portfolio_style(){
    wp_enqueue_style( 'child-boo-style', get_stylesheet_directory_uri() . '/tmpl-grid.php' );   
}

So for the style.css it work but for the php file it doesn't work and I don't know why... Can someone explain and help me please ?

Thanks in advance !

2

There are 2 best solutions below

1
On BEST ANSWER

@arcath is right you can not add php files using Enqueue functions. They are only used for adding/overwritting .css and .js files. That is also by two different methods for stylesheets you use wp_enqueue_style and for Javascript you use wp_enqueue_scripts.

Don't call method again and again best way to call enqueue method is by calling it only once in your function.php inside child directory example.

function adding_scripts_and_styles() {
wp_enqueue_script('unique_child_custom_js', get_stylesheet_directory_uri() . '/directory_path_if_any/custom.js', array('jquery'), true, true );
wp_enqueue_script('unique_child_custom_css', get_stylesheet_directory_uri() . '/directory_path_if_any/custom.css'); 
}

add_action( 'wp_enqueue_scripts', 'adding_scripts_and_styles');

For overwriding wordpresss templates create a php file in you child theme wordpress directory with the same name. Wordpress reads child theme template files first while loading.

Example if you want to overwride archive.php page template, create an archive.php in your child theme and then wordpress will use archive.php file from your child theme ignoring the parent theme archive.php.

Hope this help! Happy Coding :)

0
On

You can't enqueue PHP through the scripts/style system.

To replace some/all of a page with a child theme you need to replace the template for that page.

See the Template Herarchy for details on how WordPress picks the correct template for a page.

If you only want to change a small part of the page thats upto the parent themes developer how easy that is going to be.

Some themes implement filters to help child themes modify the page but as I said they don't have to so it might not be something you can use.