Adding HTML to tables
-
Hi
If you look at this page:
http://www.andrew-leonard.co.uk/wordpress/ferns-2/growing/spore-exchange/spore-list/
You will see that the first entry “Acrostichum aureum” has a HTML link to do a Google search. I have put the HTML in the cell.
My question is, is there any way of arranging for the HTML code to be added to each cell without having to put it in myself – rather in the same way that you can add a CSS style to a whole row with one command in “Plugin Options”
-
Hi,
thanks for your question, and sorry for the trouble.
Well, technically, it is possible to add this HTML automatically, but it’s not as easy as using “Custom CSS”. The reason simply is that this HTML is part of the table content, whereas the CSS is “just” the styling applied to the content.
So, to add that HTML automatically, you would need to use programming. In particular, you would need to use PHP code to hook into one of the available plugin filter hooks like “tablepress_cell_content” and then add the HTML there.Regards,
TobiasI am out of my depth here
I can program a bit in php and have created a function:
function Google_Wrap($tablepress_cell_content) {
$temp=$tablepress_cell_content;
$tablepress_cell_content = ““.$temp.”“;
return $tablepress_cell_content;
}
Now I am lost…
I would somehow have to execute this function on every cell in a specific row in a specific table…Hi,
that’s a great start already.
You’ll now just have to add some extra checks (i.e. whether you are dealing with the desired table and row), by using the other parameters that this filter hooks can use:add_filter( 'tablepress_cell_content', 'Google_Wrap', 10, 4 ); function Google_Wrap( $tablepress_cell_content, $table_id, $row_number, $column_number ) { ... }If you are in the desired table/row/column, you make the necessary modifications, and otherwise, you just return the unmodified cell content.
Regards,
Tobiasadd_filter( ‘tablepress_cell_content’, ‘Google_Wrap15’, 10, 4 );
function Google_Wrap15( $tablepress_cell_content, $table_id, $row_number, $column_number ) {
if ($table_id==15)
if ($column_number==2) {
$temp=$tablepress_cell_content;
$tablepress_cell_content = ““.$temp.”“;
}
return $tablepress_cell_content;
}
Is that it? – where do I put this code?
are all the variables $tablepress_cell_content, etc setup already?Hi,
yes, almost. Just be careful with the braces:
add_filter( 'tablepress_cell_content', 'Google_Wrap15', 10, 4 ); function Google_Wrap15( $tablepress_cell_content, $table_id, $row_number, $column_number ) { if ($table_id==15) { if ($column_number==2) { $temp=$tablepress_cell_content; $tablepress_cell_content = "".$temp.""; } } return $tablepress_cell_content; }Just put this into the “functions.php” in your theme or a small new plugin file, and you should be good to go 🙂
Regards,
TobiasI put the code into functions.php in my child theme and I left out a brace and I got an error message which I have now corrected so it is being “triggered” – but nothing is happening!
Have I misunderstood something?It is working!
I thought I would see the code in “TablePress” – that is what I did not understand – but it adds the code when you load the page(?) -I am not sure but it is working very well when I look at the pageSo thank you – I have enjoyed being your apprentice!
Hi,
this code is basically running whenever the HTML output of the table is being regenerated, but you won’t see these changes on the “Edit” screen.
Note that code changes will only take effect after the internal table cache is cleared, i.e. after you wait for 12 hours, or each time after saving the table.Regards,
TobiasIf you look at my table, some of the Fern Names have a code after them in the form of (Z3-8). I have just amended the function to strip that off, if it exists, so that Google just concentrates on the Fern Name.
Maybe I have misunderstood but it seems to have picked up the amended functionHi,
I’m not sure what you mean here.
If the output at http://www.andrew-leonard.co.uk/wordpress/ferns-2/growing/spore-exchange/spore-list/ (when you are logged-in into WordPress) is not what you expect, your PHP code is not working properly.Regards,
TobiasIt behaves differently when I am logged in to when I am logged out
It only picks up my amended code when I log in
Does this make sense to you?
This is the amended code:
add_filter( ‘tablepress_cell_content’, ‘Google_Wrap15’, 10, 4 );
function Google_Wrap15( $tablepress_cell_content, $table_id, $row_number, $column_number ) {
if ($table_id==15) {
if ($column_number==2 or $column_number==4) {
$x=strpos($tablepress_cell_content, ‘(‘);
if ($x==0) {$temp=$tablepress_cell_content;}
else {$temp=substr($tablepress_cell_content,0,$x);}
$tablepress_cell_content = ““.$tablepress_cell_content.”“;
}
}
return $tablepress_cell_content;Hi,
yes, that makes sense 🙂 The reason is what I described above, the table output caching.
After editing the PHP code, simply save the table on the “Edit” to flush the cache (or wait 12 hours, which is the cache period). After that, you will see the same output when logged-in and when logged-out.
Regards,
TobiasYes you did try to tell me this but I did not understand
I “saved” the table and now it works when I am logged outHi,
very nice! Great to hear that everything is working now! 🙂
Best wishes,
TobiasDear Tobias,
Please check following URL
http://www.gamesage.co.uk/?page_id=8
I need to display image into “Box Art” and “Buy Link”.
I want to update all values for above 2 columns.
Could you please tell me how I need to update “add_filter( ‘tablepress_cell_content’, ‘Google_Wrap15’, 10, 4 )” for my requirement.
Thanks,
Ziya
The topic ‘Adding HTML to tables’ is closed to new replies.