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?
Related
I'm trying to add customization to a Wordpress theme. Instead of replacing the original code, I'm using a "child theme" technique where I can replace files individually.
Now, that original code invokes the get_footer(), and I like to intercept that call so that I can insert my own html output before the actual get_footer function does its work.
I've read about the PHP 5's __call method but that only applies to classes whereas the WP code is not using classes but global functions. So I cannot use that technique, right?
get_footer() function just find and load proper template for the footer. There is a hook in WP get_footer that rans just before template being loaded.
Codex says that is not good to echo-es from this function, but you can use it to load your own template just before template that get_footer() loads.
You can do something like this in your functions.php:
add_action( 'get_footer', 'my_hook_fn' );
function my_hook_fn() {
load_template('my_template_file.php');
}
Codex
I use that thick in several sites, they do not use child themes, but I think this will do the trick.
I have tried making a function which I can run in the .tpl files.
I included a class Templates:
<?php
class Templates {
function getTemplate($template, $gameid) {
echo "test";
}
}
?>
Then I put this into the PHP page.
//Create a template object
$templates = new Templates();
$tpl->assign("template", $templates);
And then I try calling the function in the tpl file:
{$template->get('header',1)}
But I just get a blank page when I try this.
Any ideas how to actually do this?
You should probably look through the documentation on Smarty Plugins. These allow you to register your own functions which can be used just like the built-in tags and modifiers, or even custom sources of template data for use with the {include} function.
Your example is too stripped-down to guess what you actually want to achieve, so feel free to post a follow-up question if you can't see a way of achieving your specific goal.
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.
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."
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