Home > database >  How to call a PHP function within a function when the main functions is in a WordPress Plugin and us
How to call a PHP function within a function when the main functions is in a WordPress Plugin and us

Time:12-23

In a previous post I had the problem of having saving error on WordPress page caused by my own written plugin. Someone told me not to use echo in sub files but return. And it works. So now I use $content = "abc"; and add additional like: $content .= "def";

Having that said, could somebody tell me how to call in another function? Normally in php you define functions and call them right in the functions you want. Like:

function myMenu() {
  //do 
}

function myPage() {
  myMenu();
  $content = 'abc';
  $content .= 'def';
  return $content;
}

I tried a lot but was not able to figure it out. So how can I run a function (myMenu) inside a function (myPage) when I have to use return to return text from myMenu?

If you see a way to use simply echo in sub files instead return and calling function "normal" then pls tell. (I have the main plugin file, where I do require once and load some additional php files. (THis is where echo not worked on saving the page)

Thanks.

CodePudding user response:

If you return a value from the myMenu function, you can assign it to a variable in the myPage function. Something like this:

function myMenu() {
  return "menu";
}

function myPage() {
  $content = myMenu();
  $content .= 'abc';
  $content .= 'def';
  return $content;
}
  • Related