WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to display text from functions.php (7 posts)

  1. brainiac
    Member
    Posted 5 years ago #

    Hi,
    I just wrote my first theme function, which is supposed to
    insert text into sidebar. It works fine from command line,
    but WP displays its text, instead of regular page.

    e.g. function foo() { echo 'foo'; }
    then <li><?php foo(); ?>

    But all I see is 'foo' - not the rest of the page.

    I have done this dozens of times with CGI and Javascript,
    what do I have to differently to get it to work with PHP/WP ?

    Thanks!

  2. Mark / t31os
    Moderator
    Posted 5 years ago #

    More info about the problem please...

    If in your functions file you have.
    function foo() { echo 'foo'; }

    Then in a template file you have..
    <?php foo(); ?>

    The result is...

    foo

    That's what i'd expect anyway, is that not what happens?

  3. brainiac
    Member
    Posted 5 years ago #

    Thanks for replying.

    WP *only* displays foo e.g: if page is:
    <h1>title</h1><p>text</p><p><?php foo()?></p>
    all I see is foo

    Tried adding header('Content-type: text/plain')
    like I would do from js/ajax then all I got was
    plain text 'foo` -
    page displays normally I remove function call.

    Was that clear?

  4. Mark / t31os
    Moderator
    Posted 5 years ago #

    foo() only contains foo, so that will be returned/printed on screen...

    Did you expect something different?

    Seems to me it's behaving just as it should, so i'm a little confused..

  5. brainiac
    Member
    Posted 5 years ago #

    Regular site worked fine.
    After I added style function, I only saw the output
    of my function -- instead of page, not added to page. Does it make sense now?

    I rewrote it using JS to call PHP, just adding text/plain header,
    and it works fine,

    however I would like to know what I did wrong, because I would rather
    use one php statement instead of a couple of JS functions.

  6. Mark / t31os
    Moderator
    Posted 5 years ago #

    Well lets see how your functions file looks and the file you're including it into.

    Do you get any PHP errors output to the screen?...

    Please post code from functions file and template file where you're using the function..

    Thanks,

  7. brainiac
    Member
    Posted 5 years ago #

    It works: WP Test

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.