Publishing with actions provided by your theme

WordPress offers an option for theme developers to have do_action functions in different parts of the code. This can be useful, because with add_action you can run your own code in these places, like you a shortcode could run there.

Step 1 - Find actions

As you see in our Which code is making a certain part of a website? documentation from step 3, you can search for codeparts within your theme. Most people want to insert their sliders right under the </header> part, so you could try to search for that codepart, to see if you can find any do_action codes after it. You should check the header.php file first, as usually this code is there.

You could also try to search for the word  do_action, as often the specified names are telling a lot about the placement, like "after_header" or "before_footer" is commonly used.

For example in Flatsome theme this is how the code looks like in the end of the header.php file:

<?php do_action( 'flatsome_before_header' ); ?>

<header id="header" class="header <?php flatsome_header_classes(); ?>">
	<div class="header-wrapper">
		<?php get_template_part( 'template-parts/header/header', 'wrapper' ); ?>

<?php do_action( 'flatsome_after_header' ); ?>

<main id="main" class="<?php flatsome_main_classes(); ?>">

so there is a do_action before the header and after it. Now we should focus on this part:

<?php do_action( 'flatsome_after_header' ); ?>

as we want to run our code after the header.

Step 2 - Create a plugin

Create a folder inside your plugins folder. I will call it "addheader":
wp-content\plugins\  addheader

Create a PHP file with the same name you used for the folder:
wp-content\plugins\addheader\  addheader.php

and make it contain this code with a name of your choice:

 * Plugin Name: Add header

Your plugin is ready and you can activate it. Now any code written in it will run on your website!

Step 3 - Add action

Add this code to your plugin as a starting point:

 * Plugin Name: Add header

function AddHeaderSmartSlider() {
	echo do_shortcode('[smartslider3 slider="123"]');
add_action( 'flatsome_after_header', 'AddHeaderSmartSlider' , 99999);
Part 1

At the  do_shortcode part you should replace the shortcode:

[smartslider3 slider="123"]

with your slider's shortcode. You could use advanced shortcodes too, if you would only want your slider to show up on specific pages.

Part 2

At the  add_action replace flatsome_after_header with the action you found in your theme.

Part 3

99999 means the priority, so if there are multiple codes using this action, then this defines the order of what is running first, second, etc.. The smaller numbers are running first, the bigger numbers later. This value is optional, so you don't have to add it, if only your code runs in this action:

add_action( 'after_header_example', 'AddHeaderSmartSlider');

And you are done. Your shortcode will run at the given action.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.