0

I think im a bit over my head on this, but I have a wordpress function that i cant control and it outputs HTML.

It specifically outputs a <a> tag. When i try to store that output into a variable it just echos out the anchor tag even though i thought i stored the output.

I talked to someone that knows more about this and they said the function probably is using its own output system.

Im wondering if there is a way to store the functions output before it echos it out. Like this(but this doesn't work):

$link = wp_function();

This echos out the tag and doesn't store the data.

chasethesunnn
  • 2,149
  • 5
  • 25
  • 42

5 Answers5

4
<?php
ob_start();
wp_function();
$link = ob_get_contents();
ob_end_clean();
?>

Same issue here, How do I capture PHP output into a variable?

Community
  • 1
  • 1
JConstantine
  • 3,980
  • 1
  • 33
  • 46
2

Wordpress have to two types of functions:-

  1. Functions which does not return anything but echoes out output.
  2. Functions which does not echoes out anything but returns output for further uses.

All most all functions have prefixed with get_ to return the value.

for an example the_title just output title whereas get_the_title returns the title.

Search if the function have their get_ version available and use them.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • 1
    +1 because while it doesn't solve the question as stated, that doesn't mean that it isn't the right way of solving the problem. Hopefully this will help others in the future as well. – Blair McMillan Jul 13 '11 at 07:00
  • this is the function that i am working with: http://codex.wordpress.org/Template_Tags/wp_list_categories – chasethesunnn Jul 13 '11 at 07:07
1

You should be able to call ob_start prior to your wp_function() call and then use ob_get_flush():

ob_start();
wp_function();
$link = ob_get_flush();
Blair McMillan
  • 5,299
  • 2
  • 25
  • 45
  • i see that ob_get_flush() is a combination of returning the string and turning off the output buffering but is it any different from ob_get_contents(); ob_end_clean(); – chasethesunnn Jul 13 '11 at 07:15
  • @Nils: No. It just combines the two. – Blair McMillan Jul 13 '11 at 07:22
  • this actually doesn't seem to "catch" the output the same way the other two functions do. When i use the other two functions, it catches whatever the output is and then i do some manipulating and regex stuff to it and echo out the final result. When i use the single ending function, it still echos out the original function AND the regex stuff that i did to it as well. So there seems to be a difference – chasethesunnn Jul 13 '11 at 07:38
1

http://us2.php.net/manual/en/ref.outcontrol.php this may help with more functions

jeni
  • 442
  • 1
  • 4
  • 12
0

What function are you using exactly?
Have you tried to pass the 'echo' => false argument?
I have had this problem when using the wp_nav_menu() function and fixed it with this argument:

$menu = wp_nav_menu( 'echo' => false );
9ete
  • 3,692
  • 1
  • 34
  • 32