WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] pass variable to a php file (8 posts)

  1. dc_photo
    Member
    Posted 2 years ago #

    Guys,

    I am modifying the toolbox theme.
    I wrote a 'whatever.php' file, and put it under:

    wp-content/themes/toolbox/php/whatever.php

    Now, if I just include it in index.php, it works fine. When I try to pass variables however like this:

    include('php/whatever.php?myvar=3')

    , it breaks down.

    I understand (from the php manual) that I need to include the full path for this to work. However, attempts like:

    include('http://mysite.com/wp-content/themes/toolbox/php/whatever.php?myvar=3')

    of course fail...

    What am I doing wrong? Or is it simply not possible to do this - as I think I read?!

    I know I could use global variables, but I don't really want to.

    Many thanks
    D.

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Why are you including a template file?

  3. dc_photo
    Member
    Posted 2 years ago #

    Because I need to generate for my page 30 similar html elements which vary depending on these variables ... but does it really matter?

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Yes. The more we know about what you are trying to achieve, the better the chance that we can come up with a workable solution. For example, why not use a function in functions.php instead of whatever.php and pass your variables as function arguments?

  5. dc_photo
    Member
    Posted 2 years ago #

    Ah, ok. So you mean transfer the content of whatever.php into functions.php and then simply call it from say index.php whenever I need it? OK, I think I got it.

    But still just to confirm, what I was trying to do is not possible, is it?

    Many thanks for your help!

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    So you mean transfer the content of whatever.php into functions.php and then simply call it from say index.php whenever I need it?

    Yep. Using a standard PHP include in WordPress is always dicey because of the way in which the whole templating system works.

  7. dc_photo
    Member
    Posted 2 years ago #

    I just transferred it to functions.php and it works like a charm, many thanks again!

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    Glad I could help :-)

Topic Closed

This topic has been closed to new replies.

About this Topic