• Wanting to revise the elements within a widget, but do not want to continually update the theme elements.

    How do I separate the widget update (e.g. WP_Calendar_Widget in default-widgets.php) so I can change the elements of it – without having to modify the theme or the core codebase?

    Thanks in advance.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Copy the class and functions, give the class another name, then use the newly modified version..

    Eg.

    // Example - trimmed
    
    class WP_Widget_Calendar_Custom extends WP_Widget {
    
    	function WP_Widget_Calendar() {

    You can then change what you want…

    Thread Starter sdickert

    (@sdickert)

    Mark – thanks for the feedback. My other question is where do I place the code so when the theme updates, it will not get rewritten?

    Thanks.

    Place the code into a plugin? A standalone php file will work fine..

    Only need contain the new class with your adjustments, just ensure you give the php file a plugin header… like so..

    <?php
    /*
    Plugin Name: Some-unique-plugin-name
    */

    Thread Starter sdickert

    (@sdickert)

    Mark – thanks again. I got the plugin – enabled it – and am trying to figure out how to override the original widgets versus keeping the ones in the default.

    I went to the widgets interface on the Admin side and now I see a slew of inactive widgets and I do not see my new ones. Have I forgotten something?

    Thread Starter sdickert

    (@sdickert)

    I have looked into adding the widgets – like this:

    // register widgets
    add_action(‘widgets_init’, create_function(”, ‘return register_widget(“WP_PoliGastro_Widget_Calendar”);’));

    and I get this error from the plugin (which is calling a problem in the codebase):

    Warning: Missing argument 2 for WP_Widget::__construct(), called in /home/content/s/d/i/sdickert/html/phs80s/wp-includes/widgets.php on line 324 and defined in /home/content/s/d/i/sdickert/html/phs80s/wp-includes/widgets.php on line 93

    Thread Starter sdickert

    (@sdickert)

    Okay – last effort – I have tried coding this – and can not find a simple way to register the modified Calendar class. See the code below:

    <?php
    /**
     * @package 2010 Mods
     * @version 1.0
     */
    /*
    Plugin Name: 2010 Modifications
    Plugin URI: https://sanforddickert.com/#
    Description: This makes modifications to the standard twentyten theme to enable Political Gastronomica
    Author: Sanford Dickert
    Version: 1.0
    Author URI: https://sanforddickert.com/
    */
    
    /**
     * PG Calendar widget class
     *
     * @since 2.8.0
     */
    class WP_PoliGastro_Widget_Calendar extends WP_Widget {
    
    	function WP_PoliGastro_Widget_Calendar() {
    		$widget_ops = array('classname' => 'widget_calendar', 'description' => __( 'A PoliGastro calendar of your site’s posts') );
    		$this->WP_Widget('calendar', __('Calendar'), $widget_ops);
    	}
    
    	function widget( $args, $instance ) {
    		extract($args);
    		$title = apply_filters('widget_title', empty($instance['title']) ? '&nbsp;' : $instance['title'], $instance, $this->id_base);
    		echo $before_widget;
    		if ( $title )
    			echo $before_title . $title . $after_title;
    		echo '<div id="calendar_wrap2">';
    		get_calendar();
    		echo '</div>';
    		echo $after_widget;
    	}
    
    	function update( $new_instance, $old_instance ) {
    		$instance = $old_instance;
    		$instance['title'] = strip_tags($new_instance['title']);
    
    		return $instance;
    	}
    
    	function form( $instance ) {
    		$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    		$title = strip_tags($instance['title']);
    ?>
    		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
    <?php
    	}
    }
    
    /**
     * Register all of the default WordPress widgets on startup.
     *
     * Calls 'widgets_init' action after all of the WordPress widgets have been
     * registered.
     *
     * @since 2.2.0
     */
    
    // register widgets
    //register_widget('WP_PoliGastro_Widget_Calendar');
    
    ?>

    Thread Starter sdickert

    (@sdickert)

    Got it.

    Sorry i didn’t reply sooner, not been at my PC… did you manage to get the code working ok?

    Should be a simple case of copying code and renaming a few areas, but let me know if you still have problems… ??

    Thread Starter sdickert

    (@sdickert)

    I found the changes – needed to modify some of the parameters and then add_action efforts. I grabbed another plug-in and checked how they did it. No one has rewritten any of the default widgets (e.g. Calendar, Recent Posts) in a plug in – I was wondering if there was a way instead of creating two new widgets.

    Any thoughts?

    Thread Starter sdickert

    (@sdickert)

    BTW – for future readers – here is the code snippets I made:

    <?php
    /**
     * @package 2010 Mods
     * @version 1.0
     */
    /*
    Plugin Name: 2010 Modifications
    Plugin URI: https://sanforddickert.com/#
    Description: This makes modifications to the standard twentyten theme to enable Political Gastronomica
    Author: Sanford Dickert
    Version: 1.0
    Author URI: https://sanforddickert.com/
    */
    
    /**
     * PG Calendar widget class
     *
     * @since 2.8.0
     */
    class WP_PoliGastro_Widget_Calendar extends WP_Widget {
    
    	function WP_PoliGastro_Widget_Calendar() {
    		$widget_ops = array('classname' => 'widget_calendar', 'description' => __( 'A PoliGastro calendar of your site’s posts') );
    		$this->WP_Widget('pgcalendar', __('PGCalendar'), $widget_ops);
    	}
    
    	function widget( $args, $instance ) {
    		extract($args);
    		$title = apply_filters('widget_title', empty($instance['title']) ? '&nbsp;' : $instance['title'], $instance, $this->id_base);
    		echo $before_widget;
    		if ( $title )

    and then I kept the original code from the default-widgets.php

    and at the end of the file, I included:

    add_action('widgets_init', create_function('', 'return register_widget("WP_PoliGastro_Widget_Calendar");'));

    it’s the widget name in the constructor that needs changed in order to add options to one of the default widgets.

    $this->WP_Widget('widget-name' ...

    i don’t completely understand why. have you figured this out? changing this widget name in my plugin and pushing that out as an update will break the widget when the update is downloaded. this makes sense, because it technically becomes a different widget. i’d like to know why i can’t add options to one of the default widgets. my solution right now is having two plugins, one that modifies the built-in default widget, and another that duplicates it with a bit more functionality.

    thanks for your post.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Modifying widget functions within a theme’ is closed to new replies.