• Resolved islp

    (@islp)


    I want to replace the title of a widget anytime my widget title text starts with ‘(new)’.

    I tried the following in functions.php:

    add_filter('widget_title', 'my_filter_widget_title', 10, 3);
    
    function my_filter_widget_title($title, $instance, $wid){
        if(substr($title, 0, 5) == '(new)'){
            return $title = str_replace('(new)', "(old)", $title);
        }
    }

    but this thing completely removed my sidebar (it wasn’t visible anymore).

    Is it possible to do what I want to do? What’s the right way to do it?

    Thanks in advance

Viewing 7 replies - 1 through 7 (of 7 total)
  • If this function you have included removed your sidebar, then something wasn’t implemented properly. In and of itself this function will not remove your sidebar.

    If you can’t run a search and replace on the database for whatever reason, then you can modify content strings in real-time using the str_replace function:
    https://www.php.net/manual/en/function.str-replace.php

    like this:
    https://wp-mix.com/replace-all-instances-string-wordpress/

    Thread Starter islp

    (@islp)

    Hi @corrinarusso, thanks.

    I don’t want to replace any string anywhere (besides being uselessly intensive, I only have to edit four titles. Another problem would be the fact that the same string could be inserted casually in a place where it shouldn’t be replaced).

    And I don’t want to do this directly in the database, simply because this isn’t a definitive edit: my admin needs to insert a sort of shortcode (eg. ‘(new)’ and others) and this string must be replaced on every manual edit by another text.

    • This reply was modified 2 years, 10 months ago by islp.

    Sorry – I don’t understand what you are trying to accomplish.

    Thread Starter islp

    (@islp)

    @corrinarusso:

    well, it’s easy.

    First of all, we don’t use the new Gutenberg widgets.

    The old widgets featured a title, eg. the default HTML widget could have the title “Today it’s tuesday, let’s make trippa”.

    This title is printed at the top of the widget in the home page.

    Every WP website has at least an admin: my admin must manually edit the title of this widget everytime he wants.

    So my admin goes to the Appearence area and finds the HTML widget dragged in the sidebar.

    He finds the old title:

    -> “Today it’s friday, fish&chips for everybody”.

    He wants to change the title this way:

    -> “(new) Today it’s tuesday, let’s make trippa”.

    Then, save.

    When WP finds this ‘(new)’ has to replace it on the fly with another string.

    Is it clearer? ??

    • This reply was modified 2 years, 10 months ago by islp.
    Thread Starter islp

    (@islp)

    @corrinarusso : ok, I believe I found what it was by simply activating the debug.log:

    fatal error thrown in /home/justeatable/www/…/functions.php on line 12
    [08-Jan-2022 14:52:39 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function islp_filter_widget_title(), 1 passed / exactly 3 expected

    Try replacing your single quotes here around new with double quotes:
    return $title = str_replace(‘(new)’, “(old)”, $title);
    If that doesn’t work, try vice versa.

    Thread Starter islp

    (@islp)

    @corrinarusso : solved. Since I had changed my mind about the signature of the function many times, I forgot to remove some unneeded arguments. Thanks anyway. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Replace the title of widget’ is closed to new replies.