I need to add a shortcode into one of the PHP files (replacing a search bar code that is hardcoded into the header PHP file). How do I add a shortcode to the header PHP file?

This is the code that I am looking at:

<?php if ( (is_front_page()) && (of_get_option('g_search_box_id') == 'yes') ) { ?>
    <div class="search-form-wrap hidden-phone" data-motopress-type="static" data-motopress-static-file="static/static-search.php">
        <?php get_template_part("static/static-search"); ?>
<?php } ?>

And I need to remove the search-form-wrap with a different shortcode search form (for mls).

Here is the shortcode I have to deal with:

[idx_search link="###########" detailed_search="off" destination="local" user_sorting="off" location_search="on" property_type_enabled="on" theme="hori_round_light" orientation="horizontal" width="730" title_font="Arial" field_font="Arial" border_style="rounded" widget_drop_shadow="off" background_color="#ffffff" title_text_color="#000000" field_text_color="#545454" detailed_search_text_color="#234c7a" submit_button_shine="shine" submit_button_background="#59cfeb" submit_button_text_color="#000000"]

  • Make a child theme and change the specific file. Using the WordPress Codex and Google will take you through it. – flomei Nov 5 '15 at 17:50
  • I am using a child theme, but I am lost as to how to make it work out. – damann Nov 5 '15 at 17:51
  • What do you mean? Copy the header.php to your child theme, modify and save it. When you use your child theme, the modified header.php will be used... – flomei Nov 5 '15 at 17:55
  • I don't know how to take the <div> to </div> in the above code and replace it with a shortcode... I don't know how to do it and make it work. Do I just throw in [shortcode]? – damann Nov 5 '15 at 17:58
  • What Milo said... – flomei Nov 5 '15 at 18:06

In general, inserting and executing shortcodes directly into templates files is not good idea and it is not what shortcodes are intented for. Instead, you should use the shortcode callback just like any other PHP function or template tag.

For example, let's imaging this shortcode:

add_shortcode('someshortocode', 'someshortocode_callback');
function someshortocode_callback( $atts = array(), $content = null ) {

    $output = "Echo!!!";

    return $output;


Then, instead of doing this:

echo do_shortcode( '[someshortocode]' );

You should do this:

echo someshortocode_callback();

Obviously, to use the shortcode callback as template tag, the shortcode must be coded in a way it allows this use, so it may be not possible (although I've never found a shortcode that don't allow the use of the callback directly, it may exist). In this case, and only in this case, I would use do_shortcode( '[someshortcode]' ) (I've never would use it really).

Note that shortcodes are PHP placeholders to be used where PHP code can not be inserted, that is why it has no sense for me to use them in PHP scripts.

  • Yeah, the shortcode that I am using is not going to be available to do the preferred method you recommend. And I don't WANT to use the shortcode in the php file. if there was a way to remove the searchbar that is hardcoded in the php and put the shortcode I have to use in the header, on top of the rotating image, that would work great for me. – damann Nov 5 '15 at 18:28

Use do_shortcode to output a shortcode in your template.

echo do_shortcode( '[theshortcode]' );
  • 1
    Okay, I replaced the entire code I posted with the echo one, doesn't work at all. – damann Nov 5 '15 at 18:08
  • 1
    Some shortcodes don't work outside of the context of a post in the loop. Without knowing exactly what the shortcode does and how, I can't offer a solution. – Milo Nov 5 '15 at 18:10
  • It is a mls shortcode to search for realestate and it rather convoluted, will edit the OP with the shortcode code. – damann Nov 5 '15 at 18:13
  • That's the shortcode, but you would have to look at the function that handles the output of that shortcode to determine why it's not working. It may be easier to ask whomever wrote it for assistance. – Milo Nov 5 '15 at 18:17

You can use this.

<?php echo do_shortcode('[Shortcode_goes_here]') ?>
  • 1
    May you please elaborate you answer? – Ethan Jinks O'Sullivan Sep 29 '16 at 18:27
  • Just use the callback that is assigned for the shortcode. do_shortcode() is … meh. – kaiser Sep 29 '16 at 19:42
  • @EthanJinksO'Sullivan replace the shortcode_goes_here with the shortcode that the plugin gives you and then add it to the php file. – Charles Xavier Oct 3 '16 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.