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"); ?>
</div>
<?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"]
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"); ?>
</div>
<?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"]
Share Improve this question edited Feb 2, 2020 at 3:25 Peter Mortensen 2682 silver badges10 bronze badges asked Nov 5, 2015 at 17:46 damanndamann 111 gold badge1 silver badge3 bronze badges 6- Make a child theme and change the specific file. Using the WordPress Codex and Google will take you through it. – flomei Commented Nov 5, 2015 at 17:50
- I am using a child theme, but I am lost as to how to make it work out. – damann Commented Nov 5, 2015 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 Commented Nov 5, 2015 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 Commented Nov 5, 2015 at 17:58
- What Milo said... – flomei Commented Nov 5, 2015 at 18:06
3 Answers
Reset to default 5In 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.
Use do_shortcode
to output a shortcode in your template.
echo do_shortcode( '[theshortcode]' );
You can use this.
<?php echo do_shortcode('[Shortcode_goes_here]') ?>