{"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

So how do I Override Parent Theme Functions?<\/h2>\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

  1. Use pluggable functions<\/li>
  2. Change function priority<\/li>
  3. Remove functions from the hook that they are attached to<\/li><\/ol>\n\n\n\n

    Using Pluggable Functions<\/h2>\n\n\n\n

    You can use PHP’s core function 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

    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

    Change Function Priority<\/h2>\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

    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

    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 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

    This is how you can remove a function from an action or a filter hook.<\/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

    The 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

    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 remove_action()<\/code> API here<\/a> and remove_filter()<\/code> from this location<\/a>.<\/p>\n\n\n\n

    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":"\nHow can I Override Parent Theme Functions in WordPress<\/title>\n<meta name=\"description\" content=\"This guide shows you three ways you can use to override parent theme functions in WordPress depending on how the parent theme was designed\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can I Override Parent Theme Functions in WordPress\" \/>\n<meta property=\"og:description\" content=\"This guide shows you three ways you can use to override parent theme functions in WordPress depending on how the parent theme was designed\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-20T06:36:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-08T15:32:47+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to Override Parent Theme Functions in WordPress\",\"datePublished\":\"2019-11-20T06:36:10+00:00\",\"dateModified\":\"2021-12-08T15:32:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\"},\"wordCount\":687,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg\",\"keywords\":[\"CMS\",\"Community\",\"Open Source\",\"Optimization\",\"Performance\",\"PHP\",\"Software Design\",\"Software development\",\"Web\",\"WordPress\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\",\"url\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\",\"name\":\"How can I Override Parent Theme Functions in WordPress\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg\",\"datePublished\":\"2019-11-20T06:36:10+00:00\",\"dateModified\":\"2021-12-08T15:32:47+00:00\",\"description\":\"This guide shows you three ways you can use to override parent theme functions in WordPress depending on how the parent theme was designed\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg\",\"width\":1200,\"height\":630,\"caption\":\"WordPress Override Parent Theme Functions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Override Parent Theme Functions in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How can I Override Parent Theme Functions in WordPress","description":"This guide shows you three ways you can use to override parent theme functions in WordPress depending on how the parent theme was designed","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/","og_locale":"en_US","og_type":"article","og_title":"How can I Override Parent Theme Functions in WordPress","og_description":"This guide shows you three ways you can use to override parent theme functions in WordPress depending on how the parent theme was designed","og_url":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2019-11-20T06:36:10+00:00","article_modified_time":"2021-12-08T15:32:47+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to Override Parent Theme Functions in WordPress","datePublished":"2019-11-20T06:36:10+00:00","dateModified":"2021-12-08T15:32:47+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/"},"wordCount":687,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg","keywords":["CMS","Community","Open Source","Optimization","Performance","PHP","Software Design","Software development","Web","WordPress"],"articleSection":["Articles","Guides","How To","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/","url":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/","name":"How can I Override Parent Theme Functions in WordPress","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg","datePublished":"2019-11-20T06:36:10+00:00","dateModified":"2021-12-08T15:32:47+00:00","description":"This guide shows you three ways you can use to override parent theme functions in WordPress depending on how the parent theme was designed","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/override-parent-theme-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/11\/override-parent-theme-functions.jpg","width":1200,"height":630,"caption":"WordPress Override Parent Theme Functions"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/override-parent-theme-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to Override Parent Theme Functions in WordPress"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/8265"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=8265"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/8265\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/8266"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=8265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=8265"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=8265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}