Any way to insert content from a widget field into a template?

I have a custom widget created where one of the fields is a full telephone number (fulltelephone in the code). It’s use is a call to action widget for the sidebar (e.g. enter the number, some text, a button URL, etc.). Is there any way to grab the content from this field (if it’s populated in the widget) and add it somewhere to a template? I want to add a phone number to the footer but instead of hardcoding it in the footer, I thought it’d be nice if it could just grab the number already entered in the call to action widget. Is this doable? Thanks.

Here’s my widget code:

add_action( 'widgets_init', 'sidebarctawidget' );

function sidebarctawidget() {
    register_widget( 'sidebarctawidget' );
}

class sidebarctawidget extends WP_Widget {

    function sidebarctawidget() {
        $widget_ops = array( 'classname' => 'sidebarctawidget', 'description' => __('A widget for the sidebar call to actions ', 'sidebarctawidget') );

        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'sidebarctawidget' );

        $this->WP_Widget( 'sidebarctawidget', __('Sidebar Call to Action', 'sidebarctawidget'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        //Our variables from the widget settings.
        $title = apply_filters('widget_title', $instance['title'] );
        $text = $instance['text'];
        $prefix = $instance['prefix'];
        $number = $instance['number'];
        $fullnumber = $instance['fullnumber'];
        $url = $instance['url'];

        echo $before_widget;
        echo '<div class="sidebarcta">';

        // Display the title
        if ( $title )
            echo $before_title . $title . $after_title;

        //Display the text
        if ( $text )
            printf( '<p class="text">' . __('%1$s', 'sidebarctawidget') . '</p>', $text );

        //Display the text
        if ( $prefix )
            printf( '<span class="prefix">' . __('%1$s-', 'sidebarctawidget') . '</span>', $prefix );

        //Display the text
        if ( $number )
            printf( '<span class="number">' . __('%1$s', 'sidebarctawidget') . '</span>', $number );

        //Display the text
        if ( $fullnumber )
            printf( '<p class="fullnumber">' . __('(%1$s)', 'sidebarctawidget') . '</p>', $fullnumber );

        //Display the button and URL
        if ( $url )
            printf( '<a class="button" href="%1$s">' . __('Learn More', 'sidebarctawidget') . '</a>', $url );

        echo '</div>';
        echo $after_widget;
    }

    //Update the widget 

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        //Strip tags from title and name to remove HTML
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['text'] = strip_tags( $new_instance['text'] );
        $instance['prefix'] = strip_tags( $new_instance['prefix'] );
        $instance['number'] = strip_tags( $new_instance['number'] );
        $instance['fullnumber'] = strip_tags( $new_instance['fullnumber'] );
        $instance['url'] = strip_tags( $new_instance['url'] );

        return $instance;
    }

    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( 'title' => __('Get Connected TODAY!', 'sidebarctawidget') );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Title:', 'sidebarctawidget'); ?></label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'text' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Text:', 'sidebarctawidget'); ?></label>
            <input id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" value="<?php echo $instance['text']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
        </p>

        <p style="float: left;">
            <label for="<?php echo $this->get_field_id( 'prefix' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Telephone Prefix:', 'sidebarctawidget'); ?></label>
            <input id="<?php echo $this->get_field_id( 'prefix' ); ?>" name="<?php echo $this->get_field_name( 'prefix' ); ?>" value="<?php echo $instance['prefix']; ?>" style="width:50%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px; display: block;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Telephone Number:', 'sidebarctawidget'); ?></label>
            <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" style="width:50%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'fullnumber' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Full Number:', 'sidebarctawidget'); ?></label>
            <input id="<?php echo $this->get_field_id( 'fullnumber' ); ?>" name="<?php echo $this->get_field_name( 'fullnumber' ); ?>" value="<?php echo $instance['fullnumber']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'url' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Button URL:', 'sidebarctawidget'); ?></label>
            <input id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" value="<?php echo $instance['url']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
        </p>

    <?php
    }
}

Related posts

Leave a Reply