Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress Function in Parent and Child Theme] #WordPress #PHP #Theme
// To write a pluggable function, you simply enclose it in a conditional tag to check if a function with that name has already been run:
<?php
if ( ! function_exists ( 'my_function' ) ) {
function my_function() {
// Contents of your function here.
}
}
?>
Then when you come to write a function in your child theme which you want to override the one in the parent theme, you just give it the same name as the one in the parent theme:
<?php
function my_function() {
// Contents for your function override here.
}
?>
WordPress will run the function in the child theme first, and when it comes to the one in the parent theme, it'll check if it already exists and because it does, it won't run it.
# Function Priority
<?php
function child_function() {
// Contents for your function here.
}
add_action( 'init', 'child_function', 15 );
?>
higher priority fires later
<?php
function parent_function() {
// Contents for your function here.
}
# Remove a Function
add_action( 'init', 'parent_function' );
?>
To remove this function from its action hook and therefore prevent it from firing, you create a function in your child theme to remove it using remove_action():
<?php
remove_action( 'init', 'parent_function' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.