• Resolved 0cg0

    (@0cg0)


    Hi Tobias,

    is there any way to pass the table ID dynamically?
    I’ll explain:
    – in the C page I have the shortcode [test_table_cell id = 123 column = 2 /] and I would like 123 to be dynamic.
    In practice, in page A the table 123 is rendered, in page B the table 456 is rendered, how do I pass the ID of the calling table to the shortcode of page C.
    Thank you

    • This topic was modified 3 years, 4 months ago by 0cg0.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    What do you mean with “calling table” here? Do pages A and B contain a link to page C? If so, you’d have to extend these links to also send a URL parameter with the table ID, i.e. use URLs like

    https://example.com/page-c/?table_id=123
    

    Then, you could define a custom Shortcode that retrieves the ID from the URL parameter and passes it on to the actual table function. For that, you could add this to your theme’s “functions.php” file:

    add_shortcode( 'table-url', 'shortcode_0cg0_table_url' );
    function shortcode_0cg0_table_url( $atts ) {
      if ( isset( $_GET['table_id'] ) && in_array( $_GET['table_id'], array( '123', '456', ), true ) 
    {
        $atts['id'] = $_GET['table_id'];
        return tablepress_get_table( $atts );
      }
    }

    Then, you would use the Shortcode

    [table-url /]
    

    on page C. Note how the code contains a list of the table IDs that are allowed on page C (123 and 456 in this example).

    Regards,
    Tobias

    Thread Starter 0cg0

    (@0cg0)

    Hi Tobias,

    thanks for the answer and sorry if the question was confusing, my english is not the best.
    Page-A and page-B have a link to page-C (i.e. https://example.com/page0/page-C?test=3).
    In page-C there is the schortcode [test_table_cell id = 123 column = 2 /] which returns the ROW number of the table 123 contained in page-A.
    To link page-B (which contains table 456) to page-C, I have to create a copy of page-C (i.e. page-C1) where I insert the schortcode [test_table_cell id = 456 column = 2 /].
    The problem is that I have to create many page-C(i) for each table.

    So I already have a schortcode ([test_table_cell id = 123 column = 2 /]) and I would like to make dynamic only the table ID, but I always need the shortcode [test_table_cell] to get the ROW.

    Maybe I should edit [test_table_cell]:

    add_shortcode ('test_table_cell', 'polywogg_tablepress_cell_test');
    function polywogg_tablepress_cell_test ($ atts) {
      if (empty ($ _GET ['test'])) {
        return;
      }
      $ atts ['row'] = absint ($ _GET ['test']);
      return tablepress_table_cell_shortcode ($ atts);
    }

    It can be done?
    Thank you

    • This reply was modified 3 years, 4 months ago by 0cg0.
    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    then, similarly to how you do it with the row number (which is in the test URL parameter?) you will need to add the table ID.

    So, essentially, you’ll have to merge the two code pieces:

    add_shortcode( 'test_table_cell', 'polywogg_tablepress_cell_test' );
    function polywogg_tablepress_cell_test( $atts ) {
      if ( empty( $_GET['test'] ) ) {
        return;
      }
      $atts['row'] = absint( $_GET['test'] );
    
      if ( empty( $_GET['table_id'] ) || ! in_array( $_GET['table_id'], array( '123', '456', ), true ) ) {
        return;
      }
      $atts['id'] = $_GET['table_id'];
    
      return tablepress_table_cell_shortcode( $atts );
    }

    Then, the links on page A and B have to be changed to
    https://example.com/page0/page-C?test=3&table_id=123
    (or whatever table ID).

    Regards,
    Tobias

    Thread Starter 0cg0

    (@0cg0)

    Hi,
    thanks, kind as always!
    I’ll have to do some testing.

    How should I transform the shortcode [test_table_cell id = 123 column = 2 /] found on page-C?

    Thank you

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    on page C, you would then use

    [test_table_cell column=2 /]
    

    The table ID will be taken from the URL parameter table_id, and the table row from the parameter test.

    Regards,
    Tobias

    Thread Starter 0cg0

    (@0cg0)

    Hi Tobias,
    obviously everything works great!
    Your suggestions are always valuable and professional.
    Thanks again for your availability.
    Regards

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! 🙂 Good to hear that this helped!

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Dynamic ID’ is closed to new replies.