Title: cell shortcode generated by php
Last modified: January 29, 2020

---

# cell shortcode generated by php

 *  Resolved [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/)
 * Hello
 * MY CURRENT STATE:
    Value of my php variable $txt is formatted as standard TablePress
   shortcode, example: [table-cell id=1 cell=D2/]. This value depends on user input
   which is why I used some php to generate the required shortcode. I’ve tested 
   the output of $txt variable – comes out exactly as the above shortcode.
 * MY PURPOSE:
    I now want to 1)pass the value of this $txt variable (as a shortcode)
   to a table cell (in table id=2) so that then hopefully 2)the above shortcode 
   can do its job: pull data from another table cell (ie pull data from cell D2 
   in the id=1 table).
 * QUESTIONS:
    -Can you please guide me what syntax in the table cell I should use
   when doing 1) so that then 2)happens successfully? -Any other steps I need take
   to make this happen? -Do I need [https://tablepress.org/extensions/php-in-tables/](https://tablepress.org/extensions/php-in-tables/)
   plugin for my purpose?
 * Please advise

Viewing 13 replies - 1 through 13 (of 13 total)

 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12377338)
 * Hi,
 * thanks for your post, and sorry for the trouble.
 * To be honest, I don’t yet understand. Where does that `$txt` variable live? What
   does it contain? Does it contain a Shortcode? Why are you then not evaluating
   that Shortcode? Why don’t you use your own Shortcode (like `[my-var]`) to retrieve
   the variable’s value?
 * Regards,
    Tobias
 *  Thread Starter [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12377485)
 * Hi Tobias
 * Thanks for your reply.
 * The `$txt` lives in my custom-page.php template. Yes indeed – `$txt` contains`[
   table-cell id=1 cell=D2/]`. I need to pass this schortcode to a cell in one table
   so that then data gets pulled from another table. How can I evaluate the Shortcode/
   what steps required for this please? New to this and interested in the most efficient
   way.
 * Re: `[my-var]`, did did you mean I shall input `[$txt]` into the cell? If yes,
   then some concerns: 1)this did not bring up the Shortcode (tried `[$txt]` and`[
   txt]` what am I doing wrong?). 2)I am not sure if this is fit for purpose. Please
   note my end goal is for the shortcode, once it’s successfully passed, is to trigger
   data pull from another dable. Please share more info if this would be the most
   efficient way?
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12378460)
 * Hi,
 * ok, I’m still missing the point as to why you are not directly putting `[table-
   cell id=1 cell=D2/]` into the other table. Or is the Shortcode (the parameters)
   dynamically created/changing?
 * With evaluating, I simply mean to add an extra `do_shortcode()`, i.e. instead
   of
 *     ```
       $txt = '[table-cell id=1 cell=D2/]';
       ```
   
 * use
 *     ```
       $txt = do_shortcode( '[table-cell id=1 cell=D2/]' );
       ```
   
 * With that `[my-var]`, I meant to define your own Shortcode using the WordPress
   functions:
 *     ```
       add_shortcode( 'my-var', 'webmotivated_handle_my_var_shortcode' );
       function webmotivated_handle_my_var_shortcode() {
         global $txt;
         return $txt;
       }
       ```
   
 * Regards,
    Tobias
 *  Thread Starter [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12379180)
 * Tobias, Thanks for your builds.
 * Yes exactly. The reason why I’m doing this is because “the Shortcode (the parameters)
   are dynamically created/changing”.
 * –`echo $txt;` in below code drops “Ruby” **onto my web page** which uses custom-
   page.php as template (by this I mean on a page, yet I need it in my tablepress
   table as well). You’ll see in below code I’m using non-dynamic shortcode, this
   is temporary while I’m solving this.
    -[my-var] (entered either into a TablePress
   cell like shown below or just into plain WP shortcode) still displays as [my-
   var] and that’s my main problem (expecting “Ruby”, because expected result of[
   table-cell id=1 cell=D2/] = “Ruby”). -And so where I’m struggling is pushing “
   Ruby” into the tablepress cell using the shortcode [my-var].
 * This php code is in custom-page.php template (…so NOT in the TablePress cell).
 *     ```
       <...>
       $TablePress_Table_ID = 1;
       //$txt = sprintf("[table-cell id=%u cell=D%u/]",$TablePress_Table_ID, $sat_day); // generates dynamic shortcode, temporarily off.
       $txt = do_shortcode( '[table-cell id=1 cell=D2/]' ); // Tobias's example
   
       add_shortcode( 'my-var', 'webmotivated_handle_my_var_shortcode' );
       function webmotivated_handle_my_var_shortcode() {
          global $txt;
          return $txt;
       }
       echo $txt;
       ```
   
 * This php code is inside the cell @ TablePress
    `[my-var]`
 * Bit lost here…
    -how do I code the custom-page.php template so that I can then
   enter `[my-var]` into the desired tablepress cell and see “Ruby” (cell D2 in 
   table 1 = Ruby and so [table-cell id=1 cell=D2/]=”Ruby”)? -alternatively, in 
   what syntax do I need to reference $txt inside desired tablepress cell so that
   I can see “Ruby” as a result. (asking because `echo $txt` in the custom-page.
   php drops “Ruby” onto my page (just on a page, not in tablepress table))
 * Please advise
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12380436)
 * Hi,
 * this could be a load order problem, i.e. the Shortcode definition might have 
   to come before the tables are printed to the page.
 * Alternatively, you could experiment with the TablePress PHP in tables Extension
   here. Then, your PHP code would go into the table cells, without having to use
   an extra Shortcode. It’s a bit uglier, but also reduces possible points of error.
 * Regards,
    Tobias
 *  Thread Starter [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12381455)
 * Tobias, ok i see
 * RE: PHP IN CELLS. I’ve activated the extension [https://tablepress.org/extensions/php-in-tables/](https://tablepress.org/extensions/php-in-tables/).
   So given `echo $txt` fires what I need into the page OK, **how can i now make
   use of `$txt` variable in the TablePress cell so that it works??** In other words,
   in what syntax shall i put it? Asking because `<?php echo $txt;>` inside TablePress
   Cell doesn’t work. Details below.
 * `<?php echo $txt;>`causes: Parse error: syntax error, unexpected ‘>’, expecting
   end of file in /var/www/wp-content/plugins/tablepress-php-in-tables/tablepress-
   php-in-tables.php(26) : eval()’d code on line 1
 * `<?php echo $txt;` causes: Notice: Undefined variable: txt in /var/www/wp-content/
   plugins/tablepress-php-in-tables/tablepress-php-in-tables.php(26) : eval()’d 
   code on line 1
 * RE: LOAD ORDER. OK will do some google into this. Can you just confirm load order
   might affect both `[my-var]` way and `<?php echo $txt>` way of doing things or
   just one of these ways?
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12382794)
 * Hi,
 * you might have to access this variable as a global variable. Also, note that 
   the closing PHP delimiter is `?>` and not just `>`:
 *     ```
       <?php echo $GLOBALS['txt'] ;>
       ```
   
 * Yes, the load order plays a role here as well. Your definition of the `$txt` 
   variable needs to be finished before it is shown in the table.
 * Regards,
    Tobias
 *  Thread Starter [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12383216)
 * Tobias
 * Noticed `?` is missing at the end in your last post, so should be
    `<?php echo
   $GLOBALS['txt'] ;?>`, yes?.
 * It still does not work; error once `<?php echo $GLOBALS['txt'] ;?>` is pasted
   into a TablePress cell:
    Notice: Undefined index: txt in /var/www/wp-content/
   plugins/tablepress-php-in-tables/tablepress-php-in-tables.php(26) : eval()’d 
   code on line 1
 * —
 * `$txt` is defined as follows in the custom-page.php template:
 *     ```
       $TablePress_Table_ID = 1;
       $txt = do_shortcode( '[table-cell id=1 cell=D2/]' ); // Tobias's example
   
       add_shortcode( 'my-var', 'webmotivated_handle_my_var_shortcode' );
       function webmotivated_handle_my_var_shortcode() {
          global $txt;
          return $txt;
       }
       ```
   
    -  This reply was modified 6 years, 4 months ago by [webmotivated](https://wordpress.org/support/users/webmotivated/).
    -  This reply was modified 6 years, 4 months ago by [webmotivated](https://wordpress.org/support/users/webmotivated/).
    -  This reply was modified 6 years, 4 months ago by [webmotivated](https://wordpress.org/support/users/webmotivated/).
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12384346)
 * Hi,
 * ah, of course I made the same typo now… 🙂
    Yes, `<?php echo $GLOBALS['txt'];?
   >` would be correct.
 * If you are getting that “undefined index” error, this means that you have not
   yet defined your variable in the global scope.
    You could also try adding
 *     ```
       global $txt;
       ```
   
 * in your code. You can also remove the code for the Shortcode definition, as we
   are no longer using that.
 * Regards,
    Tobias
 *  Thread Starter [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12384884)
 * Tobias
 * Struggling here as you can see. Your instructions not clear, sorry.
 * When you say “try adding `global $txt;` in your code”, did you mean adding it
   in the TablePress cell like so: `<?php global $txt; echo $GLOBALS['txt']; ?>`?
   Please clarify.
 * If not, presume you meant adding `global $txt;` to my custom-page.php template
   used by the page + while keeping `<?php echo $GLOBALS['txt']; ?>` in the TablePress
   cell? Yes?
 * If yes, then already tried `<?php echo $GLOBALS['txt']; ?>` in TablePress + below
   code in the custom-page.php without luck:
    option 1 [Error message: Notice: Undefined
   index: txt…]
 *     ```
       global $txt;
       $txt = 1;
       echo $txt;
       ```
   
 * option 2 [Error message: Notice: Undefined index: txt…]
 *     ```
       $txt = 1;
       //Global stuff
       function global_stuff() {
          global $txt;
          return $txt;
       }
       echo $txt;
       ```
   
 * Code in all these options outputs 1 onto the page successfully, however `<?php
   echo $GLOBALS['txt']; ?>` fails @ TablePress.
 * I guess what would help me since I’m so lost now, is some quick example which
   works on your side and so should work on my side. In case it wouldn’t work on
   my side, but i knew it should work (because you told me so), then I would keep
   pushing my WP environment until it works. Looks like I need to sort out the basics
   before I start handling shortcodes with PHP into TablePress. Otherwise I’m now
   in situation where I haven’t seen even the tinny example of success of using 
   php in the TablePress cell. By example I mean: 1) what should I put in my custom-
   page.php so to create a global $txt 2)what should I put in the tablepress cell–
   > kind of clear now that it should be `<?php echo $GLOBALS['txt']; ?>`. So then
   I’d appreciate if you could send me quick example of 1) which should definitely
   work with 2). Possible?
    -  This reply was modified 6 years, 4 months ago by [webmotivated](https://wordpress.org/support/users/webmotivated/).
    -  This reply was modified 6 years, 4 months ago by [webmotivated](https://wordpress.org/support/users/webmotivated/).
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12386042)
 * Hi,
 * I’m afraid that I can’t really help here with your code. The Extension works 
   fine and PHP code is evaluated (as the notice shows). What you do with it and
   how to debug it is up to you, I’m afraid.
 * Putting `<?php echo $GLOBALS['txt']; ?>` in a cell is usually enough. You need
   to make sure that your variables are declared/defined before they are used here.
   You could for example try defining your variable earlier, e.g. in your theme’s“
   functions.php” file, i.e. the
 *     ```
       global $txt;
       $txt = 1;
       ```
   
 * part.
 * Regards,
    Tobias
 *  Thread Starter [webmotivated](https://wordpress.org/support/users/webmotivated/)
 * (@webmotivated)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12444162)
 * Tobias
    Thank you for your help. The last one worked.
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12444334)
 * Hi,
 * great! Thanks for the confirmation!
 * Best wishes,
    Tobias

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘cell shortcode generated by php’ is closed to new replies.

 * ![](https://ps.w.org/tablepress/assets/icon.svg?rev=3192944)
 * [TablePress - Tables in WordPress made easy](https://wordpress.org/plugins/tablepress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/tablepress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/tablepress/)
 * [Active Topics](https://wordpress.org/support/plugin/tablepress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/tablepress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/tablepress/reviews/)

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 13 replies
 * 2 participants
 * Last reply from: [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * Last activity: [6 years, 3 months ago](https://wordpress.org/support/topic/cell-shortcode-generated-by-php/#post-12444334)
 * Status: resolved