Skip to main content

Messages to Post navigation & My Spaces URL for primary menu

  1. Is there an easy way for users to go from Posts to Messages and back to posts?

  2. Is there a way to put My Spaces at the top primary menu? When I include the URL from the profile dropdown, it doesn't work for the primary menu: #{{user_url}}/spaces

Thanks!

Jeff Brigman

They don’t support dynamic url tokens. However it’s possible via code snippet.

Patricia Montagno

Jeff BrigmanΒ Thank you!

Jeff Brigman

Patricia MontagnoΒ I have a code snippet working for this (#2) and will share it soon.

Patricia Montagno

Amazing! Thank you! Jesse BenjaminΒ 

Jeff Brigman

Patricia Montagno this is a quick fix/solution, I will try to code something better later and may try to expand it further. Β 

create the menu link, set visibility to "Logged in Only". Then in the url add only this:

{user_url}/spaces

Add this code snippet using whatever code snippet plugin you prefer.

<?php
/**** Usage in FCom Menu URL field: {user_url}/spaces ****/

if (!defined('ABSPATH')) {
    exit;
}

add_filter('fluent_community/main_menu_items', function ($items, $scope) {

    if (!is_user_logged_in() || empty($items) || !is_array($items)) {
        return $items;
    }

    if (!class_exists('\FluentCommunity\App\Services\Helper')) {
        return $items;
    }

    $Helper = '\FluentCommunity\App\Services\Helper';

    // --- Resolve FC username (xProfile preferred) with WP fallback ---
    $username = '';
    try {
        $userModel = $Helper::getCurrentUser();
        if ($userModel) {
            $xprofile = $userModel->syncXProfile(false);
            if ($xprofile && !empty($xprofile->username)) {
                $username = $xprofile->username;
            }
        }
    } catch (\Throwable $e) {
        // ignore
    }

    if (!$username) {
        $wpUser = wp_get_current_user();
        if (!empty($wpUser->user_login)) {
            $username = $wpUser->user_login;
        }
    }

    if (!$username) {
        return $items;
    }

    // --- Build absolute user URL and force https scheme ---
    $fullUserUrl = untrailingslashit($Helper::baseUrl('u/' . $username));
    $fullUserUrl = set_url_scheme($fullUserUrl, 'https'); // force https

    // Token map (support several formats, since UI sometimes strips braces)
    $tokenMap = [
        '{{user_url}}'  => $fullUserUrl,
        '{user_url}'    => $fullUserUrl,
        '#{{user_url}}' => $fullUserUrl,
        '#user_url'     => $fullUserUrl,
        'user_url'      => $fullUserUrl, // last-resort fallback
    ];

    /**
     * Normalize URLs to prevent:
     *  - http://https//example.com/...   (missing colon)
     *  - http://https://example.com/...  (double scheme)
     *  - http://http://example.com/...   (double scheme)
     *  - scheme-less malformed inputs
     * Always returns https://... when it looks like an absolute URL.
     */
    $normalize_url = function ($url) {

        if (!is_string($url) || $url === '') {
            return $url;
        }

        $url = trim($url);

        // Fix the specific broken form: http://https//example.com/...
        $url = preg_replace('#^https?://https//#i', 'https://', $url);

        // Fix double scheme: http://https://example.com/...
        $url = preg_replace('#^https?://https?://#i', 'https://', $url);

        // Fix double scheme: https://http://example.com/...
        $url = preg_replace('#^https?://https?://#i', 'https://', $url);

        // If it starts with http://, upgrade to https://
        if (stripos($url, 'http://') === 0) {
            $url = 'https://' . substr($url, 7);
        }

        // If it is a valid absolute URL now, force scheme again (belt + suspenders)
        if (preg_match('#^https?://#i', $url)) {
            $url = set_url_scheme($url, 'https');
        }

        return $url;
    };

    // Recursively replace tokens & normalize URLs
    $walk = function (&$node) use (&$walk, $tokenMap, $normalize_url) {
        if (!is_array($node)) {
            return;
        }

        // Common keys used across FCom versions and menu builders
        $urlKeys = ['permalink', 'url', 'link', 'href', 'permalink_url'];

        foreach ($urlKeys as $key) {
            if (!empty($node[$key]) && is_string($node[$key])) {

                // Replace tokens
                $node[$key] = str_replace(array_keys($tokenMap), array_values($tokenMap), $node[$key]);

                // Normalize final URL
                $node[$key] = $normalize_url($node[$key]);
            }
        }

        // Recurse children
        foreach (['children', 'items', 'sub_items'] as $childKey) {
            if (!empty($node[$childKey]) && is_array($node[$childKey])) {
                foreach ($node[$childKey] as &$child) {
                    $walk($child);
                }
            }
        }
    };

    foreach ($items as &$item) {
        $walk($item);
    }

    return $items;

}, 50, 2);

Patricia Montagno

Jeff BrigmanΒ thank you so much!