tablepress search

How to search in TablePress tables with WordPress Fulltext Search?

The TablePress search functionality is not great. Yes, you can search live in tables, however, if you inserted the table shortcode into the post or page, this post/page will not be found by words from the table. It’s not good. The basic WPFTS (Free or Pro edition) configuration does not fix this issue too.

As you know, the WP Fulltext Search plugin indexes post contents without open shortcodes by default, which is done to reduce memory consumption and indexing time. You can switch on shortcode indexing by the simple checkbox in WPFTS Settings / Indexing Engine configuration. However, this does not work for TablePress plugin shortcodes.

The reason for that is the way how TablePress initializes its code. The shortcode processors in this plugin are initialized fully only in front-end mode. However, the wpfts_index_post is executing in back-end (admin) mode. And thus, TablePress’s shortcodes remain unconverted.

How we can fix this? We going to use the small PHP snippet, which forces TablePress to create shortcode processors even in Admin mode to natively convert shortcodes into HTML code and put this HTML to the WPFTS index. Open your functions.php file in the current theme folder and insert it to the beginning of the file.

add_filter('wpfts_index_post', function($index, $p)
{
        global $wpdb;
        
        // Check for post types if we don't need to unpack tables for each post
        if (($p->post_type == 'post') || ($p->post_type == 'page')) {
                global $post;
                
                // Check if we have table shortcode inside the post_content
                if (preg_match('~\[table~', $p->post_content, $zz)) {
                        // Yes, so let's check if TablePress controller didn't initialized yet
                        if (class_exists('TablePress', false)) {
                                // Let's initialize it
                                $tb_controller = TablePress::load_controller( 'frontend' );
                                $tb_controller->init_shortcodes();
                                
                                // Now we need to unpack all shortcodes in this post content
                                $post = get_post($p->ID);
                                setup_postdata($post);
                
                                ob_start();
                                the_content();
                
                                $r = ob_get_clean();
                                $r = strip_tags(str_replace('<', ' <', $r));
                
                                wp_reset_postdata();
                                
                                // Done. Let's update post_content cluster data (we can use different cluster as well)
                                $index['post_content'] = $r;
                        }
                }
                
        }
        return $index;
}, 3, 2);

After this change, do not forget to save functions.php, upload it to your hosting and press the Rebuild Index button to refresh index data.

As you can see it’s nothing too hard.

Please let us know if this recipe helped you or not.

Wait, in case you’re looking for hosting for your WordPress website, do not hurry. First, I strongly recommended reading this.

5 2 votes
Article Rating
Was it useful? Share the experience!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments