Jess McKenzie
Jess McKenzie

Reputation: 8385

Wordpress: Shortcodes

I have the following shortcode. What I would like to do is turn this shortcode into a sidebar area so that I can run a decent contact widget. I have googled and tried a few tutorials but I cannot get it working.

Index.php

<?php
                $classes = '';
                if (strtolower($menuItem->title) == $precision_contact_page) {
            ?>
            <div id="<?php echo strtolower($menuItem->title); ?>scroller">
            <?php
                $page_data = get_page($menuItem->page_id);
                echo do_shortcode($page_data->post_content);
            ?>

Functions.php:

    //contactform shortcode
    function contactform_shortcode( $atts, $content = null ) {
        extract( shortcode_atts( array(
            'class' => 'content'
        ), $atts ) );

        $returncontent = '<div class="' . esc_attr($class) . '">';
        $returncontent .= '<a href="#" id="info" class="open">ContactInformation</a>
                <h5>Get In Touch</h5><div id="contactform">
                <div id="response"></div>
                <form id="precision-contact-form" method="POST" action="" class="form">
                    <div id="main">
                        <p class="name">
                            <input type="text" name="uname" id="uname" />
                            <label for="uname" class="overlabel">Name</label>
                        </p>
                        <p class="email">
                            <input type="text" name="uemail" id="uemail" />
                            <label for="uemail" class="overlabel">E-mail</label>
                        </p>
                        <p class="text">
                            <textarea name="ucomments" id="ucomments" ></textarea>
                        </p>
                        <p class="submit">
                            <button type="submit" name="submit" id="submit" class="graybutton">Send Email</button>
                        </p>
                    </div><!--end main-->
                </form>
            </div><!--end contact form-->';
        $returncontent .= '</div>';
        return $returncontent;
    }
    add_shortcode('contactform', 'contactform_shortcode');
?>

Upvotes: 0

Views: 488

Answers (2)

Rob Holmes
Rob Holmes

Reputation: 396

If you want your shortcode to work in the side bar then you can do the following.

in functions.php add the following

add_filter('widget_text', 'do_shortcode');

This will run all your text widget's content through the *do_shortcode* function.

You can now add a new text widget to your sidebar and use your shortcode inside it, just as if you were in the post edit screen.

[contactform]

Upvotes: 0

Ajay Patel
Ajay Patel

Reputation: 5418

Past this where you want in side bar

 <?php echo do_shortcode('[contactform']'); ?>

Upvotes: 1

Related Questions