Wordpress Widget - Adding URL to title

Posted by Nick Canarelli on Stack Overflow See other posts from Stack Overflow or by Nick Canarelli
Published on 2012-10-02T02:40:19Z Indexed on 2012/10/04 21:38 UTC
Read the original article Hit count: 840

Filed under:
|
|

I can't seem to figure out how to wrap the title of the widget in an tag. For example, I am trying to get it so that when you type the url in a text field, it is then placed in the tag so that it is a hyperlink on the website...

    class Example_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function Example_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays company announcements.', 'example') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );

        /* Create the widget. */
        $this->WP_Widget( 'example-widget', __('Announcement Widget', 'example'), $widget_ops, $control_ops );
    }

    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );

        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', $instance['title'] );
        $excerpt = $instance['excerpt'];
        $url = $instance['url'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Display the widget title if one was input (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title;

        /* Display name from widget settings if one was input. */
        if ( $excerpt )
            printf( '<p style="font-family: arial; font-size: 12px; line-height: 16px;">' . __('%1$s.', 'example') . '</p>', $excerpt );

        /* After widget (defined by themes). */
        echo $after_widget;
    }

    /**
     * Update the widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        /* Strip tags for title and name to remove HTML (important for text inputs). */
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['excerpt'] = strip_tags( $new_instance['excerpt'] );

        return $instance;
    }

    /**
     * Displays the widget settings controls on the widget panel.
     * Make use of the get_field_id() and get_field_name() function
     * when creating your form elements. This handles the confusing stuff.
     */
    function form( $instance ) {

        /* Set up some default widget settings. */
        $defaults = array( 'title' => __('Title Goes Here', 'example'), 'excerpt' => __('Excerpt goes here.'), );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></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:100%;" />
        </p>

        <!-- Your Name: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'excerpt' ); ?>"><?php _e('Excerpt:', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'excerpt' ); ?>" name="<?php echo $this->get_field_name( 'excerpt' ); ?>" value="<?php echo $instance['excerpt']; ?>" style="width:100%;" />
        </p>
    <?php
    }
}
?>

And here is the functions file code

    register_sidebar(array(
  'name' => __( 'Announcements' ),
  'description' => __( 'Display company announcements here.' ),
  'before_widget' => '',
  'after_widget' => '<hr style="margin-top: 4px; color: #f00; background-color: #585040; height: 1px; border: none; margin-bottom: 2px;"/>',
  'before_title' => '<h2 style="font-size: 12px;">',
  'after_title' => '</h2>'
));

© Stack Overflow or respective owner

Related posts about php

Related posts about Wordpress