1

I have a HTML table that I am concatenating like this:

$admin_table_output .= "Item Description: </td><td>";
$admin_table_output .= "<input type='text' name='name' value='$name'></td><td> (Optional)</td></tr><tr><td colspan='4'>";

$admin_table_output is echo'ed out at the end. That works just fine.

My problem is that I want to use apply_filters to modify the variable $admin_table_output like so:

$admin_table_output .= "Item Description: </td><td>";

apply_filters('tabs_page_static_value',$admin_table_output);

$admin_table_output .= "<input type='text' name='name' value='$name'></td><td> (Optional)</td></tr><tr><td colspan='4'>";

Using a hook like this:

function tabs_page_static_value_filter($admin_table_output) {

     $admin_table_output .= 'testing';

     return $admin_table_output;

}
add_filter('tabs_page_static_value','tabs_page_static_value_filter',10,1);

However this does not work. I've also tried using do_action instead, I've tried to echo the variable instead of return it and also not concatenating inside the hook.

I'm thinking I might need to do some kind of a variable variable thing to solve this problem. Any ideas?

1
  • 1
    Ignore my previous comment on the &$ thing... but you should've assigned the filtered value back to the $admin_table_output variable. I.e. $admin_table_output = apply_filters('tabs_page_static_value',$admin_table_output);.
    – Sally CJ
    Commented Aug 17, 2018 at 1:12

1 Answer 1

2

As @Sally mention in the comment. if you want to let others to extend the HTML code. you should also append the new HTML they returning in the filter.

Try code below

$admin_table_output .= "Item Description: </td><td>";

$admin_table_output = apply_filters('tabs_page_static_value',$admin_table_output);

$admin_table_output .= "<input type='text' name='name' value='$name'></td><td> (Optional)</td></tr><tr><td colspan='4'>";
3
  • 2
    This will lead to duplication of "Item Description", as the filtered value is being appended to the original value, not replacing it. Commented Aug 17, 2018 at 3:40
  • @1naveengiri I used = and not .= .. :)
    – Sally CJ
    Commented Aug 17, 2018 at 4:36
  • Thanks so much! Yes, = works but .= does not. Commented Aug 17, 2018 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.