如何使用过滤器将事件管理器中的字符串“events”更改为“Conversations”

问题描述 投票:0回答:1

我想使用过滤器将所有出现的“事件”从事件管理器更改为“对话”。

例如“我的事件”应为“我的对话”,“事件”应为“对话”。

Screenshot

php wordpress replace case-sensitive
1个回答
0
投票

在事件管理器插件中,语言文件位于“events-manager\includes\langs”路径中。

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

我使用了事件管理器插件中使用的一些文本。

尝试

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.