• Hi! I have a plugin which highlights unread posts on the blog by adding a bit of markup to their titles. It uses the the_title filter and only works when in_the_loop() returns true.

    I got a request to make the highlight feature also work in the Recent Posts widget. My question is: how can I determine that this particular widget is being shown at this very moment? Or is there any other trick I can use to “hijack” that specific widget?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Are you talking about the Recent Posts widget or do you mean the Recent Posts block?

    What does the developer of this plugin say about your request?

    Thread Starter i.lychkov

    (@ilychkov)

    Oh, I’m the developer of the plugin, haha. And I’m just looking for a way to implement that. And I mean the widget, yes.

    Really, what I need is a function analogous to in_the_loop() that would instead detect whether a certain widget is being processed right now (or at least maybe just the sidebar in general).

    Then take a look at the class that this widget represents:

    https://github.com/WordPress/WordPress/blob/master/wp-includes/widgets/class-wp-widget-recent-posts.php

    From line 111 you can see the loop that is used here. I can’t see any way of adapting the source code in the output via PHP in your sense.

    I see only 2 possibilities for your case:
    a) You parse the generated HTML code before outputting the entire page and adapt the output of this widget via regex. However, this can be very time-consuming and potentially dangerous.
    b) You adapt the output via JavaScript in the frontend. This should be easier to do.

    Thread Starter i.lychkov

    (@ilychkov)

    Well, sorry to say, but the (a) option definitely is scary, and (b) doesn’t seem very elegant.

    I thought of using the the_widget hook and setting a variable in my plugin to true when the “Recent posts” widget starts to render – and then I would just have to check the value of that variable in my code. But the problem is that I see no way of setting that variable back to false once the widget is done rendering.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Modify titles only for posts shown in the Recent Posts widget’ is closed to new replies.