Notifications
Clear all

wpForo Meta Title and Description

3 Posts
2 Users
0 Reactions
228 Views
Posts: 2
Topic starter
(@Anonymous 228344)
Joined: 3 weeks ago

Hey!

Love your plugin. I noticed Rank Math wasn't showing good meta titles and descriptions for forum pages and topics, so I made this fix and hope I did it correctly.

  • Topics: Uses topic title as meta title, first post as description.
  • Forums: Uses forum name as meta title, forum description as description.
// Meta description and title for wpForo pages including topics
add_filter('rank_math/frontend/description', function($description) {
    if (function_exists('is_wpforo_page') && is_wpforo_page() && function_exists('WPF')) {
        
        // For TOPIC PAGES - get first post content
        if (!empty(WPF()->current_object['topic']['first_postid'])) {
            $postid = WPF()->current_object['topic']['first_postid'];
            $post = WPF()->post->get_post($postid);
            
            if (!empty($post['body'])) {
                $content = wp_strip_all_tags($post['body']);
                $content = strip_shortcodes($content);
                $content = preg_replace('/\s+/', ' ', $content);
                
                // Trim to 158 characters
                if (strlen($content) > 158) {
                    $content = substr($content, 0, 155);
                    $content = substr($content, 0, strrpos($content, ' ')) . '...';
                }
                
                return $content;
            }
        }
        // For FORUM PAGES - use forum description
        elseif (!empty(WPF()->current_object['forum']['description'])) {
            $content = wp_strip_all_tags(WPF()->current_object['forum']['description']);
            $content = preg_replace('/\s+/', ' ', $content);
            
            if (strlen($content) > 158) {
                $content = substr($content, 0, 155);
                $content = substr($content, 0, strrpos($content, ' ')) . '...';
            }
            
            return $content;
        }
    }
    return $description;
});

// Force Open Graph description for topics
add_filter('rank_math/opengraph/description', function($description) {
    if (function_exists('is_wpforo_page') && is_wpforo_page() && function_exists('WPF')) {
        
        // Specifically for TOPIC PAGES
        if (!empty(WPF()->current_object['topic']['first_postid'])) {
            $postid = WPF()->current_object['topic']['first_postid'];
            $post = WPF()->post->get_post($postid);
            
            if (!empty($post['body'])) {
                $content = wp_strip_all_tags($post['body']);
                $content = strip_shortcodes($content);
                $content = preg_replace('/\s+/', ' ', $content);
                
                if (strlen($content) > 158) {
                    $content = substr($content, 0, 155);
                    $content = substr($content, 0, strrpos($content, ' ')) . '...';
                }
                
                return $content;
            }
        }
    }
    return $description;
});

2 Replies
Posts: 18
(@reitonas)
Eminent Member
Joined: 2 years ago

Posted by: @Anonymous

so I made this fix and hope I did it correctly.

 

I'll follow this post.

 

I also use Rank Math. The plugin automatically disables most of its features when using wpForo.

 

To avoid incompatibilities with wpForo (which has its own SEO settings).

 

Rank Math only seems useful for the WordPress pages section.

 

For articles, wpForo is used.

 

Your idea is appreciated because Rank Math is truly specialized for SEO.

 

 

 


Reply
Posts: 2
Topic starter
(@Anonymous 228344)
Joined: 3 weeks ago

Hi, I'm using Rank Math Pro and after some testing, I'm finally using this function on my production site now. Let me know if you spot anything that doesn't look right! Thanks.

 

// Meta description and title for wpForo pages including topics

function wpforo_get_clean_description($type = 'topic') {
    if (!function_exists('is_wpforo_page') || !is_wpforo_page() || !function_exists('WPF')) {
        return '';
    }

    $content = '';

    // Topic page
    if ($type === 'topic' && !empty(WPF()->current_object['topic']['first_postid'])) {
        $post = WPF()->post->get_post(WPF()->current_object['topic']['first_postid']);
        if (!empty($post['body'])) {
            $content = $post['body'];
        }
    } 
    // Forum page
    elseif ($type === 'forum' && !empty(WPF()->current_object['forum']['description'])) {
        $content = WPF()->current_object['forum']['description'];
    }

    if (empty($content)) return '';

    // Clean content
    $content = wp_strip_all_tags($content);
    $content = strip_shortcodes($content);
    $content = preg_replace('/\s+/', ' ', $content);
    $content = trim($content);

    // Trim to ~158 characters
    if (strlen($content) > 158) {
        $content = substr($content, 0, 155);
        $content = substr($content, 0, strrpos($content, ' ')) . '...';
    }

    return $content;
}

// Rank Math meta description filter
add_filter('rank_math/frontend/description', function($description) {
    $topic_desc = wpforo_get_clean_description('topic');
    if (!empty($topic_desc)) return $topic_desc;

    $forum_desc = wpforo_get_clean_description('forum');
    if (!empty($forum_desc)) return $forum_desc;

    return $description;
});

// Rank Math Open Graph description filter
add_filter('rank_math/opengraph/description', function($description) {
    $topic_desc = wpforo_get_clean_description('topic');
    if (!empty($topic_desc)) return $topic_desc;

    $forum_desc = wpforo_get_clean_description('forum');
    if (!empty($forum_desc)) return $forum_desc;

    return $description;
});

// Fix Rank Math meta title for wpForo
add_filter('rank_math/frontend/title', function($title) {
    if (function_exists('is_wpforo_page') && is_wpforo_page() && function_exists('WPF')) {
        // For topics
        if (!empty(WPF()->current_object['topic']['title'])) {
            return WPF()->current_object['topic']['title'] . ' - ' . get_bloginfo('name');
        }
        // For forums
        elseif (!empty(WPF()->current_object['forum']['title'])) {
            return WPF()->current_object['forum']['title'] . ' - ' . get_bloginfo('name');
        }
    }
    return $title;
});

Reply