Change parent theme file function in child themes functions.php

im trying to understand how Action Hooks and Filters work and have this example i want to edit

This function is in a parent theme framework php file:

public function formatArticleCat01( $show_category = false, $shorten_text_chars = 300, $show_date = true, $show_comments = false, $show_author = false, $show_views = false ) {
$sFigure = ( $this->article_thumb != '' ) ? '<div class="col-sm-6 col-md-4 col-lg-5">'. $this->getReviewScore() .'' .$this->getFigureSmall() .'</div><div class="col-sm-6 col-md-8 col-lg-7">' : '<div class="col-xs-12">';
return '<div class="row clearfix">
            <!-- start:article.default -->
            <article class="def">
                '. $sFigure .'
                    <div class="entry">
                        '. ( $show_category ? $this->getCategoryLabelSpan() : '' ) .'
                        <h3 itemprop="name">
                            <a itemprop="url" href="'. get_permalink($this->article_link) .'">'. $this->article_title .'</a>
                        </h3>
                        <div class="entry-meta">
                            '. ( $show_date ? $this->getPostDateMeta() : '' ) .'
                            '. ( $show_author ? $this->getAuthorMeta() : '' ) .'
                            '. ( $show_comments ? $this->getCommentCountMeta() : '' ).'
                            '. ( $show_views  ? $this->getViewsLabelSpan() : '' ) .'
                        </div>
                        <div class="text hidden-xs">
                            '. MipThemeFramework_Util::ShortenText($this->article_content, $shorten_text_chars) .'
                        </div>
                        '. $this->getStarRatingLabelSpan() .'
                    </div>
                </div>
            </article>
            <!-- end:article.default -->
        </div>';
}

So lets say i want to add another class to that div just in this line

$sFigure = ( $this->article_thumb != '' ) ? '<div class="col-sm-6 col-md-4 col-lg-5">'. $this->getReviewScore() .'' .$this->getFigureSmall() .'</div><div class="col-sm-6 col-md-8 col-lg-7">' : '<div class="col-xs-12">';

It should work with a filter hook right? Can someone help me to understand the functionality of filter hooks and make this example work

I have no clue what to add in my child theme functions.php file to prevent the loss of the changes when i update the theme.

Hope someone can help me

Thanks

2 Answers
2

Filters are for modifying the data, but actions are like bus stops where you can attach your functions to theme and they will be run when the script reaches an specific state.

To use any of the above, they must be first declared somewhere. Let’s take a look at this example from the codex page of apply_filters:

// Function that modifies the data
function example_callback( $string, $arg1, $arg2 ) {
    // (maybe) modify $string
    return $string;
}
// The filter used by user
add_filter( 'example_filter', 'example_callback', 10, 3 );

// Declaration of the filter by the person who 
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );

Now as you can see, the filter is declared and given a name by using $data = apply_filters( ... ). Then, somewhere else in the code it is called by using add_filter( ... ). So, if you have no filter declared attached to that data, there is no way you can filter that piece of data.

Now about your code. It seems like you grabbed that piece of code from a class. Most decent developers use pluggable functions when they write their code. It means, they define their classes and functions as follows:

if ( ! class_exists( 'some_class' ) {
    class some_class {
        // Class code here
    }
}

This allows the user to override that specific class or function, by simply defining it himself. Take a look at your code. If it’s following the same practice, then you can copy the class to your child theme’s functions.php file, and modify the parts of it you wish.

Leave a Comment