{"id":8265,"date":"2019-11-20T01:36:10","date_gmt":"2019-11-20T06:36:10","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=8265"},"modified":"2021-12-08T10:32:47","modified_gmt":"2021-12-08T15:32:47","slug":"override-parent-theme-functions","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/","title":{"rendered":"How to Override Parent Theme Functions in WordPress"},"content":{"rendered":"\n
If you are a budding WordPress theme developer there is one thing you will have to end up knowing sooner or later. That is, how to override parent theme functions in WordPress<\/a>.<\/p>\n\n\n\n The reason you would want to do this is to be able to alter some or most of the behavior of a parent theme while retaining other features that are inherited into your child theme.<\/p>\n\n\n\n With this WordPress feature, three main parts of the parent theme can be manipulated. They are the templates, the main CSS<\/a> stylesheet, and the functions.<\/p>\n\n\n\n 1. The templates:<\/strong> These are replaced by those in the child theme if they bear the same name and location. This is necessary to retain the well-formed output which normally ends up in your web browser as HTML<\/a>.<\/p>\n\n\n\n 2. CSS Style:<\/strong> The style.css file of your child theme, and any others by extension, are appended below the ones from the parent theme. The browser will then process the CSS giving priority to the last occurrence of any matching rules.<\/p>\n\n\n\n 3. Functions:<\/strong> The functions.php file contains functions specific to your theme. unlike templates or CSS styles, you cannot override parent theme functions simply by declaring the same function in the child theme.<\/p>\n\n\n\n The parent theme functions are registered after those of the child theme which means any similar function names will be overridden by those of the parent theme in WordPress.<\/p>\n\n\n\n Because the functions in WordPress are first registered before being used, it allows you to specify which function ends up being used on your website. There are three main ways you can do this:<\/p>\n\n\n\n You can use PHP’s core function This method is the best practice for those developing parent themes and will stop the parent theme function from being declared if it has been declared in the child theme.<\/p>\n\n\n\n It is not always the case that you encounter a parent theme developed with pluggable functions, which in itself is a bad design for a parent theme.<\/p>\n\n\n\n WordPress provides another solution to this problem. Basically in WordPress functions are generally added to an action or filter hook. When this is done you may also supply a priority level to it.<\/p>\n\n\n\n If you do not supply a priority it is given the default of 10<\/strong>. If you find the parent theme function has no priority assigned then anything above 10 should give it a higher priority as in WordPress functions are added to the filter hook in ascending order.<\/p>\n\n\n\n Here is an example of how this works in your child theme.<\/p>\n\n\n\n Another alternative is to completely remove the parent theme function from the filter hook and add your child theme in its place. This option can also be used to remove a function from the filter hook and net necessarily replace it if you do not need an alternative.<\/p>\n\n\n\n This can be done using the WordPress functions This is how you can remove a function from an action or a filter hook.<\/p>\n\n\n\n The You will also need to specify the priority of the original function and set the same priority level or lower because technically you cannot remove an action before it has been triggered.<\/p>\n\n\n\n At this point, the parent function is no longer registered and you are now free to declare it in your child theme as shown above.<\/p>\n\n\n\n You can find more details about the Hopefully, this guide should point you on your way on how to override parent theme functions in WordPress.<\/p>\n","protected":false},"excerpt":{"rendered":" If you are a budding WordPress theme developer there is one thing you will have to end up knowing sooner or later. That is, how to override parent theme functions…<\/p>\n","protected":false},"author":1,"featured_media":8266,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,16],"tags":[136,142,424,433,449,452,543,544,635,651],"yoast_head":"\nSo how do I Override Parent Theme Functions?<\/h2>\n\n\n\n
Using Pluggable Functions<\/h2>\n\n\n\n
function_exists()<\/code> to check if the function has already been declared. If it has not been declared then the function will be declared and used.<\/p>\n\n\n\n
if ( ! function_exists ( 'my_function' ) ) {\n function my_function() {\n \/\/ Contents of your parent theme function here.\n }\n}\n\nfunction my_function() {\n \/\/ Contents for your child theme function override here.\n}<\/code><\/pre>\n\n\n\n
Change Function Priority<\/h2>\n\n\n\n
function your_function() {\n \/\/ Contents for your parent theme function here.\n}\nadd_action( 'init', 'your_function', 20 );\n\nfunction your_function() {\n \/\/ Contents for your child function override here.\n}\nadd_action( 'init', 'your_function', 25 );<\/code><\/pre>\n\n\n\n
Remove Functions From the Hook<\/h2>\n\n\n\n
remove_action()<\/code> and
remove_filter()<\/code> depending on whether the function was added to an action or a filter hook in the parent theme.<\/p>\n\n\n\n
function child_remove_parent_function() {\n remove_action( 'init', 'parent_function', 10 );\n}\nadd_action( 'wp_loaded', 'child_remove_parent_function' );<\/code><\/pre>\n\n\n\n
remove_action()<\/code> and
remove_filter()<\/code> functions must be attached to a filter hook for it to work and that is why it is not called stand-alone.<\/p>\n\n\n\n
remove_action()<\/code> API here<\/a> and
remove_filter()<\/code> from this location<\/a>.<\/p>\n\n\n\n