I know php, but very new to wordpress. and I just don't want to learn this stuff, only need to change something for a short while,
I am declaring a function in file
var/www/wp-content/plugins/woocommerce/templates/checkout/thankyou.php
function cr (){
}
But wordpress gives me error
Can't redeclare function cr() (previously declared on line no..xxx) in file ....
now the line number it mentions is the only place where this function is declared, This is what I tried :
tried to rename the function to very uncommon names and every time the same error.
wrapped the function in
if(!function_exists('cr')) {
function cr() {
..
..
}
}
I get an error :
Fatal error: Call to undefined function cr() in
/var/www/storearn/wp-content/plugins/woocommerce/templates/checkout/thankyou.php
on line 74
What could be the reason?
You're declaring the function in the wrong file.
Firstly, template files are the wrong place to declare a function. It belongs either within a custom plugin or your theme (functions.php or some file included from there). Ideally a custom plugin but it would depend on context. With the plugin placed in the right location there shouldn't be any risk of it being duplicated. You may also want to prefix it too.
The second issue I see is that you're attempting to modify files within the WooCommerce plugin itself. As soon as you update WC, those modifications will be overwritten. Templates should be overridden individually within your theme.
Documentation: https://docs.woocommerce.com/document/template-structure/
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.
while seeing word press core code in depth i came across a file which has many empty functions for eg :
/**
* #ignore
*/
function apply_filters() {}
i realy dont know what is the use of declaring empty function in php..
i found this in wp-admin/list-scripts.php on line 34 - 37
and in wp-include/plugin.php on line 163 - 207 the same function is re declared with some works in it
In total i have 2 questions
What is the use of declaring an empty function in php
Why wordpress din't show any Fatal error: as the same function is already declared. ?
In PHP (and many other OOP languages), an empty function (or, more precisely, method) can be used in an interface. Any class inheriting that interface must implement the declared functions. However, last time I checked (which is, 2 minutes ago), WordPress isn't really an OOP system, so forward to 2.
list-scripts.php is not a default WordPress file - I can't find it in any of my WP installation. You may want to test by putting a die('called'); on top of the file and see if it gets executed. Therefore, WordPress won't encounter duplicated function declaration, and no fatal errors are introduced.
Now, even if list-scripts.php is a default WP file, when working with WP (and PHP in general) more often than not you see this:
if (!function_exists('apply_filters')) {
function apply_filters($arg1, $arg2) {
// code is poetry
}
}
This makes sure a function is only declared if it hasn't been before, and avoids the fatal error.
I guess wordpress will conditionally include either one or the other file. A lower level API of wordpress expects this functions to be defined and calls them. The extension itself is free to implement the function or not, however it has at least to provide the empty function body. The concepts behind this are much like interfaces work in OOP.
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.
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.
I'm writing my first wordpress plugin and I'm trying to create a function to be called when the plugin is activated.
Currently it looks like this:
class ThumbsUp {
...
}
global $thumbs;
function thumbs_install() {
//global $thumbs;
$thumbs = new ThumbsUp(); /* Line 160 */
$thumbs->installThumbsUp();
} /* Line 162 */
// When plugin is activated -> install.
register_activation_hook(__FILE__,'thumbs_install');
But when I activate the plugin I get the following error:
Plugin could not be activated because it triggered a fatal error.
Fatal error: Cannot redeclare thumbs_install() (previously declared in /dev/site/wp-content/plugins/thumbs-up/thumbs-up.php:160) in /dev/site/wp-content/plugins/thumbs-up/thumbs-up.php on line 162
I've googled and looked and it's talked about as a variable scope issue but I can't find any examples of the answer and my php is not strong enough to translate the discussion into code.
Here's the solution described by John Blackbourn in the WP-hackers ML:
Any global variables that you want to reference inside the function that is called by register_activation_hook() must be explicitly declared as global inside the main body of the plugin (ie. outside of this function). The plugin file is include()-ed inside another function at the point where it is activated unlike at others times when the plugin file is simply include()-ed. Phew. Bit of an odd one to get your head around but there we go.
I thought I had done what is described but I still get the error. I've also tried every other combination of where I could possibly put the global $thumbs...
There is a more generic answer to this question: every error that occurs in the code that is run from the function registered with register_activation_hook will be shown as "cannot redeclare ... " instead of the actual error. I suspect this is because of the way WordPress includes the plugin file when it calls the activation hook.
Note: if you arrived here via your search engine for a result about Wordpress's register_activation_hook() and global variables, please jump to the second part of this answer.
The rationale behind this error message is that the thumbs_install name is first created while include()ed once, and then a second time.
One of those times, it is include()ed in the scope of activate_plugin() from /wp-admin/includes/plugin.php on line 560; the other one is most likely your doing: Wordpress does not include any plugin that is not yet activated and includes the plugin file in activate_plugin() only once.
Moreover, I was unable to reproduce the issue with the code you pasted in your question, but I got exactly that error with the following version of the ThumbsUp class:
class ThumbsUp {
function installThumbsUp() {
include(__FILE__);
}
}
However, since you did not share the code of your ThumbsUp class with us, I cannot help you further on that direct matter.
It is worth noting that the first (before activation) inclusion of the plugin in the activate_plugin() function is aimed at preventing Wordpress from crashing because of un-activated plugins; and therefore, it is very well possible that the extra include() or require() happens somewhere else in the code of your plugin (not necessarily in a local scope).
About the use of global variables in the callback function passed to register_activation_hook(); essentially because the first include() (during activation) of the plugin happens in the scope of a function (activate_plugin()), it is necessary to declare those variables global in every location of the plugin where they are accessed.
That means: they need to be explicitly set global in the scope of the plugin file too (where you normally consider variables to already be global).
This is because the said variables are defined, during the first activation, in the scope of activate_plugin(), and unless set global explicitly, they will not exist in the global scope.
Example:
<?php
global $myvar;
$myvar = 'some value';
function using_myvar() {
global $myvar;
some_processing_with($myvar);
}
register_activation_hook(__FILE__, 'using_myvar');
Nota bene: Since after the first activation, the plugin is considered 'safe' to be include()ed globally; it is only necessary for variables used by the aforementioned callback to be declared global in the file scope.