Action hooks – bbird.me http://bbird.me WordPress quick tips Wed, 08 Aug 2018 16:39:07 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 How to change a WordPress plugin function using action hooks http://bbird.me/how-to-change-a-wordpress-plugin-function-using-action-hooks/ http://bbird.me/how-to-change-a-wordpress-plugin-function-using-action-hooks/#respond Thu, 28 Apr 2016 09:56:02 +0000 http://bbird.me/?p=496 This is just one of the many possible scenarios on how to change a function in a WordPress plugin, but

Post How to change a WordPress plugin function using action hooks je prvi puta viđen na bbird.me.

]]>
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:

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:

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:

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:

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:

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.

Post How to change a WordPress plugin function using action hooks je prvi puta viđen na bbird.me.

]]>
http://bbird.me/how-to-change-a-wordpress-plugin-function-using-action-hooks/feed/ 0
Actions do stuff, filters change stuff http://bbird.me/actions-do-stuff-filters-change-stuff/ http://bbird.me/actions-do-stuff-filters-change-stuff/#respond Fri, 22 May 2015 10:15:10 +0000 http://bbird.me/?p=134 This post is going to be a bit different - it will summarize actions and filters that exist within WordPress ecosystem.

Post Actions do stuff, filters change stuff je prvi puta viđen na bbird.me.

]]>
This post is going to be a bit different – it will summarize actions and filters that exist within WordPress ecosystem. There are numerous articles describing action and filter hooks and all that information might appear overwhelming. From all the talk and information about hooks, I find these definitions to explain it in the best way:

  • Actions allow you to add extra functionality at a specific point in the processing of the page.
  • Filters allow you to intercept and modify data as it is processed.


Actions

You’ll find two functions when it comes to action hooks (there are more than two, though, such as remove_action(), but we are not interested in them at this point):

The first question would normally be – when to use either of these? It may be a very rough answer, but I’ll give it a shot:

  1. If you’re developing a theme or a plugin, there is a chance that you will use both.
  2. If you’re making some customizations on a theme (adding some functionality) or a plugin, it is likely that you will need add_action only.

Let’s have a look at wp_footer() function, which also exists as an action hook.

function wp_footer() {
    /**
     * Print scripts or data before the closing body tag on the front end.
     *
     * @since 1.5.1
     */
    do_action( 'wp_footer' );
}

This is copied directly from WordPress core. If we want to output something (such as function) into wp_footer() action hook, we would use add_action:

function output_some_text() {
echo 'This outputs some text into footer';
}
add_action( 'wp_footer', 'output_some_text' );

At this point, the logical question is – how do I know if there is an action hook (that I would need at a certain point) available for the theme, plugin or WordPress? You will need to dig into documentation – for WordPress, you can find all the action hooks inside official code reference. Advanced plugins such as WooCommerce also have a generous list of hooks – take a peek.

To conclude – action hooks don’t change what’s already there, they are used for adding something new instead.


Filters

Filter is another type of hook and comes in two flavors (as with action hooks, there are some other filters available, but we are not interested in them at this point):

The difference is the same as it is the case with action hooks – if you are developing something from scratch, you will likely use apply_filters.  On the other hand, if you are doing some changes on the existing themes are plugins, you will use add_filter. As an example, let’s create a function that will not earn us the best developer award – it will only disable the logout link in the WordPress admin area. Let’s have a look at the wp_logout_url() function and logout_url filter (copied directly from WordPress core):

function wp_logout_url($redirect = '') {
    $args = array( 'action' => 'logout' );
    if ( !empty($redirect) ) {
        $args['redirect_to'] = urlencode( $redirect );
    }
 
    $logout_url = add_query_arg($args, site_url('wp-login.php', 'login'));
    $logout_url = wp_nonce_url( $logout_url, 'log-out' );
 
    /**
     * Filter the logout URL.
     *
     * @since 2.8.0
     *
     * @param string $logout_url The Log Out URL.
     * @param string $redirect   Path to redirect to on logout.
     */
    return apply_filters( 'logout_url', $logout_url, $redirect );
}

For this purpose, we will want to change the $logout_url variable only, so we will use add_filter() in the following way:

function disable_logout_link ($logout_url) {
   $logout_url = ''; //no link defined
   return $logout_url;  
   
}
add_filter( 'logout_url', 'disable_logout_link' )

The possibilities of filters are endless, since filter hooks are frequent in many plugins, themes and most importantly – WordPress core has numerous filters available, allowing us to change practically every single part of it.

Post Actions do stuff, filters change stuff je prvi puta viđen na bbird.me.

]]>
http://bbird.me/actions-do-stuff-filters-change-stuff/feed/ 0