This is just one of the many possible scenarios on how to change a function in a WordPress plugin, but let me show you a real world example from Easy Property Listings, a really great and customizable real estate plugin. My client wanted to remove the <a> element from featured image on single property listing page. The thing was that if you click on that image, it would take you to that same post anyway, so having a link there was a redundant thing to have.
So let’s have a look at the function which renders featured image:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function epl_property_featured_image( $image_size = 'index_thumbnail' , $image_class = 'index-thumbnail' ) { if ( has_post_thumbnail() ) { ?> <div class="entry-image"> <div class="epl-featured-image it-featured-image"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail( $image_size , array( 'class' => $image_class ) ); ?> </a> </div> </div> <?php } } add_action( 'epl_property_featured_image' , 'epl_property_featured_image' , 10 , 2); add_action( 'epl_single_featured_image' , 'epl_property_featured_image' , 10 , 2 ); |
Why are there two actions? Because they are hooked in different parts of the plugin. So the first thing we need to do (inside functions.php or your custom functions plugin) is to remove the action. Make sure you use the same priority values. So depending on where you want to change this (on which template), you can go with:
1 |
remove_action( 'epl_property_featured_image' , 'epl_property_featured_image' , 10 , 2); |
And now the next thing is to create your own function, which will in our case be only slightly altered above function:
1 2 3 4 5 6 7 8 9 10 11 |
function epl_property_featured_image_custom( $image_size = 'index_thumbnail' , $image_class = 'index-thumbnail' ) { if ( has_post_thumbnail() ) { ?> <div class="entry-image"> <div class="epl-featured-image it-featured-image"> <?php the_post_thumbnail( $image_size , array( 'class' => $image_class ) ); ?> </div> </div> <?php } } |
And then hook this custom function into epl_property_featured_image() hook by doing this:
1 |
add_action( 'epl_property_featured_image' , 'epl_property_featured_image_custom' , 10 , 2); |
In the end, if asking yourself in what way this function outputs on that specific template, it is because the template is using do_action() hook by calling whatever is hooked on that action:
1 |
do_action( 'epl_property_featured_image' ); |
This is one of the great examples on how action hooks can be used by plugin developers to allow easy plugin modifications without having to worry about future updates.