I want to try calling the pre-defined functions from the front end private messages plugin in my custom template. But It shows that the function does not exist. But that function is working fine in the plugin.
What should I do?
I want to try calling the pre-defined functions from the front end private messages plugin in my custom template. But It shows that the function does not exist. But that function is working fine in the plugin.
What should I do?
Share Improve this question edited Aug 23, 2017 at 7:12 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Aug 23, 2017 at 6:39 Priyanka SinghPriyanka Singh 231 silver badge5 bronze badges2 Answers
Reset to default -1There can be various reason for the undefined function error:
The plugin might load its function definitions later than
template_include
or in the admin backend only.You might have made a syntax error, for example if the plugin is using a namespace, but you are calling the function without that namespace. Or the function is really a class method, and you treat it as a function …
Another question is: Why would you call that function directly? Templates should have as little dependencies as possible. Imagine someone finds a security problem in the plugin, and you have to turn it off. Do you really want to edit all your template files then? Probably not. :)
It is much better to keep that dependency out of the template and call a custom action instead. For example, in your template you can replace the direct call with an action call:
do_action( 'frontend.message' );
In your theme's functions.php
, you register a callback for that action:
add_action( 'frontend.message', 'my_frontend_message' );
function my_frontend_message()
{
if ( ! function_exists( 'plugin_frontend_message' ) ) {
// Show debug info for those who can do something about it.
if ( current_user_can( 'manage_option' ) ) {
print '<p class="error">Function plugin_frontend_message() not found.</p>';
}
return;
}
print plugin_frontend_message();
}
If you turn off the plugin now, nothing bad will happen, your visitors will just see nothing instead of a message.
- Check documentation of plugin (to now more about function)
Than check the function if is static You can use
ClassName::FunctionName
If not check if there is global object added by plugin
global $plugin_name_object; $plugin_name_object->my_function();
if not simply create
new object
of plugin and call this function.obj = new Plugin(); $obj->my_function();