• Resolved simplistik

    (@simplistik)


    Is it possible to add an “arbitrary” filter to $cell_content inside the _render_row() method?

    I want to apply Markdown to the cell contents and I’ve already created a filter that does all that, so that’s not an issue. Also, you do have a filter tablepress_cell_content around the content which can be hooked. But the issue here is that your span triggers #colspan# and things like that, get caught inside the Markdown and are rendered as headers instead of expanding to a column span.

    What I’ve done is added a filter just before line 606

    $row_cells[] = "<{$tag}{$span_attr}{$class_attr}{$style_attr}>{$cell_content}</{$tag}>";

    after all the span trigger logic e.g.

    $cell_content = apply_filters('tablepress_extra_formatting', $cell_content);

    Since your span triggers are processed before this point, all the content that’s left is content that needs to be formatted. However this will obviously disappear if you update the plug-in.

    Let me know what you think :). Thanks.

    https://www.ads-software.com/plugins/tablepress/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your question, and sorry for the trouble.

    To be honest, adding an extra filter hook is not really a good solution. This would slow down the rendering process considerably, for everybody.
    Instead, you should simply check for the span trigger keywords in your filter hook handler function, and simply not evaluate the cell content as Markdown in that case, e.g. with something like

    if ( in_array( $cell_content, array( '#colspan#', '#rowspan#', '#span#' ) ) ) {
      return $cell_content;
    }

    Regards,
    Tobias

    Thread Starter simplistik

    (@simplistik)

    Thanks for the fast response. I get what you’re sayin about the speed issue completely understandable. The solution you provided actually works, for some reason it never crossed my mind that the to do a search within the text.

    For anyone else looking for a similar solution this is the concept being used:

    add_filter('tablepress_cell_content',  'process_markdown');
    
    function process_markdown($text)
    {
        if ( in_array($text, array('#colspan#', '#rowspan#', '#span#')) ) return $text;
    
        $markdown = markdown($text);
    
        return $markdown;
    }
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    yes, that’s exactly what I meant ?? Good to hear that this helps!

    Your approach with Markdown looks really interesting. Are you going to publish it as a plugin? Or can you maybe email it to me (my address is in the main plugin file “tablepress.php”)? Thanks!

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

    Thread Starter simplistik

    (@simplistik)

    I didn’t have any intentions of adding it as a plugin.

    I’m actually using Parsedown https://parsedown.org/ to do all the heavy markdown lifting (seems to be the most consistent one). So the implementation of it is very simple: https://gist.github.com/simplistik/7901838

    I’ve cut out everything that wasn’t necessary, but that is literally how the implementation is happening, and obviously the include path is subject to the user’s directory structure.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    very nice! Thanks for sharing the code!

    Best wishes,
    Tobias

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Request: Hooking the cell contents’ is closed to new replies.