Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a function in my plugin myplugin with the name foo, how to call it from the frontend?

 e.g. index.php?
share|improve this question

closed as off-topic by s_ha_dum Oct 11 at 21:47

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.

2  
hmmm a 25% acceptance rate is pretty low –  Tom J Nowell Nov 18 '12 at 20:24
1  
How to call a PHP function within a PHP file is a PHP question, not a WordPress question. –  Chip Bennett Nov 19 '12 at 11:57

3 Answers 3

up vote 6 down vote accepted

The same way you would any other:

foo();

Active plugins are loaded before the theme files

You may want to check that your plugin is activated and the function is available so things dont go pear shaped if you forget to activate it, like:

if(function_exists('foo')){
    foo();
} else {
    echo "oh dear you haven't activated/installed 'myplugin', go do that before the 'foo' feature is available"'
}

Also keep in mind foo is a very generic function name, perhaps the "omgfoo" plugin also has a foo function. So prefix/namespace your function to something unique

You will eventually want to use actions and filters, as they're safer and ebtter practice, you can continue to read up on that here

share|improve this answer

Active plugins are loaded (as in technically - their files are included and processed by PHP during WordPress load) by the time theme templates run.

So your function should be available and can be called as any other function:

<?php foo(); ?>

for example.

share|improve this answer

You don’t. A Theme should not rely on a plugin except in a very controlled environment. Use actions and filters instead.

So in your theme you might use:

do_action( 'before_header' );

… or …

$bg_options = array (
    'wp-head-callback' => 't5_custom_background_frontend',
    'default-color'    => 'f0f0f0',
    'default-image'    => '',
);
$bg_options = apply_filters( 't5_theme_bg_options', $bg_options );
add_theme_support( 'custom-background', $bg_options );
add_action( 'login_head', $bg_options['wp-head-callback'] );

In your plugin you use add_action() and add_filter() to change or add new content. This way the theme will still work when the plugin has been deactivated and you don’t have to use function_exists().

share|improve this answer

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