开发者

Store output from a php function

开发者 https://www.devze.com 2023-03-20 02:58 出处:网络
I think im a bit over my head on this, but I have a wordpress function that i cant control and it outputs HTML.

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 fun开发者_开发知识库ctions 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.


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

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


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.


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消