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

Solutions Collecting From Web of "Change parent theme file function in child themes functions.php"

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.

In layman’s term Filters are used to modify or change an existing data such as content of a post, or any other defined value. Actions on the hand do something when a particular thing is run.

For both actions and filter you have to declare them first, i.e in case of filters you must define what can be changed using a filter, and for actions hooks what will cause the action to run.

Note:
In your above example, there is no mention of any filter, therefore the only way for you to modify that function is to copy it in your child theme’s function.php file and add a class manually.

Now to Understand how filters work, lets look at an example,

Lets’ say you want to print an author’s name, then you can apply a filter to it, so that in case you want to change the author’s name you can do so without modifying the original function.

/*
 * - 'change_author_name_filter' is the filter hook $tag
 * - 'Digvijayad' is the value being filtered
$author_name = apply_filter('change_author_name_filter', 'Digvijayad');

Now if you use $author_name then it will use 'Digvijayad' as the default author name. However if you add a filter like the following.

// filter call_back
function change_author_name( $author_name ){
    //here you can do whatever you want to author name;
    // you can modify it or replace it altogether.

    // To print 'Digvijayad & Jack Johansson' you can do the following.
    // $author_name .= ' & Jack Johansson';

    // or you can replace it altogether. 
    $author_name = 'Jack Johansson';

    return $author_name;
}
add_filter('change_author_name_filter', 'change_author_name', 10, 1);

Now if you use $author_name it will be replaced by 'Jack Johansson'

As for Actions, they trigger a response when something is done. lets take Alarm as analogy. First you set a time for an alarm, and when the time is reached, the alarm clock plays a sound to tell you that it is time.

Now with actions, you can tell the alarm clock to do other things such as ‘call a friend’ too when the time is reached. Now lets put this in code.

Let’s assume you have already set the time, now you set an do_action hook that will do something, when the time is up.

//code when time is checked
// and now time is up
do_action('time_is_up'); //basically it will perform this action when ever the time is up

now you can add more actions to this particular event. From the analogy, you can now tell it to call a friend when ‘time_is_up’ actions is run.

// Now Whenever 'time_is_up' runs this function will run as well
function call_friend(){
    //code for calling a friend;
    echo 'calling friend';
}
add_action('time_is_up', 'call_friend', 10);

You can similarly add more actions to the same 'time_is_up' action hook.
For example, maybe you also want to call your family member

function call_my_family(){
   //code to call my family
   echo 'calling family';
}
add_action('time_is_up', 'call_my_family', 10);

Hopefully this simple explanation clears out your confusion about actions and filters.

For more reading, you can read Tom McFralin’s article on Actions and Filters.