Why do we need to check function_exists for user defined functions? It looks ok for internal or core PHP functions but if user know and defined a function himself then why do need to check for its existance?
Below is custom user defined function
if( !function_exists( 'bia_register_menu' ) ) {
function bia_register_menu() {
register_nav_menu('primary-menu', __('Primary Menu'));
}
add_action('init', 'bia_register_menu');
}
Thanks
To make sure you don't register the same function twice, which will cause an error.
You also use if(function_exists('function_name')) when you are calling functions defined in plugins. In case you deactivated your plugin, your site will still be functional.
In dynamically loaded files using autoloaders, the file containing the function or class might not have loaded, so you need to check if it exists
This answer on the Wordpress StackExchange clarifies why you should sometimes use if function_exists around a function declaration in a theme:
The if function_exists approach allows for a child theme to override the function definition by simply defining the function themselves. Since child theme's functions.php files load first, then they will define the function first and the parent's definition will not get loaded.
I suppose it's analogous to the protected keyword in object oriented languages.
However I still wonder whether there would be any need for it around function declarations in plugins.
Imagine that you use you're URL to get the function name and call it.
Then we have the following info:
url: http://mysite.com/my/page/
When converting this url into a function name, you would do something like this:
implode('_', $myUrlPart); //my_page
The output would be "my_page" as string. But if you call this right away and the function does not exist, an error will be shown. This is where the function_exists comes in, take a look:
if (function_exists($function_name)) {
$function_name(); //the function is called
} else {
//call other function to show HTTP 404 page or something like that
}
Does this makes it a little clearer?
Because WordPress is designed so poorly it does not have any proper mechanism for autoloading modules like that, so you need to add safeguards.
Related
I am just wondering if there is any way to run some custom function when a block is being installed? I can see that there is after_installation() function used in a block, but there is no function declaration in the super class block_base.
In db/install.php (inside your block's folder) put a function called xmldb_block_BLOCKNAME_install(). You should probably return true at the end of this, but I'd have to double check to see if that is required.
You can put whatever you want inside the function. This works for all different plugin types in Moodle.
Am having a plugin, In that I need to change some text it in, but this function doesn't have hook to use.
Usually the function will be override by using remove_action() or remove_filter(). For both the function we need filter name to override. But this plugin function doesn't add any filters.
Now I need to override this function.
For example
Plugin.php in plugin
function plugin(){
echo 'hello';
echo 'welcome you';
}
Function.php in theme
I want to the function plugin() in plugin.php to
function theme_plugin(){
echo 'hello';
echo 'You are welcome';
}
There is not add_action for plugin().
How to override the plugin() to theme_plugin()?
Look here Redefining PHP function?, you can't override php function or overload it. You have to find another way for solving your problem.
Regards,
If am right, you can us this link: http://sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/
**** Edit**
I have no much experience with wordpress. But here is someone asking for the same: Overriding a theme function from plugin in WordPress
And the marked answer is:
Unless the function is meant to be overridden, no. This is basic PHP. You can't redefine a function. If you try you will get a fatal error.
Parts of WordPress are written to be overwritten. Look at /wp-includes/pluggable.php. Every function in there is wrapped in a if( !function_exists(...) ) conditional. Unless your theme did the same, and some do for some functions, you can't overwrite.
Look around for filters that might help you instead.
Looking at your code, you should be able to unhook that. Just make sure to hook the unhook late enough. That is not a good solution, though since you are breaking theme functionality and also must know the know the names of all the hooked functions that themes are using.
Is there something in $fragments, or in $_POST or $_GET or anything else, that you can use to conditionally run your code, leaving the rest alone.
Anyway, Wordpress is opensource, so you can change the code by yourself? Why just change the plugin?
I'm trying to remove an action that a plugin registers in a separate functions.php file, but the syntax is stumping me. The plugin (I can't copy/paste - commercial plugin) infers to the add_action like so:
class Plugin_Class{
function add_actions(){
add_action('tag', array(&$this, 'function_to_remove'), 10);
}
function_to_remove(){
global $wp;
// Code here
}
}
I'm mostly confused with &$this. I know that this refers to the instance of the class, but based off my research it should be removed like so:
Need help with remove_action()
I just don't know how to come up with the syntax for my situation. Why define the global variable? Would I need to do that in my case? I'm assuming the widget array comes from WP core code, but I'm confused on how I need to implement this in my case, which seems to be much simpler. Sorry if this stuff is remedial.
Thanks for any help in advance.
The &$this creates a reference instead of a copy. That way when you access that variable later, you really access this object and not a copy.
http://www.php.net/manual/en/language.references.whatdo.php
See the paragraph about array "not exactly assigning by reference, but equivalent."
In the script below, does the order in which items are declared matter?
For example, if the add_action points to a function that has not yet been defined? Does it matter or should the function declaration always precede any code in which its called?
add_action('load-categories.php', 'my_admin_init');
function my_admin_init(){
//do something
}
That doesn't matter if the function is declared before or after the call but the function should be there in the script and should be loaded in.
This is the first method and it will work:
some_func($a,$b);
function some_func($a,$b)
{
echo 'Called';
}
This is the second method and will also work:
function some_func($a,$b)
{
echo 'Called';
}
some_func($a,$b);
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
However, while this is more of a personal preference, I would highly recommend including all the functions you actually use in an external functions.php file then using a require_once() or include_once() (depending on tastes) at the very top of your main PHP file. This makes more logical sense -- if someone else is reading your code, it is blindingly obvious that you are using custom functions and they are located in functions.php. Saves a lot of guesswork IMO.
you can call a function before it's defined, the file is first parsed and then executed.
No.
It is not C :P...
As you can see here , the whole file is first being parsed and then executed.
If a function that doesn't exist is being called, php will throw an error.
Fatal error: Call to undefined function
As per my personal experience, In some special cases (Like, passing array's in function or function inside a function and so on). It's best option to define the function above the call. Because of this sometimes neither function works nor PHP throw an error.
In normal php functions, it doesn't matter. You can use both of the types.
It does not matter, as long as it is declared somewhere on the page.
as seen here:
http://codepad.org/aYbO7TYh
Quoting the User-defined functions section of the manual :
Functions need not be defined before
they are referenced, except when a
function is conditionally defined
So, basically : you can call a function before its definition is written -- but, of course, PHP must be able to see that definition, when try to call it.
This question already has answers here:
Redefining PHP function?
(9 answers)
Closed 8 years ago.
Can I redeclare a existing function, with the same name, but different code? Or somehow "disable" the old function?
I want to redefince a core WordPress function, but since plugins and theme call this function a lot, I need to keep the same function name.
You could use the WordPress hooks (called filters and actions) and then use the add_filter() function to override the function you are using.
i.e.
function function_name() {
//code goes here
}
$hook = 'get_options'; // the function name you're filtering
add_filter( $hook, 'function_name' );
The Codex will help a lot with this.
http://codex.wordpress.org/Plugin_API/Filter_Reference
Only if you use something like APD that extends the zend engine to allow for that:
Intro
Override Method Docs
Note: Runkit seems like a better option than APD since it's more specific to this purpose and would allow you to keep the original method intact at a different address.
You can wrap the block defining the original function in a conditional checking if another of the same name is not already defined (I'm assuming you mean Wordpress functions and not core PHP ones)
<?php
if(!function_exists('function_name')){
//old definition here
} ?>
You could then redefine it above while still preserving the original should you need to roll back to it.
Depending on how complex the changes are and how many times you may do this, you may also want to look into Namespaces if you are on PHP 5.
Just comment the old function out and write your new one ;)
In PHP it is not possible to redefine or overload (i.e. define a function with the same name but different parameters) a function natively. There though are extensions like runkit which allow to redefine functions (runkit_function_redefine), but you probably don't want to use these (such extensions are rarely installed and mostly unreliable.)
My attempted solution was to do this:
function suffusion_get_image($options = array()) {
include_once ABSPATH.'wp-content/themes/suffusion-child/functions/media.php';
return childtheme_overide_suffusion_get_image($options = array());
....
}
Obviously there is an overhead at upgrade as you would need to add lines back into the scripts again and I have used this method successfully to date but now trying to do it with get_terms in the wp-includes and hitting a redeclaration issue which I am trying to resolve or workaround at the moment.
My reason to edit core is that the existing core does not provide in a convenient way for a multisite requirement.
Someone has just suggested on another forum however using override_function but the manual is worded such that it appears to be of use only to built-in functions - I took it that means PHP built in functions