phpwordpressreplacecase-sensitive

How to change the string 'events' in events-manager to 'Conversations' using a filter


I would like to change all the occurrences of 'events' from event-manager to 'conversations' using a filter.

For example My events should be My conversations, Events should be Conversation.

Screenshot


Solution

  • In event manager plugin, the language files are located in this path "events-manager\includes\langs".

    https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext

    I used some of the texts which used in event manager plugin.

    Try

    add_filter( 'gettext', 'change_event_manager_text', 20, 3 );
    
    /*
     * Ref: https://stackoverflow.com/questions/44450192/how-to-replace-string-with-another-string-and-keep-case-in-php-and-mysql
     */
    function replaceWithCase($source, $replacement, $string, $pos = 0) {
    
        while (($pos = strpos(strtolower($string), strtolower($source), $pos))!== false) {
            $substr = mb_substr($string, $pos, strlen($source));
            $remaining = mb_substr($string, $pos + strlen($source));
    
            if (ctype_upper($substr)) {
                $string = substr_replace($string,strtoupper($replacement),$pos,strlen($source));
                continue;
            }
    
            $substrParts = preg_split('//u', $substr, null, PREG_SPLIT_NO_EMPTY);
            $replaceParts = preg_split('//u', $replacement, null, PREG_SPLIT_NO_EMPTY);
            $newWord = '';
    
            foreach ($replaceParts as $k => $rp) {
                if (array_key_exists($k,$substrParts))
                    $newWord .= ctype_upper($substrParts[$k]) ? mb_strtoupper($rp) : mb_strtolower($rp);
                else
                    $newWord .= $rp;  
            }
            $string = substr_replace($string,$newWord,$pos,strlen($source));
            $pos = $pos + strlen($source);
        }
    
        return $string;
    }
    
    function change_event_manager_text( $translated_text, $text, $domain ) {
        if( $domain === "events-manager" ) {
            // some translated_text in event manager 
            // see includes\langs\
            $event_manager_texts = array(
                "Event", 
                "View/Edit Event",
                "event",
                "Recurring Event",
                "Edit Event",
                "Add Event",
                "Displaying Event",
                "Published Event",
                "Add New Event",
                "New Event",
                "View Event",
                "Parent Event",
                "Add Recurring Event",
                "Add New Recurring Event",
                "Edit Recurring Event",
                "New Recurring Event",
                "Search Recurring Events",
                "No Recurring Events Found",
                "No Recurring Events Found in Trash",
                "Parent Recurring Event",
                "Duplicate this event",
                "Enable registration for this event",
                "Not a Group Event"
            );
    
            if( in_array( $translated_text , $event_manager_texts) ) {
                return replaceWithCase('event', 'conversation', $translated_text );
            }
        }
        return $translated_text;
    }