Function in Child Theme not overriding Parent Theme function

The question:

I have read up on this and tried it myself, but I cannot seem to figure out how to override a parent theme function using my child theme.

I need to know what I am doing wrong (and I am sure there are several wrong things).

Here is the function in the parent theme:

function html5_insert_image($html, $id, $caption, $title, $align, $url, $size, $alt) {
    $url = wp_get_attachment_url($id);
    $src = wp_get_attachment_image_src( $id, $size, false );
    $html5 = "<figure class='align$align'><a href='$url'>";
    $html5 .= "<img src='$src[0]' alt='$alt' />";
    if ($caption) { 
        $html5 .= "<figcaption>$caption</figcaption>";
    }
    $html5 .= "</a></figure>";
    return $html5;
}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );

Here is the function in the child theme:

function remove_html5_insert_image(){
    remove_filter('init','html5_insert_image',1);
}
add_action('after_setup_theme','remove_html5_insert_image');

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

First, you are confused. It is not the same “override a function” that remove a filter. Assuming that you want to remove a filter, I think that your code should be:

function remove_html5_insert_image(){
    remove_filter('image_send_to_editor','html5_insert_image', 10);
}
add_action('after_setup_theme','remove_html5_insert_image');

Note that the filter tag and the priority argument in remove_filter must match the filter tag and the priority defined in the filter you want to remove.

Anyway, There is no need of that function since WP 3.9, use this instead to make WordPress use figure and figcaption in the output of captions:

add_action( 'after_setup_theme', 'cyb_theme_setup' );
function tbn_theme_setup() {
    // See more in http://codex.wordpress.org/Function_Reference/add_theme_support#HTML5
    add_theme_support( 'html5', array( 'caption' ) );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment