Overwrite parent functions in child function.php WordPress

I’m using a child theme.

Here’s the parent function

Read More
function aaron_customize_css() {
    echo '<style type="text/css">';
     if ( is_admin_bar_showing() ) {
        ?>
        .main-navigation{top:32px;}
        @media screen and ( max-width: 782px ) {
            .main-navigation{top:46px;}
        }
        @media screen and ( max-width: 600px ) {
            .main-navigation{top:0px;}
        }    
    <?php
     }
    echo '.site-title{color:#' . get_header_textcolor() . ';} ';
    $header_image = get_header_image();
    if ( ! empty( $header_image ) ) {
    ?>
        .site-header {
        background: <?php esc_attr_e( get_theme_mod('aaron_header_bgcolor', '#fafafa') ) ?> url(<?php header_image(); ?>) <?php esc_attr_e( get_theme_mod('aaron_header_bgrepeat', 'no-repeat') ); ?> <?php esc_attr_e( get_theme_mod('aaron_header_bgpos', 'center top') ); ?>;
        background-size: <?php esc_attr_e( get_theme_mod('aaron_header_bgsize', 'cover') ); ?>;
        }
    <?php
    /* No image has been chosen, check for background color: */
    }else{
        if( get_theme_mod('aaron_header_bgcolor') ){
            echo '.site-header { background:' . esc_attr( get_theme_mod('aaron_header_bgcolor', '#fafafa') ) . ';}';
            echo '#action:hover, #action:focus{text-shadow:none;}';
        }
    }
    //Call to Action text color
    if( get_theme_mod( 'aaron_action_color' ) <> ' ') {
        echo '#action, #action a{ color:' . esc_attr( get_theme_mod('aaron_action_color', '#000000') ) . ';}';
    }
    echo '</style>' . "n";
}
add_action( 'wp_head', 'aaron_customize_css');

Here’s the child function which is already edited.

function aaron_customize_css_child() {
  echo "<!-- test i am in child theme   -->     ";
  echo '<style type="text/css">';
  if ( is_admin_bar_showing() ) {
    ?>
    .main-navigation{top:32px;}
    @media screen and ( max-width: 782px ) {
      .main-navigation{top:46px;}
    }
    @media screen and ( max-width: 600px ) {
      .main-navigation{top:0px;}
    }
    <?php
    }
    echo '.site-title{color:#' . get_header_textcolor() . ';} ';
    $header_image = get_header_image();
    if (! empty( $header_image)) {
      ?>
      .site-header {
        background: url(<?php header_image(); ?>) no-repeat center center;
        -webkit-background-size: cover;
        -moz-background-size:    cover;
        -o-background-size:      cover;
        background-size:         cover;
        }
    <?php
    /* No image has been chosen, check for background color: */
    }else{
        if( get_theme_mod('aaron_header_bgcolor') ){
          echo '.site-header { background:' . esc_attr( get_theme_mod('aaron_header_bgcolor', '#fafafa') ) . ';}';
          echo '#action:hover, #action:focus{text-shadow:none;}';
        }
    }
    echo '</style>' . "n";
}
add_action( 'wp_head', 'aaron_customize_css_child');

I’m trying to overwrite the parent function in function.php. I followed the instructions from http://code.tutsplus.com/articles/how-to-modify-the-parent-theme-behavior-within-the-child-theme–wp-31006. But it’s not working. I even looked into several questions on stackoverflow but it didn’t have what I was looking for. I did check out http://codex.wordpress.org/Child_Themes and it said that I can replace the parent function in child functions.php by declaring

if(!function_exists( 'aaron_customize_css')) {
  function aaron_customize_css() {}
}

I even tried many other methods but I couldn’t get it to work. Help?

Related posts

1 comment

  1. You can “remove” the parent theme function with remove_action(). This function removes a function which has been attached to a specified action hook, and is often used to replace functions with a substitute:

    remove_action('wp_head', 'aaron_customize_css');
    

    And then add your own function in place of it:

    add_action('wp_head', 'aaron_customize_css_child');
    

    So, in all, you’ll have something like the following in your child theme:

    function aaron_customize_css_child() {
        remove_action('wp_head', 'aaron_customize_css');
        // function contents
    }
    add_action('wp_head', 'aaron_customize_css_child', 100);
    

Comments are closed.