Skip to content

Instantly share code, notes, and snippets.

@mediter
Created July 10, 2018 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediter/db29fd7e616b01c48338a8c4a3065a46 to your computer and use it in GitHub Desktop.
Save mediter/db29fd7e616b01c48338a8c4a3065a46 to your computer and use it in GitHub Desktop.
[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