tablepress search

How to search in TablePress tables with WordPress Fulltext Search?

Sometimes people facing issues when using WPFTS (Free or Pro edition) with TablePress plugin. TablePress plugin is a cool tool to create beautiful tables, which can be inserted to any WP post or page via simple shortcode like [table id=1].

As you know, 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 opens by the simple PHP snippet using wpfts_index_post. 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 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.

Was it useful? Share the experience!

Leave a Reply

Please Login to comment
  Subscribe  
Notify of