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.

1
  • What's the exact name of function you are trying with? Commented Jul 13, 2011 at 6:56

5 Answers 5

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

+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.
this is the function that i am working with: codex.wordpress.org/Template_Tags/wp_list_categories
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();

3 Comments

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();
@Nils: No. It just combines the two.
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
1

https://www.php.net/manual/en/ref.outcontrol.php this may help with more functions

Comments

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 );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.