Ben Spi
Ben Spi

Reputation: 806

wordpress: how to include a php file in frontend AND admin area (Fatal error: Cannot redeclare myfunction())

I have some troubles to include a file called myplugin_functions.php

This php file contains functions that are required to make some functions work in the wordpress admin area. For that reason I include this file in backend pages using:

include( plugin_dir_path( __FILE__ ) . 'myplugin_functions.php');

This works perfectly.

When I do the same thing on frontend pages, the frontend can access these functions. Great.

But when I then navigate to the backend I get an error message

Fatal error: Cannot redeclare myfunction() (previously declared in /www/htdocs/...) in /www/htdocs/..../myplugin_functions.php on line 28

include_once() makes the problem vanish. But I think wordpress doesn't like including the same php functions for frontend and backend.

Do you know why? What is the best practice to get around this?

Thank you?

Upvotes: 0

Views: 1343

Answers (1)

David
David

Reputation: 5937

wordpress uses the same functions for frontend and backend.....you're prob getting confused with ajax functions which are hooked seperately.

If you are including the file within a function, use require_once/ include_once. If you want the functions to be available to the entire directory, place require_once in your theme functions or plugin file file but then don't include when using functions.

require_once / include_once are actually good practice btw :)

Upvotes: 1

Related Questions