• jonahstaehler

    (@jonahstaehler)


    Objective:

    We want to implement a filter for messages sent through the Better Messages plugin in WordPress. The filter should block messages containing specific keywords (e.g., “E-Mail”, “Telefon”, “@”, “außerhalb”), while allowing all other messages to go through without issue. The goal is to prevent users from sharing contact information by blocking messages that contain these restricted terms.

    Challenges Encountered:

    Unfortunately, our current implementation either blocks all messages or allows all messages, without accurately filtering based on the specified keywords. Here is a detailed list of what we’ve tried so far and the issues with each approach:

    Attempts Made

    1. Using the better_messages_message_before_save Hook:

    • Initially, we used the better_messages_message_before_save hook, provided by the Better Messages plugin, with a PHP function that checks for the specified keywords.

    Code Example:

    Code Example:

    function filter_and_block_messages($message_data) {
    $blocked_keywords = array(‚E-Mail‘, ‚Telefon‘, ‚@‘, ‚außerhalb‘);
    if (isset($message_data[‚message‘])) {
    $message_text = $message_data[‚message‘];
    foreach ($blocked_keywords as $keyword) {
    if (stripos($message_text, $keyword) !== false) {
    wp_die(‚Your message contains restricted contact information and has been blocked.‘);
    }
    }
    }
    return $message_data;
    }
    add_filter(‚better_messages_message_before_save‘, ‚filter_and_block_messages‘);

    •	Outcome:
    Unfortunately, this code either blocked all messages (even those without the restricted keywords) or allowed all messages without filtering, depending on slight adjustments we made.
    • Suspected Issue:
    It appears that the hook better_messages_message_before_save does not behave as expected or may not provide access to the message content in a way that allows for accurate keyword checking.

    2. Testing with Alternative Hooks (e.g., wp_insert_post_data):
    • We then attempted to use WordPress’s general post-insertion hook, wp_insert_post_data, to capture messages before they are saved. We adjusted the code to focus specifically on the message post type to avoid interfering with other content.
    • Outcome:
    Messages were not blocked as expected, and the hook did not seem to capture Better Messages content reliably. All messages went through without filtering.
    • Suspected Issue:
    The Better Messages plugin may not use the standard wp_insert_post_data workflow for saving messages, and as a result, this hook did not work.
    3. Client-Side JavaScript Filtering:
    • As an alternative, we tried adding JavaScript to filter messages on the client side before they are sent. This involved detecting the message content in the text area and blocking the send button if restricted keywords were found.
    • Outcome:
    While technically effective in some scenarios, this solution is not reliable. JavaScript-based filtering can easily be bypassed by users with minimal technical knowledge, and it does not provide the security or reliability of a server-side filter.
    • Limitation:
    Client-side filtering is generally not recommended for secure content filtering, as it is more vulnerable to bypass methods and doesn’t guarantee that messages will be blocked once they reach the server.
    4. Debugging the Hook and Reviewing Output Logs:
    • We included debugging statements to verify whether the better_messages_message_before_save hook was even running as expected and to confirm that the message content was accessible at that point.
    • Outcome:
    Debugging outputs showed inconsistent behavior, indicating that the hook might not be designed to handle content filtering in the way we intended, or it might be overridden by the Better Messages plugin’s internal processing.

    Summary of the Issue

    After numerous attempts with different hooks, custom PHP functions, and JavaScript, we are unable to achieve the desired outcome of blocking messages based on specific keywords. Either all messages are blocked, or none are filtered based on the keywords, which suggests that the better_messages_message_before_save hook may not be functioning as expected, or that Better Messages handles message content differently.

    Request for Help

    We are looking for guidance on the following points:

    1. Confirmation of the Appropriate Hook:
    Is better_messages_message_before_save the correct hook for filtering message content before it’s saved? If not, is there an alternative hook or method within Better Messages to achieve this?
    2. Example Code or Best Practice:
    If possible, an example of how to properly intercept and filter message content within Better Messages based on specific keywords would be highly appreciated.
    3. Documentation or Further Support:
    Any additional documentation or references on how Better Messages handles message content storage and filtering would be very helpful.

    We appreciate any insights or suggestions from the community or the developers of Better Messages to help us achieve this functionality.

    Die Seite, für die ich Hilfe brauche: [Anmelden, um den Link zu sehen]

Ansicht von 2 Antworten – 1 bis 2 (von insgesamt 2)
  • You asked your question in English in the German support forums. To keep the threads accessible for users who don’t speak English, please ask your question in German or post it in the international support forums. Thanks for your understanding.

    Moderation Note: sorry, I close this thread as you have asked your question in a German support forum in English. To keep the threads accessible to all people not speaking English, I kindly ask you to either rephrase your question in German or ask in the International support forums. Thank you for your understanding. Best regards, Hans-Gerd

Ansicht von 2 Antworten – 1 bis 2 (von insgesamt 2)
  • Das Thema „Filtering and Blocking Messages with Specific Keywords in Better Messages Plugin“ ist für neue Antworten geschlossen.