Created
July 10, 2018 07:57
-
-
Save mediter/db29fd7e616b01c48338a8c4a3065a46 to your computer and use it in GitHub Desktop.
[WordPress Function in Parent and Child Theme] #WordPress #PHP #Theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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