In this article I will give a simple example of how to implement do_action for wordpress theme. Due to the action hook user to easily create child themes and change only with remove action.
When you create a wordpress parent theme, then you should think about how the user to easily create child theme. You do not need to copy from an existing file when creating a child theme. You just need to understand do_action hook, which is where we can use in the function. But first you need to understand that in the functions if you want to add HTML markup. You need to echo or not to echo. In practice, I will give do_action be applied where the file footer.php. You may follow, and apply the theme that you have created.
In the first step I enter a function in functions.php in the parent theme. Below is an example of code to echo and not the echo.
If you prefer to use the echo function is the basis of creation:
function mytheme_footer (){
echo '<footer>';
echo '<div class="site-info">';
echo ' <p>Your brand name here</p>';
echo '</div>';
echo ' </footer>';
echo '</body>';
echo '</html>';
}
If you'd rather not use the echo function is the basis of creation:
function mytheme_footer (){
?>
<footer>
<div class="site-info">
<p>Your brand name here</p>
</div>
</footer>
</body>
</html>
<?php
}
You may choose one of the methods above and entries in the functions.php. It must be input in the parent theme as we will prove how do_action work.
Once you enter one of the codes above you should understand add_action to call the function.
add_action('my_footer','mytheme_footer');
function mytheme_footer (){
echo '<footer>';
echo '<div class="site-info">';
echo ' <p>Your brand name here</p>';
echo '</div>';
echo ' </footer>';
echo '</body>';
echo '</html>';
}
add_action('my_footer','mytheme_footer');
function mytheme_footer (){
?>
<footer>
<div class="site-info">
<p>Your brand name here</p>
</div>
</footer>
</body>
</html>
<?php
}
add_action('my_footer','mytheme_footer');
Okay, let's start implementing the code that you enter in the functions.php into footer.php. You just need a line of code of the action.
<?php
do_action('my_footer');
You might want to change the footer of the parent theme with the markup you created yourself. You only need to remove the action from the parent theme. As the above example is given add_action ('my_footer', 'mytheme_footer'); Then you need to remove function mytheme_footer .
In the child theme would be a good idea to make a set up for your theme. It's so easy for you to do the action and remove the add action. example:
function mychild_theme_setup() {
}
add_action( 'after_setup_theme', 'mychild_theme_setup' );
So, when you want to remove you simply enter remove_action and add_action that you created. Suppose you want to remove the footer.
function mychild_theme_setup() {
remove_action('my_footer','mytheme_footer');
}
add_action( 'after_setup_theme', 'mychild_theme_setup' );
Here you remove 'mytheme_footer' and add your own line function I demonstrated 'my_child_theme_footer'
function mychild_theme_setup() {
remove_action('my_footer','mytheme_footer');
add_action('my_footer','my_child_theme_footer');
}
add_action( 'after_setup_theme', 'mychild_theme_setup' );
This is an example of the application of the child theme where you remove footer and add the markup that you created yourself. I gave an example by using echo, you can apply other means that do not use echo.
function mychild_theme_setup() {
remove_action('my_footer','mytheme_footer');
add_action('my_footer','my_child_theme_footer');
}
add_action( 'after_setup_theme', 'mychild_theme_setup' );
function my_child_theme_footer (){
echo '<footer>';
echo '<div class="site-info">';
echo ' <p>Your child theme footer name here</p>';
echo '</div>';
echo ' </footer>';
echo '</body>';
echo '</html>';
}
You can develop further your way, as easy to recognize markup with before and after . For example, you want to create a markup that can change the parent theme. You can add the open, after and before.
do_action('before_body_tag');
echo '<body>';
do_action('after_body_tag');
If you notice this is a simple and widely in use are also WordPress theme framework. Why?
By do_action you easily create child theme, is both beside the user and the developer. Because when theme and plugin updates the user does not lose configuration and could develop further.
Not a member? Register now!.