I am trying to add multiple functions to show custom fields in some WordPress page templates. The functions are to show output values from the plugin Toolset Types, however, the same principles should apply in the WordPress built in custom fields.

I can show the output successfully inside the page template by using a code like this:

<?php if (in_category( 'cars' )) : ?>
<?php echo types_render_field("cars-specs", array( )); ?>
<?php endif;?>

However, as I need to insert multiple functions and some functions are common between multiple page templates, I thought of creating a folder inside the child theme and call it functions-library.

Then I created a file inside this folder called cars-functions.phpand pasted the code above in it. Then I went to the page template and put this code:

<?php include('/functions-library/cars-functions.php');?>

But nothing showed.

So I created a function in the cars-functions.php like this:

<?php
function add_cars_specifications() {
 if (in_category( 'cars' )) : [
 echo types_render_field("cars-specs", array( ));
 endif;
 }
add_action( 'init', 'add_cars_specifications' );
?>

Then pasted the following code in the page template:

<?php
include('/functions-library/cars-functions.php');
get (add_cars_specifications());
?>

But still nothing showed.

So what is the best way to pass the PHP functions into the page templates?

Also, if I have multiple functions, is it better to have them in separate files? or this will cause performance issues?

I checked this link: https://stackoverflow.com/questions/921479/include-whole-content-of-a-file-and-echo-it But it deals with echoing the content as html output, not as functions.

I also checked this link: https://stackoverflow.com/questions/8104998/how-can-call-function-of-one-php-file-from-another-php-file-and-pass-parameter-t but this one deals with passing parameters to be used in the destination file. I just want the values to be shown in the desitnation files.

Thank you very much for your support.

closed as off-topic by Mark Kaplun, Dave Romsey, bueltge, EAMann Sep 30 '16 at 17:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • The same principals might apply, but if you show code that depends on a plugin there is nothing we can do to help you here – Mark Kaplun Sep 23 '16 at 4:20
  • Thanks @ Mark Kaplun. What I meant by this comment is to "rule out" that this question is "plugin dependent". I am asking about the proper php function to include functions from other files, not how to show the data of the custom field. Thanks – Atef Wagih Sep 23 '16 at 4:23
  • in addition your description and your code should have ended with a php error... – Mark Kaplun Sep 23 '16 at 4:23
  • you are trying to include a file from the root of the hdd, this should not end well – Mark Kaplun Sep 23 '16 at 4:24
  • @MarkKaplun. I am trying to include a file from a folder inside the child theme. and yes, the error is that the function in I am trying to show did not show. Thanks – Atef Wagih Sep 23 '16 at 4:31
up vote 2 down vote accepted

Just add your functions to your theme's functions.php file. They will be available to all of the templates within the theme.

functions.php:

function add_cars_specifications() {
    if ( in_category( 'cars' ) ) {
        echo types_render_field( 'cars-specs', array() );
    }
}

In your page template file and other templates in the theme, add_cars_specifications() will be in scope, so call it like this:

<h3>Car Specifications</h3>
<?php add_cars_specifications(); ?>
<p>More text...</p>

You can also include other files from functions.php which themselves contain functions. This makes it easier to organize things. Inside functions.php:

require_once( get_template_directory() . '/includes/template-tags.php' ); 

If you really want to make an include only available inside of a particular page template or templates, you can include the external file from the top of your page template like so:

require_once( get_template_directory() . '/includes/my-functions.php' );

Keep in mind that if you do an include like this, you won't be able to trigger actions and hooks that occur earlier in the execution flow, so for example trying to fire a function attached to the init hook would not work.

Note that get_template_directory() is for parent themes and get_stylesheet_directory() is for child themes.

  • Thanks @Dave Romsey. I am less than average in php. So does the function I wrote correct? If so, I can paste it in the 'functions.php'. then how can I call it in the page template? Thanks – Atef Wagih Sep 23 '16 at 4:37
  • Sure Atef. Sorry, but I think that function needs work. I'd suggest doing some research on the plugin API. The init hook is triggered very early in the flow of things, so you never want to echo anything there. (The template has not been rendered yet, so this would be like outputting stuff before your page's opening <html> tag -- not good.) – Dave Romsey Sep 23 '16 at 4:47
  • For your example, assuming the category and types things are working, you can just call add_cars_specifications() directly in your template. You don't need to attach it to the init hook. Try it with a basic function that just outputs a test string, and once that works, start adding the additional complexity. – Dave Romsey Sep 23 '16 at 4:48
  • Thanks I used the "require" code you provided and was able to get a simple test to show. What I am confused about is: when I create a separate file and call it in the functions.php, do I need to create a function in this file? or just paste the code in it? (please see my first code I pasted in the question). Then in the page template, how to I call the values? Thanks a lot. – Atef Wagih Sep 23 '16 at 5:14
  • Sure! Updated the answer with a bit more info and made the example specific to your use case. – Dave Romsey Sep 23 '16 at 5:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.