I'm searching a lot about custom widgets, it seems that it can be displayed just in the sidebar, but I want to display it in my theme's footer It's just a copyright custom text that I want to add into a widget to easily change it in WP admin, I was following this tutorial: http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/
I have the following structure:
theme-folder
----includes
--------widgets
------------copyright.php
//functions.php
include_once( 'includes/widgets/copyright.php' );
if (function_exists("register_sidebar")) {
register_sidebar();
}
copyright.php file:
<?php
function register_my_widget() {// function to register my widget
register_widget( 'My_Widget' );
}
function My_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays the authors name ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
add_action( 'widgets_init', 'register_my_widget' );// function to load my widget
class my_widget extends WP_Widget {// The example widget class
// constructor
function __construct() {
parent::__construct(
false,
__( 'My Widget', 'my-widget' ),
array( 'description' => __( 'My sample widget', 'my-widget' ) ) );
}
}
function widget( $args, $instance ) {// display the widget
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$name = $instance['name'];
$show_info = isset( $instance['show_info'] ) ? $instance['show_info'] : false;
echo $before_widget;
// Display the widget title
if ( $title )
echo $before_title . $title . $after_title;
//Display the name
if ( $name )
printf( '<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $name );
if ( $show_info )
printf( $name );
echo $after_widget;
}
function update( $new_instance, $old_instance ) {// update the widget
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['name'] = strip_tags( $new_instance['name'] );
$instance['show_info'] = $new_instance['show_info'];
return $instance;
}
function form() {// and of course the form for the widget options
//Set up some default widget settings.
$defaults = array( 'title' => __('Example', 'example'), 'name' => __('Bilal Shaheen', 'example'), 'show_info' => true );
$instance = wp_parse_args( (array) $instance, $defaults );
//Text Input
// <p>
// <label for="echo $this->get_field_id( 'name' );"><?php _e('Your Name:', 'example');</label>
// <input id="echo $this->get_field_id( 'name' );" name="echo $this->get_field_name( 'name' );" value="echo $instance['name'];" style="width:100%;" />
// </p>
}
?>
I needed to comment the html because it's not being recognized, and I don't know how to call the widget inside my footer.php, anyone have tried to do the same before?