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."
Related
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 just getting my feet wet in Wordpress, and I'm trying to write a very simple plugin using OOP techniques. I've been following this tutorial: http://www.yaconiello.com/blog/how-to-write-wordpress-plugin/ . So far I feel like I understand most of what's going on, but I'm slightly puzzled by statements such as this one:
add_action('init', array(&$this, 'init'));
Having read the documentation on Wordpress's add_action() and PHP callables, I gather that the second argument is a method of a class instance. But I don't understand why $this has to be passed by reference.
Found this note in the PHP docs about callables which I suspect may have something to do with it, but I'm still having a hard time wrapping my head around what the difference is:
Note: In PHP 4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it. For more details, see References Explained.
If I have PHP 5, am I safe just using array($this,'init')?
Possibly related: add_action in wordpress using OOP?
Yes - you are safe to just use array($this, 'init');.
What this is actually is, is a "callable" in PHP. It will be invoked using call_user_func() or call_user_func_array() (internally inside the add_action method).
PHP's official description of this type of callable:
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
You can read more about callables here.
I am having problems with some legacy code that I am trying to add to a Yii project.
It has to do with global variables, which I am well aware should instead be passed as parameters, but since this old code and is used in other projects rewriting it is not really and option.
$testVar = '123';
function testOutput() {
global $testVar;
var_dump($testVar);
}
testOutput();
Now if I include this file in a plain php file it works and outputs
string '123' (length=3)
But if I include this file in a Yii controller or even in a template it output this
null
I have tried to search for this issue but I just get a bunch of results about people using global variables incorrectly. I am sure it is not actually a Yii issue but most likely a php_ini setting that Yii is setting, but I can't find anything when searching the code or the Yii docs that would explain this.
This example can be tested by just creating a file with my first code block and then include it into a Yii template or controller. I even tested it with a clean example Yii project.
I hope I didn't hurt my chances of figuring this out by tagging this questiong with Yii since I have a feeling that it is not just a Yii specific issue.
Any insights would be greatly appreciated.
If you do like this, it will work, I just tested with Yii controller
global $testVar;
$testVar = '123';
function testOutput() {
global $testVar;
var_dump($testVar);
}
testOutput();
As DCoder mentioned, if youre declaring them inside a class, function/method then they are not global. You can try assigning them to the $_GLOBALS array though:
$GLOBALS['testVar'] = 123;
However depending on the legacy code and how youre integrating it you may need to change all references in that legacy code to use $GLOBALS['thevar'] instead of $thevar or do an extract($GLOBALS) at the top of some or all of the legacy files.
Googled: Global Variables in Yii
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.
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