• Resolved michaelgazin

    (@michaelgazin)


    Ok, that title probably doesn’t make sense, so let me explain. There are built in functions that are immediately recognized in wordpress, such as the get_header() function or the get_sidebar() function, and once plugins are installed, they too sometimes have recognized “include” functions. How can I make my own?

    I created a file with the php function I made written out, and I am currently using the include function to reference it (ie <?php include(remote_address); ?>. How can I simplify that to one of those include functions where I don’t have to type out the actual web location of the function file?

    Does that make sense?

Viewing 7 replies - 1 through 7 (of 7 total)
  • If your “function file” is — like header.php and sidebar.php would be — part of the current theme’s files, you can use:

    <?php include(TEMPLATEPATH . '/function-filename.php'); ?>

    The WordPress constant TEMPLATEPATH is the full directory path to the active theme’s directory.

    Thread Starter michaelgazin

    (@michaelgazin)

    Its not necessarily part of the theme’s files. I made a little code for the sidebar that displays events for the current day of the week. The events are already predefined and attached to a day of the week in the “function file,” and I simply have a PHP widget with an include function that references that file. It all works fine right now, but I was just looking for a more specific and simple form of function such as “get_events();” instead of my current “includes(remote_address);”

    theposterpreviouslyknownas

    (@theposterpreviouslyknownas)

    you always have the option of using the legacy my_hacks.php file and enabling it’s use. That or you can just add your function to any of the core wp files, or you can plugin-ize it.

    all of the above will allow you to just call the function, instead of using the include method.

    The goal is just to get the function defined without having to include another file, any of the above 3 will work.

    So what you’re asking is: Can I create my own function that is available to theme templates?

    “theposterpreviouslyknownas” notes one way to do this, which is to set up my_hacks.php and drop your function in there. Use of my-hacks.php is also a little out of date. The better option is to learn how to write your functions into a plugin, and use that:

    https://codex.www.ads-software.com/Writing_a_Plugin

    There is also the ability to add functions to a specific theme by creating (or editing) a functions.php for that theme. This PHP document, if it exists in the active theme’s directory, is automatically included by WordPress.

    I would not recommend adding functions to the core WordPress script files, as this would require having to re-add them with each upgrade.

    Thread Starter michaelgazin

    (@michaelgazin)

    Hey guys, thanks for the helpful and quick replies! I’ll experiment with all of this.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    There’s 3 ways:
    -My hacks: not recommended.
    -A theme’s functions.php file: useful if the function is heavily tied to the theme itself.
    -A plugin: Useful because you can turn it on and off in the interface, and allows your setup to be more or less self-contained.

    The “right” way to do it depends, of course, on what exactly it is that you’re doing. ??

    theposterpreviouslyknownas

    (@theposterpreviouslyknownas)

    echo echo echo ?? and your welcome ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to make a php include function link to the actual function?’ is closed to new replies.