Jump to content

Custom blocks with php - how to output php?


Go to solution Solved by teraßyte,

Recommended Posts

Posted

Hi all,

I've successfully created a custom block containing some php which just echo's text to the page (as a simple test).

But I get an error on save if I do anything other than wrap my custom php inside a function, like so:

function myTest() {
	echo 'testing';
}

How to I run my function? If I just include

myTest()

in the page source I get an error.

Posted

Thanks Marc. Unfortunately I can no longer edit the original post but that was just my example.

Can you please confirm for me how this is supposed to work, because I can't find it in the documentation. Is it a requirement that php code be encapsulated in a function, and then the function must be called (the only non-functionised code) from a single line as I have shown above (with semicolon)?

I can't get anything to generate some output, even though the block editor is no longer balking at my php code.

  • Solution
Posted (edited)

As long as the semicolon is there that code works just fine inside a custom PHP block. I just tested myself by copy pasting your code (adding the semicolon) in a block. Then I added the block to a page.

Maybe you have more code in your block other than the one you posted in the first post?

 

And no, there is no need to even use a function. You can just input your PHP code inside the block without it. A custom PHP block with the code below prints the same output as your code above:

echo 'testing';
Edited by teraßyte
Posted

I see thanks guys.

I don't know what is going on with my cloud setup but this code wasn't working on Friday, and I refreshed today and it starts working. Must have been a cache issue somewhere, but thanks for the help.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...