1. Docs
  2. Developer Guide
  3. Using Actions

Using Actions

By overriding templates and/or adding your own CSS, you can amend how the property pages and components look. Another method of changing where things appear is to use actions.

We can do this by using a range of actions made available to us by Property Hive.

What are actions?

Actions are a fundamental part of WordPress. If you’re unsure on what actions are, we recommend you take a look at the WordPress documentation where they are explained and examples given. In summary however, actions allow us to do or show something at a particular point on the page.

Where does this code go?

Any amendments to actions can go in your theme’s functions.php file. If you’re not familiar with editing PHP files there are also plugins available which allow you to add the code straight into WordPress.

Moving and swapping details using actions

There are an infinite number of ways we can use actions to amend the layout but let’s start simple.

Let’s imagine for a moment that on the single property record page we want to place the title/address of the property above the images, instead of below them. If we open up the template file called ‘content-single-property.php‘ we see this by default:

<?php
/**
* propertyhive_single_property_summary hook
*
* @hooked propertyhive_template_single_title - 5
* @hooked propertyhive_template_single_floor_area - 7
* @hooked propertyhive_template_single_price - 10
* @hooked propertyhive_template_single_meta - 20
* @hooked propertyhive_template_single_sharing - 30
*/
do_action( 'propertyhive_single_property_summary' );
?>

What we see here is an action called ‘propertyhive_single_property_summary‘ and above it, in the comments, we see a list of all the functions that will be called as a result of this action, and the priority of each. The lower the priority, the earlier it will be executed. In this example we see we have everything from the title with priority 5, to sharing with priority 30.

Now look a little higher in the ‘content-single-property.php‘ template and we’ll see this:

<?php
/**
* propertyhive_before_single_property_summary hook
*
* @hooked propertyhive_show_property_images - 10
*/
do_action( 'propertyhive_before_single_property_summary' );
?>

This is similar to what we’ve already seen, but this time the action is called ‘propertyhive_before_single_property_summary‘ and there is only one function being referenced which is to include the images.

So our aim if you remember was to move the title from below the images to above them. What we need to do is move the title function from one action to another.

First, let’s remove it from it’s current position:

<?php
remove_action('propertyhive_single_property_summary', 'propertyhive_template_single_title', 5);
?>

Easy right? We call the remove_action() function, pass in the current action name, the function in question, as well as the current priority. Note that the priority must match that in the template otherwise it will have no effect.

Now all we need to do is the opposite and add it to the other action:

<?php
add_action('propertyhive_before_single_property_summary', 'propertyhive_template_single_title', 5);
?>

And that’s it. Now the title will appear above the images. This is just a basic example but the same principles can be applied to most of the Property Hive templates.

Using actions to add additional information

As well as swapping information, you can also output your own bespoke information using existing actions and your own custom functions. The example below shows how we could output the number of bedrooms on the search results page:

<?php
add_action('propertyhive_after_search_results_loop_item_title', 'my_function_to_show_bedrooms', 15);
function my_function_to_show_bedrooms()
{
global $property;
echo 'Bedrooms: ' . $property->bedrooms;
}
?>

In the above example we’re using the existing hook called ‘propertyhive_after_search_results_loop_item_title’, we’re setting the priority to 15 so it goes between the price and summary, and then using a custom function we’ve called ‘my_function_to_show_bedrooms’ to output the information we want to show.

Was this article helpful to you? Yes No