Hi,

I'm fairly new to Backdrop so apologies for a basic question.

I have tried added some simple php to the body of a page using the edit tab and the source option, and using the full html editor.

When I save the page my <?php tag is comment out i.e. replaced with <!--?php.

What am I doing wrong?

Thanks.

Accepted answer

You're missing the 'PHP filter' module which makes this possible. However this was removed from Backdrop core and I don't believe a replacement exists in contrib. This was for security reasons, so I think the recommended way to add custom PHP code is either via a module or theme function...

Comments

Hi Richard,

welcome to Backdrop... so did you enter PHP code into the Text field? Or did you edit a template file?

 

 

Hi and thanks.

When logged into my website as admin I get two tabs, View and Edit. I selected Edit and entered the code into the Body (Edit Summary) part of the page. (Is this called a block?)

BWPanda's picture

You're missing the 'PHP filter' module which makes this possible. However this was removed from Backdrop core and I don't believe a replacement exists in contrib. This was for security reasons, so I think the recommended way to add custom PHP code is either via a module or theme function...

Ok thanks. Sorry for the big delay in replying. What I can't figure out is, once I've written a php module, how to I actually call that functionality in a backdrop webpage? All the php examples/tutorials online assume that the php is embedded in an html page within <?php... ?> tags which are unsupported in Backdrop.

indigoxela's picture

What I can't figure out is, once I've written a php module, how to I actually call that functionality in a backdrop webpage?

Backdrop uses "hooks" for that and has tons of them. If your module wants to inject something into page markup, you'd, for instance, use template_preprocess_page().

Doing the first steps with Backdrop might be easier, if you start with a theme, though.

You still have all the hooks available, but it's easier to put something in a template file (*.tpl.php).

You'd copy the base template file to your theme, flush caches, and adapt what you need.

Maybe it helps to have a look at how the core themes like Basis do things with templates.

If you can't figure out which template file to copy, ask here in the forum.

 

Thanks. I read the API documentation about hooks but I couldn't find an example of the syntax you use to call a hook within the markup of a webpage.

indigoxela's picture

...to call a hook within the markup of a webpage

Hooks are typically called from a xxx.module file or from a theme's template.php.

Here's a hook called from a theme which adapts some page variables, adds some css...

Here's a hook called from another theme that adapts the markup of a layout.

If you want to add some php code directly to a template file, you don't need hooks.

But maybe I totally misunderstand what you want to achieve...

What exactly would you like to add to the body of a page or pages? Content? Markup?

I'm probably thinking about it in the wrong way. All the examples I have seen of using php put the php code within <?php tags within the body of html page however that is not supported within Backdrop. So I was trying to figure out how to modify the body of the html page to get at a variable . So for example, if I had some php code that read a value, say some exif data from a photo, how would I call the php from that page and then get that value back to the page to display it.