I've managed to sync the comments count on Site 2 in Wordpress multisite. If someone needs it, the code is below. Just make sure to set starting increment point of post ID's on another site to some high number. I've set it to 5 million, to avoid having duplicate post IDs on both sites.

It creates a custom REST API endpoint to intercept post updates coming from this extension. Then checks if the post ID exists on the main site. If don't, it means that the discussion is belonging to another site. Then proceeds to the posts table on the other site and updates it there.

Paste the code in your functions.php file and be sure to change "POST TABLE NAME ON SITE 2" to your own name (with prefix)

function custom_update_post(WP_REST_Request $request) {
    $post_id = $request['id'];
    $comment_count = $request['comment_count'];

    if (get_post($post_id) !== null) {
        // Update the existing post with the new comment count
        wp_update_post(array(
            'ID' => $post_id,
            'comment_count' => $comment_count
        ));
    } else {
        // Update the second site on multisite table with the new comment count
        global $wpdb;
        $table_name = 'POST TABLE NAME ON SITE 2';

        $existing_record = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE ID = %d", $post_id));

        if ($existing_record) {
            $wpdb->update(
                $table_name,
                array('comment_count' => $comment_count),
                array('ID' => $post_id),
                array('%d'),
                array('%d')
            );
        } else {
            $wpdb->insert(
                $table_name,
                array(
                    'ID' => $post_id,
                    'comment_count' => $comment_count
                ),
                array('%d', '%d')
            );
        }
    }

    return new WP_REST_Response("Comment count updated successfully", 200);
}

function custom_update_post_register_route() {
    register_rest_route('wp/v2', '/posts/(?P<id>\d+)', array(
        'methods' => 'POST',
        'callback' => 'custom_update_post',
        'permission_callback' => function () {
            return current_user_can('edit_posts');
        },
        'args' => array(
            'id' => array(
                'validate_callback' => function($param, $request, $key) {
                    return is_numeric($param);
                }
            ),
            'comment_count' => array(
                'required' => true,
                'validate_callback' => function($param, $request, $key) {
                    return is_numeric($param);
                }
            ),
        ),
    ));
}

add_action('rest_api_init', 'custom_update_post_register_route');
2 months later

Hello, I just installed the Flarum extension and the WordPress plugin. I configured all the things like the documentation says. But Flarum keeps giving me this error:

We could not authenticate with the given credentials. Double-check the URL, username, and password. Check the documentation for more troubleshooting steps.

When I run the health check in WordPress Tab (before and after typing my WordPress details), it says:

  • The "WordPress admin username" setting must be filled
  • The "WordPress URL" setting must be filled

I'm following the documentation to know more about this error. But I'm not understanding what I am doing wrong. My WordPress Health Check Tool is not giving me any API errors and the Flarum API Key is correctly pasted into my WordPress settings.

I have WordPress in the main directory and Flarum in /forum and my Flarum logs don't say anything.

My Flarum info:

Flarum core: 1.8.1
PHP version: 8.1.20
MySQL version: 10.5.19-MariaDB-0+deb11u2
Loaded extensions: Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, gmp, iconv, igbinary, imagick, imap, intl, ldap, exif, mysqli, pdo_mysql, Phar, posix, readline, redis, shmop, SimpleXML, sockets, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, Zend OPcache
+----------------------------------+---------+--------+
| Flarum Extensions | | |
+----------------------------------+---------+--------+
| ID | Version | Commit |
+----------------------------------+---------+--------+
| flarum-flags | v1.8.0 | |
| flarum-approval | v1.8.0 | |
| flarum-subscriptions | v1.8.0 | |
| flarum-tags | v1.8.0 | |
| flarum-suspend | v1.8.0 | |
| flarum-lock | v1.8.0 | |
| afrux-forum-widgets-core | v0.1.7 | |
| flarum-nicknames | v1.8.0 | |
| fof-follow-tags | 1.1.7 | |
| v17development-seo | v1.8.0 | |
| tohsakarat-fancybox-plus | 1.0.21 | |
| sycho-profile-cover | v1.3.5 | |
| nearata-twofactor | v2.2.0 | |
| nearata-sensitive-content | 1.1.0 | |
| matteociaroni-public-suspensions | v1.0.0 | |
| malago-ads | 0.3.2 | |
| kilowhat-wordpress | 1.8.1 | |
| justoverclock-welcomebox | 2.0.2 | |
| fof-username-request | 1.1.2 | |
| fof-user-directory | 1.2.3 | |
| fof-user-bio | 1.1.1 | |
| fof-upload | 1.2.3 | |
| fof-socialprofile | 1.1.5 | |
| fof-sitemap | 2.1.0 | |
| fof-share-social | 1.1.2 | |
| fof-profile-image-crop | 1.1.1 | |
| fof-prevent-necrobumping | 1.1.1 | |
| fof-polls | 1.4.0 | |
| fof-oauth | 1.3.2 | |
| fof-nightmode | 1.5.2 | |
| fof-mason | 1.2.0 | |
| fof-links | 1.1.4 | |
| fof-linguist | 1.1.1 | |
| fof-ignore-users | 1.2.0 | |
| fof-geoip | 1.1.0 | |
| fof-disposable-emails | 1.0.0 | |
| fof-best-answer | 1.2.5 | |
| fof-bbcode-details | 1.1.1 | |
| fof-analytics | 1.1.0 | |
| flarum-sticky | v1.8.0 | |
| flarum-statistics | v1.8.0 | |
| flarum-pusher | v1.8.0 | |
| flarum-mentions | v1.8.0 | |
| flarum-markdown | v1.8.0 | |
| flarum-likes | v1.8.0 | |
| flarum-lang-italian | 1.11.0 | |
| flarum-emoji | v1.8.0 | |
| flarum-bbcode | v1.8.0 | |
| datlechin-link-preview | v1.4.0 | |
| clarkwinkelmann-circle-groups | 1.0.1 | |
| afrux-news-widget | v0.1.1 | |
| acpl-mobile-tab | 1.1.1 | |
+----------------------------------+---------+--------+
Base URL: https://mydomain.eu/forum
Installation path: /var/www/html/mydomain.eu/forum
Queue driver: sync
Session driver: file
Scheduler status: Inattivo
Mail driver: smtp
Debug mode: off!

    feds you must either connect the 2 websites using the wizard, or connect them manually by filling the URLs and API key. If you already entered settings on the WordPress side manually, the installation wizard will no longer work. This is for security reasons, so the setup API endpoint on WordPress cannot be abused once the connection has been made.

    Try removing all setting values on the WordPress side under the Flarum integration settings and try to use the Wizard on the Flarum side again. Alternatively you can fill in the remaining blank fields on both sides to manually establish the connection.

    If the Wizard still doesn't work with all settings left empty on the WordPress side, can you share the list of WordPress plugins you are using? In particular if there are any authentication, rate limiter or spam detection plugins. You can send the list to me privately using the email listed on the documentation page if you prefer.

    The health check in the Flarum extension will only provide useful information once the 2 websites have been successfully connected. Its main role is to verify nothing was accidentally unlinked or broken in the future.

    I just connected it manually and it worked, but the Health Check keeps me to give this error:

    The "WordPress URL" setting in Flarum does not match the "WordPress Address (URL)" setting from WordPress (expected: "", actual: "https://mydomain.eu")

    What does "WordPress Address (URL)" mean? I wrote my correct WordPress URL in the WordPress URL field in Flarum.

      feds do you have a multisite WordPress install?

      If I remember correctly, the "expected" value is the URL configured in WordPress wp-config.php that Flarum reads through the WordPress REST API, while "actual" is the copy-paste of the value currently stored in Flarum.

      If you have a multi-site, it should still work in most part but the wizard and health check won't. This is why I listed multi-site installations as unsupported for now. But I have customers who successfully run the extension in multi-site. I am hoping to add full support in the future.

      If you don't have multi-site, maybe there's a WP plugin interfering with the URL returned by the WordPress REST API. If everything else is correct, you can ignore that warning. If you find which plugin or WordPress settings causes the "expected" URL to become an empty value, please let me know so I can investigate!

      Yes, I have a WordPress Multisite. But on the main site, I use the Broadcast plugin to broadcast all posts to the main site. So, I activated the plugin only on the main site.

      EDIT: I cleared Flarum and WordPress cache, and now it works successfully also on the other sites of my Network without any problem. Thank you very much 😉

      2 months later

      @clarkwinkelmann

      Is it possible to use this plugin without linking flarum accounts to wordpress accounts, that is, so that only flarum accounts are required and WP accounts in general are not used (and not required) or created?

      I only need the WP site to show comments from flarum, and for a created new post on WP to create a new thread on the flarum forum. Is it possible to do that?

        jaclas yes, absolutely!

        In the extension there are 2 "features" that can be independently toggled. If you only enable the Comments Integration, the extension will create the comment discussions and add the iframe to WordPress. When users attempt to comment, it will only prompt them to login or create an account within Flarum directly. You can use Flarum social login extensions to add more login options within Flarum.

          clarkwinkelmann

          Thanks, one more question.
          Is it possible that after a new post there would be a forum thread created, but below that post on WP there would only be a link to that forum thread, but without the comment content (not an iframe just an <a> tag)? This link would be with a description such as "Click here to discuss". And clicking would redirect (open a second browser tab) to the forum thread, where there would be a full discussion?

            jaclas that's not a feature at this time, but I could implement it.

            You could also change this locally by overriding the comments template in a theme or directly in my plugin. Only issue is that I don't think there's a way to build a permalink to the non-iframe page with the information available in WordPress. I would have to add a new endpoint to Flarum as well.

            11 days later

            Is it possible to prevent a user creation, so that only wordpress users can access the forum?

              Version 1.8.2 - September 16, 2023

              • Fix admin page breaking when other extensions try to customize the style of the header.

              This fixes the incompatibility with Asirem Theme.

              2 months later

              Hello @clarkwinkelmann

              First af all, great plugin! I saw your demo video and I was sold so I bought the subscription of your plugin :-D .

              I did not manage to get it working yet, though. I get the error ERR_TOO_MANY_REDIRECTS when trying to login (Wordpress pop up). Removing cookies did not help. I think I have set up everything correctly, but still there must be something wrong. Maybe with Nginx, I don't know.

              I have checked the Networks tab in Chrome Dev, it keeps redirecting from:

              https://novacustom.com/forum/auth/wordpress?continue=1&token= + token

              To:

              https://novacustom.com/newlogin?redirect_to=https%3A%2F%2Fnovacustom.com%2Fforum%2Fauth%2Fwordpress

              What can I check to solve this? I use HidemyWPGhost to keep my website secure. I'm afraid of disabling the whole plugin ... Is this the problem indeed? Or is there anything else I can try?

              I'm looking forward to your reply, many thanks in advance!

                wessel-novacustom

                I have managed to fix this by myself by making this change in my Nginx configuration!

                I changed this line:
                try_files $uri /forum/index.php$query_string
                To:
                try_files $uri /forum/index.php?$query_string

                  wessel-novacustom glad you found a solution.

                  It had to be related to the rewrites/try rules of WP and Flarum which are nested, you need to make sure everything under /forum/ is handled by Flarum's index.php

                  a month later

                  Version 1.8.3 - January 5, 2024

                  • Fix open redirect vulnerability in logout script

                  Only the Flarum extension needs updating. The WordPress plugin has not received any change.

                  Forum data and credentials were never at risk, this was just a bad practice that could potentially harm the forum domain reputation if used to facilitate spam. It could also potentially be abused in a phishing attack against the users of the forum to make a malicious link look more legitimate.

                  The same vulnerability existed in Flarum core and was fixed in Flarum 1.8.5. This extension requires a patch because it uses a modified version of Flarum's logout controller that also contained the vulnerability. You don't need to add the WordPress domain name to the new redirectDomains config key, as the global login and logout feature doesn't use that Flarum mechanism.

                  2 months later

                  Thanks for this really great plugin. Installing it with the documentation was pretty straight forward. But maybe I did somewhere something wrong.

                  Login from wordpress / comment section

                  On the wordpress site: When I want to comment the blog acrticle I cant login or register. Error message says something like

                  Oops! Something went wrong there. Please reload the page or try again.

                  Login or register for a new account on the flarum site still works fine.

                  Picture resizing

                  Some pictures on the flarum site in the content excerpt are looking wired. The width of the picure is changed to fit in the frame, but the height remains. Like this:

                  I tried to geht rid of th e picture with whitelist of HTML tags with p,ul but that seems not to work with my blog.

                  Previous blog postings

                  All the previous postings on the blog site have instead the comments frame this message:

                  This post doesn't have a comment thread yet. If you are the blog administrator, publish the post, or disable and re-enable comments to start a thread.

                  I can use “Bulk Edit” on the posting site to turn of the comments an on again. But there about 3000 postings. Is there any easier, smarter way to that?

                  Where to change words

                  Where can I change some words like on the flarum site in the excerpt WorpdPress Post. Also I want to change the read on blog and Read more button into my language.

                  On the Wordpress Site I want to change the word comments in to the site language as well.

                    Flab sorry if I skip over some of your questions, I'll reply to the most pressing points and read again your post later to make sure I didn't forget anything.

                    For the "Something went wrong", there should be more info in the Flarum or webserver log file, see https://docs.flarum.org/troubleshoot for how to find it.

                    For the picture size, I assume it's the post thumbnail rather than an image in the post content. I don't remember if my extension has a setting to toggle its visibility, probably not. But I could add a setting for that. The post preview on the Flarum side is displayed with some very simple CSS, which you should be able to override from the forum custom CSS if you wish. I don't remember seeing that issue with the image ratio but I'll take a look at whether I can fix it. It could also be caused by the CSS from other Flarum extension.

                    There is currently no way to automatically create comment threads for existing WP posts. This is a feature I planned to add but have not completed yet. The only solution at the moment is to manually toggle comments off and on again on each WP post. If you need this feature I can move it up my TODO list and work on it in the coming weeks.

                    To change texts on the Flarum side, you can use the Linguist extension https://discuss.flarum.org/d/7026-linguist-customize-translations-with-ease I think all texts should be translatable but if you can't find one let me know and I'll look into it.

                    On the WP side (outside of the iframe), I don't think my extension provides any text at all (except the admin panel setting names). The comment count is re-using the translation from the WP built-in themes, so it should already be translated by WP language packs. Texts inside the iframe are managed in Flarum.

                      clarkwinkelmann

                      Login from wordpress / comment section

                      Content of the storage/log
                      https://pastes.io/a6fg6fxaze

                      Picture resizing

                      In the admin panel both options Show WordPress post thumbnail above excerpt and Show WordPress post thumbnail in Flarum discussion list are switched off.
                      The image is in the begining of the post content, not the post thumbnail.
                      Unfortunately I dont know how to write anything in CSS.

                      Previous blog postings

                      If you need this feature I can move it up my TODO list and work on it in the coming weeks.

                      That would be a really nice to have feature and will save a lots of clicks.

                      Where to change words ✅

                      To change texts on the Flarum side, you can use the Linguist extension https://discuss.flarum.org/d/7026-linguist-customize-translations-with-ease I think all texts should be translatable but if you can't find one let me know and I'll look into it.

                      I could find everything with the linguist extension.