ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Calling an ExpressionEngine variable via PHP

October 20, 2010 7:16am

Subscribe [2]
  • #1 / Oct 20, 2010 7:16am

    Wayne Smallman

    176 posts

    Hi guys!

    After sifting through the Add-On Development guide, and having spent 15 minutes searching through the forums, I’ve admitted defeat — how do I call an ExpressionEngine variable within PHP?

    As an example, I want to parse a URL, using the value of a Custom Field as the variable:

    $array = parse_url($product_pdf, PHP_URL_PATH); print_r($array);

    But this doesn’t work.

    Any help would be much appreciated!

    Also, if you could provide me with a link to the relevant resources (so I can learn myself), that would excellent.

  • #2 / Oct 20, 2010 10:56am

    Jamie Poitra

    409 posts

    You need to assign the EE variable to a PHP one in some way.

    You haven’t really provided enough info for me to give a concrete example.  But if you are using a Plugin for instance you’d want to pass the EE variable (a custom field in this case) to plugin as a parameter. 

    But of course it might be different depending on whether you were trying to use a plugin, an extension, a module, or just PHP straight in the template.

    Jamie

  • #3 / Oct 20, 2010 11:59am

    Wayne Smallman

    176 posts

    Hi Jamie, I have a variable called {product_pdf} which I want to use within PHP. The variable itself is being pushed into the Template via another and is being called via {embed:product_pdf}. That’s all there is to say!

  • #4 / Oct 20, 2010 12:04pm

    Jamie Poitra

    409 posts

    And HOW are you using it in PHP?  Through a Plugin? through PHP embedded into the template?...

    That answer drastically changes how you’d do it.

    Jamie

  • #5 / Oct 20, 2010 12:12pm

    Wayne Smallman

    176 posts

    And HOW are you using it in PHP?

    “The variable itself is being pushed into the Template via another…”

  • #6 / Oct 20, 2010 12:21pm

    Jamie Poitra

    409 posts

    Wayne,

    We’re crossing wires obviously.  Where is your PHP you want to use the variable in?  Is it in the template itself?  Or is it in the form of an EE Plugin, Module, or Extension?

    I understood that you were passing it via templates to another but that doesn’t affect how you’d use it in PHP in any way.

    Jamie

  • #7 / Oct 20, 2010 12:32pm

    Wayne Smallman

    176 posts

    Hi Jamie, I made mention of any Plugins nor any Add-Ons. If I was using such things, I would have said.

    I can only repeat what I’ve said previously; that the variable itself is being pushed into the Template via another.

  • #8 / Oct 20, 2010 12:40pm

    Jamie Poitra

    409 posts

    I see.  We’ll you made mention of reading the developer docs which are all about doing it in the more supported ways of having a plugin or what not.  You were not clear.  Simply not mentioning something isn’t helpful at all.  So your PHP is embedded in the template.  That’s all you needed to say in order to make it clear what you were trying to do.  I wasn’t trying to be obtuse or an asshole I was just trying to figure out what it was you were doing so I could give you an answer that would make sense. 

    So you just need to assign it to a variable.

    There’s a few ways you could do this.  The easiest is simply something like this:

    $product_pdf = '{embed:product_pdf}';

    The above has the drawback of relying on your EE variable not having any single quotes in it.  If it has a chance of that then you probably want to do it a bit differently.  Maybe something like this so it handles things like quotes a little better:

    $product_pdf = <<<CODE
    {embed:product_pdf}
    CODE;
    
    $product_pdf = trim($product_pdf);

    You might not need to trim the variable after that but that will probably result in a slightly cleaner value.

    Does that make sense?

    Jamie

  • #9 / Oct 20, 2010 12:42pm

    Jamie Poitra

    409 posts

    Also Parse order is important here.  You’ll want to make sure you have it set to parse PHP on Output for this template otherwise you won’t get the variable in PHP before the PHP tries to render and your variable won’t have the correct value.

    Jamie

  • #10 / Oct 20, 2010 12:57pm

    Wayne Smallman

    176 posts

    I see. We’ll you made mention of reading the developer docs which are all about doing it in the more supported ways of having a plugin or what not. You were not clear. Simply not mentioning something isn’t helpful at all.

    Jamie, you assumed, which I cannot mitigate against. I can hardly list all of things I wish to exclude, or I’d still be writing the opening thread.

    $product_pdf = '{embed:product_pdf}';

    Does that make sense?

    I’ve been developing in PHP since 2001, so that’s the easy part. Figuring out how ExpressionEngine does its thing is where I’m coming unstuck!

    Yes, the example you gave works, and I was so very close previously; I neglected to add the “embed” part.

    Do you have (or know of) any related resources? I may have cause to begin writing Plugins and Add-Ons at some point, once I get my head around ExpressionEngine more fully.

    Thank you for your time.

  • #11 / Oct 20, 2010 1:02pm

    Wayne Smallman

    176 posts

    Also Parse order is important here. You’ll want to make sure you have it set to parse PHP on Output for this template otherwise you won’t get the variable in PHP before the PHP tries to render and your variable won’t have the correct value.

    Yes, I left the option at the default, which is on output.

    Thanks again.

  • #12 / Oct 20, 2010 1:10pm

    Jamie Poitra

    409 posts

    😊 I was’t wanting you to exclude anything just state what you were trying to do more clearly.  In this case use PHP directly within a template.  All you kept saying was you were using PHP in EE which can be done 4 different ways.  All of which are quite different from one another and would require completely different approaches to getting a variable into them.

    As for learning the Ad-On stuff the developer docs are pretty straightforward if you follow them through and code some test stuff while doing so.  Plugins are the easiest to get started with (vs. Modules and Extensions). 

    In addition to the developer docs.  I’d just take a look at what other plugins are doing and use them for models at first.  Start with some of the more simple ones like the character limit or word limit ones where you just have the plugin class and constructor function to deal with.  And then look at more complex stuff that use multiple functions or more obscure EE classes to do the work.

    Probably 90% of what I learned about developing EE Ad-Ons I’ve learned by looking at what EllisLab and other people did in their own Ad-Ons.

    Jamie

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases