• I needed to append a “More news…” link to the end of the Recent News list in a website sidebar. I found the code involved was not in a custom theme widget, but in a WordPress-native widget, class-wp-widget-recent-posts.php.

    I easily altered this widget and make it work the way I needed, but I suppose this was not very hortodox, since the next update would delete my “patch”.

    What’s the right way to modify the behavior of this widget?

    I found there’s a widget_posts_args filter, but I don’t understand how to use it to append a final link to my list.

    • This topic was modified 4 years, 6 months ago by islp.
    • This topic was modified 4 years, 6 months ago by islp.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi.
    You can copy whole the widget class to your functions.php, rename and register it.
    (not tested)

    1. Change class name to like class My_WP_Widget_Recent_Posts extends WP_Widget.

    2. Change the widget name to parent::__construct( 'my-recent-posts', __( 'My Recent Posts' ), $widget_ops );.

    3. Register the widget.

    function register_my_widget() {
        register_widget( 'My_WP_Widget_Recent_Posts' );
    }
    add_action( 'widgets_init', 'register_my_widget' );

    https://codex.www.ads-software.com/Widgets_API

    Thread Starter islp

    (@islp)

    It doesn’t work

    Umm. I have tested with TwentyTwenty theme, and it worked as expected.
    Is not there ‘My Recent Posts’ widget in Widgets page?

    Thread Starter islp

    (@islp)

    Ok, since I believed I was overriding the behavior of the native widget (NOT replacing it), I didn’t drag the widget into the widgets panel. It works, but I have one more question: I see the theme has an area dedicated to custom widgets. Would it be possible to move the entire code there?

    Good!
    I suppose it is OK to move code to the custom widgets area.
    It just add an extra widget, instead of replacing/overwriting the native widget, so it would be the same as adding custom one.

    Thread Starter islp

    (@islp)

    This time it doesn’t work and it doesn’t get listed in the widgets area. Maybe the theme references its widgets somewhere else in the code. Anyway, thanks. ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Modify the behavior of a native widget’ is closed to new replies.