0

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?

1

Check this tutorial it might be help you https://www.tipsandtricks-hq.com/how-to-add-widgets-to-wordpress-themes-footer-1033

1

Add this code in widget action hook of your theme function.php

<?php
register_sidebar( array(
    'name' => 'Custom Footer',
    'id' => 'custom-footer',
    'description' => 'Appears in the footer area',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => '</aside>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
    ) );
?>

For Get in your theme footer widget use this code:

<?php
    if(is_active_sidebar('custom-footer'))
    {
        dynamic_sidebar('custom-footer');
    }
?>
  • thanks Arpan, its working, how can I create as a custom input text on this custom widget in wp-admin to display the text in the widget view? – Wagner Moreira Jul 5 '17 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.