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″]

3 Answers
3

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *