4

Is it possible to add content (icon & text) to widget title in admin area and customizer based on other value from the widget?

Widgets are custom and developed by me (not WP default one)

Ideally it should work bot in admin area & customizer. On frontend extra content is now shown.


enter image description here


Then user changes an option in the widget and upon the save title changes to

enter image description here


I've managed to achieve this with JS and .on('widget-updated widget-added') event, but looking for more robust approach as it doesn’t work in customizer.

  • is it a widget that you have developped ? – mmm Dec 6 '17 at 12:05
  • @mmm not sure I understand the question? – Runnick Dec 6 '17 at 12:12
  • you want to custom a existing widget of WordPress or it's a new widget that you have created ? – mmm Dec 6 '17 at 12:28
  • It’s a new widget developed by me. Please notice it can have multiple appearances within one sidebar. – Runnick Dec 6 '17 at 13:46
1

You can add filter for widget_title hook.

Or change the $instance['title'] on update.

  • Can you provide a code example? Second way wouldn't work as it seems to update title both on frontend and backend. – Runnick Dec 11 '17 at 13:24
0

As per my understanding I can suggest you that you can probaly use something like font-awesome. This will help you to display a image sort of thing using simple CSS class.

So you may please check my solution and let me know your views over it.

  • Unfortunately I can’t see the solution? – Runnick Dec 7 '17 at 12:16
0

Let me describe every situation.

The widgets

  1. If you would like to change "Search" on the above screenshot, I should disappoint you, you can look in /wp-admin/includes/widgets.php (line 232) and in any widget class (I opened /wp-includes/widgets/class-wp-widget-search.php) and you will find out that there are no filter hooks for it. It could be changed only in your custom widgets.

On my custom widget I change it in this part of the widget class code:

    function __construct() {
         parent::__construct(
            'misha_widget', 
            'Widget title', // here it is
            array( 'description' => 'Widget description' )
        );
    }
  1. If you would like to change "My search" on widget in admin area, JavaScript is good enough way to do it because WordPress also do it with JS. The widget_title hook will be applied only for website appearance not for admin area or customizer:

    add_filter('widget_title', 'misha_change_title1', 10, 3 );
    function misha_change_title1( $title, $instance, $id ){
        if( $id == 'search' ) {
            return '📷'. $title;
        }
        return $title;
    }
    

What about customizer, you can find the instruction how to run custom JS code here https://codex.wordpress.org/Theme_Customization_API#Step_2:_Create_a_JavaScript_File

  • In the case of 1, how would you change it on your own custom widgets? – Runnick Dec 11 '17 at 13:31
  • I've updated the answer – Misha Rudrastyh Dec 11 '17 at 14:27
  • Please see the updated question and sorry if I wasn’t clear. Constructor seems to change the first part of the title and you can’t put anything dynamic in there. – Runnick Dec 11 '17 at 15:24
  • My opinion is simple enough - if WordPress adds the widget title near widget name with JavaScript - the best way is using JavaScript. I could help you with the code but you said you've alreade figured it out :) – Misha Rudrastyh Dec 11 '17 at 15:55
  • Yes, my main concern is it doesn’t work in customizer so I was hoping for some native approach that will handle both admin & customizer within one code piece. If you could help with integrating it in the customizer though, it would be great! – Runnick Dec 11 '17 at 16:10

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.