Wordpress basic concept - how to extend/override/customize a plugin hook? - php

I am working on a wordpress site and would like to clarify a basic concept that is definitely very important, and this is how to customize/extend a wordpress hook (at least that's what I think I want to do!)
As the real world example, I am setting up a wp-ecommerce site. When a user adds an item to the cart, I would like to do one or two more things than the original function does. Looking through the source, I find:
/wp-content/plugins/wp-e-commerce/wpsc-includes/ajax.functions.php
with the function:
function wpsc_add_to_cart()
I know I could simply edit the code right here, but obviously that is the completely wrong way to go about it as when the plugin is updated, I will lose changes. What is the correct way to extend a function that is part of a plugin, or wordpress for that matter?
Endless thanks in advance.

You can use the wordpress action hooks to resolve the code loss while plugin upgrade.
You can remove the function which is in plugin file by using remove_action hook and do your own code by adding add_action in your function.php file. So that you can customize your plugin code from theme's function.php.
Here are the examples to explain.I hope it will help.
http://codex.wordpress.org/Plugin_API
http://themeshaper.com/2009/05/03/filters-wordpress-child-themes/

I use a little supressed notice function (it lives in my child themes function.php page), for plugins that get irritating eg: please setup twitter account to use , this kind of warning is not useful at certain stages and sometimes just do not care for it.
function supressed_notices_active(){
echo '<div class="error"><p>Supressed Notices are active</p></div>';
}
if(function_exists('the_plugin_custom_function_call')){
remove_action('the_plugin_custom_function_call' );
add_action('admin_notices','supressed_notices_active');
}else{
function test_message_from_me(){
echo '<h1>show</h1>';
}
add_action('admin_notices','test_message_from_me');
}
So I create the supressed notice function to at least create a warning, so i remember.
Check if the target function exists with the function_exists($target_function) hook
then remove this action from running with the remove_action($tag,$target_function) hook
then just add your custom function with the add_action($tag,$target_function) hook (do not need to have a separate function this could just be a closure)
then else if the function does not exist either still run a new action or leave this section, it can be useful for testing to just add anything so you atleast get some feed back.

What you could try... Copy the function within the plugin file,
paste it into your themes functions.php file,
ie:
function wpsc_add_to_cart() {
global $wpdb, $wpsc_cart;
// default values etc..etc..
// new code here?
}
the only thing with this is, if the plugin is updated and that funciton is renamed or removed, changed or something you could start to run into trouble...
could you not ask the plugin developer to possibly add your requirements to it,
possibly for a small fee? but if your using it as your main shopping cart, then chances are, that small investment could be a good thing.
Marty

Related

Wordpress WooCommerce 'woocommerce_hidden_order_itemmeta' filter hook not being triggered

I've got the hook set up for 'woocommerce_hidden_order_itemmeta' but it is not being triggered during a purchase. My code is below. Note, the code is simplified with the function simply logging a message so I can see if it is being triggered.
function filter_woocommerce_hidden_order_itemmeta( $array ) {
error_log("GOT TO filter_woocommerce_hidden_order_itemmeta()");
return $array;
}
add_filter( 'woocommerce_hidden_order_itemmeta', 'filter_woocommerce_hidden_order_itemmeta', 10, 1 );
I have configured many other WooCommerce hooks and they are being triggered correctly. Is there something special about 'woocommerce_hidden_order_itemmeta'? Does something have to be configured elsewhere before this hook is triggered?
Thanks in advance for any help!!
Cheers!!
I had this same problem recently. Still trying to figure out what's going on, but I can sure you that, if you put this code at functions.php of theme, there's a great chance to get this specific hook to work.
However, this problem affects not only woocommerce_hidden_order_itemmeta hooks, but other ones like woocommerce_after_order_itemmeta, and probably everything suffixed by _order_itemmeta (only a personal guess in this last part)
Obs: Before trying this workaround above, certify yourself that you're using this hook on Order Details Metabox. I think this is the only place where these hooks like *_order_itemmeta are supposed to get work, anyway.

Can't find where the do_action("This function") leads to in wordpress theme

I have this in my index.php file. It adds the home banner image in WordPress. I know that it is mostly generated in WordPress customizer, but I need to add an anchor tag in this section. I can't find it anywhere in the file structure.
<?php do_action('cleanblog_index_top'); ?>
I'm not able to find where cleanblog_index_top leads to. Any help would be great. Thank you!
I stumbled on this old one while looking up the docs for do_action(). The answers are brutal so I decided to provide a better answer in case anyone else stumbles here.
If a WordPress theme has something like do_action( 'example_action_hook_tag' ) somewhere in one of the template files (such as index.php, page.php or whatever) the purpose is to provide theme or plugin authors with a way to write their own custom function that they can then "hook" onto the action with the function add_action().
WordPress would then call this function any time and anywhere do_action( 'example_action_hook_tag' ) is called.
The creators of commercial themes will often litter their template files with hooks declared with do_action() to make it easier for their customers to customize their themes via functions.php or by writing a site-specific plugin.
It looks to me that this is the likely scenario that is impacting the OP. This also explains why the OP was unsuccessful in finding where this "leads to".
It would only "lead somewhere" if the OP wrote a function in the theme/child-theme functions.php or in a plugin and added the line do_action( 'cleanblog_index_top', 'name_of_ops_function' ) to hook their function onto the cleanblog_index_top. WordPress would then call their function when do_action( 'cleanblog_index_top' ) was called in index.php.
From the name of the OP's hook, cleanblog_index_top, it sounds like the theme author intended to provide a way for others to inject output at the top of the index page template.
Suppose the OP wanted <h1>Hello World</h1> to appear there.
In functions.php of a theme/child-theme the OP could add a function that echo's this out:
function op_customization() {
echo '<h1>Hello World</h1>';
}
And hook their function onto cleanblog_index_top:
add_action( 'cleanblog_index_top', 'op_customization' );
Cheers!
You should never edit the index.php file directly, for the same reason you should never edit core Wordpress files directly - the next time WP pushes an update, your changes will be overwritten (and that assumes you don't break anything). Never edit plugin files directly, same reason.
You need to look in your theme, you should only make changes to the functions.php and style.css files in your theme - unless you create a child theme and that is a topic you should Google.

How to trace which function in Wordpress adds certain code to header?

I want to remove most css and scripts added to my Wordpress by plugins and maybe core Wordpress. How can I trace which function adds which line in my page header?
Tried to run search for the lines I want to remove but unsuccessfully. I know already that the code is done through wp_header hook but it does not help finding exact function.
If you know which plugins add the code you can go through plugin files in wp-content/plugins/plugin-name and find following functions.
wp_enqueue_script()
wp_enqueue_style()
Usually it will be together and wrapped in another function which will be called by action:
add_action( 'wp_enqueue_scripts', 'name_of_function' );
For not loading the scripts just uncomment add_action line.
But please be aware, that those scripts are usually vital for correct function of plugin so you will have to serve them from somewhere else.
Also when disabling wp_enqueue be sure you are not disabling admin scripts which would be loaded by action
add_action( 'admin_enqueue_scripts', 'name_of_function' );
To be sure always check parametres of wp_enqueue function where script name should match the script you had previously seen in head section of your site.
Every update of plugin will override the files and therefore enable scripts again
Hope this helps.

Wordpress plugin that overrides function in wp-includes file

This is my first time attempting to make a Wordpress plugin. I would like to insert three lines of code into the post-template.php file under wp-includes. I am thinking I will need a way to override the function, in this case, get_the_content, so that it calls my plugin's version of the function instead of the default one.
Based on what I've searched, it is not a good idea to override the core files so I'm hoping there is a simple way to modify the function through my plugin.
What would be the best method to go about doing so?
You could overwrite it using the same function name inside your plugin, also you could create a child theme and inside that you can copy the file post-template.php so it will load the edited version instead without updating issues, or it may be possible (if its plugable) to overwrite or edit it using a hook, check out this answer: https://stackoverflow.com/a/17202451/7862006
You got that right. Never modify the core files directly for these reasons. However, luckily there are different ways to achieve what you want in WordPress. The simplest would be to add a filter in functions.php (if you are using a theme find the functions.php inside ../themes/..) like this:
add_filter('the_content', 'new_content_filter');
function new_content_filter( $content )
{
$new_content = 'hello';
return $new_content;
}
This will get the content and apply a new filter on it.

WordPress: Check if plugin is installed (ACF)

I want to prevent fatal error in my theme if the ACF plugin is deactivated or not installed.
The main function of the plugin is get_field().
I wrote this code in my functions.php to check:
if ( !function_exists('get_field') ) {
function get_field() {
echo '<span>plugin ACF is not installed</span>';
}
}
Please tell me this is acceptable practice?
ACF itself uses a check to see if the framework has been loaded. If it has already been included and invoked by another plugin or theme, then ACF won't re-instantiate its own class again. It does this with a class check:
if (!class_exists('ACF')) {
// The ACF class doesn't exist, so you can probably redefine your functions here
}
I use exactly this in my own plugins that rely on the presence of ACF so that if it happens to get deactivated, the whole site doesn't bomb out.
First of all, this is not the main plugin function, just one of them. Probably, most commonly used by a plugin user in a theme. Another one is the_field(), which actually prints value (get_field() returns it).
Regarding practice of defining your custom function - it's fine. However, I would not print that long message in every place where ACF field is expected - some of them may be short (numbers), and this message will break the layout. Printing something shorter is better, imo.
Also, function_exists is proper check, not is_plugin_active, because ACF can also be shipped as a library with a theme framework or other plugin.
Another option is to remove ACF dependency on the frontend completely. You can output the contents of the fields with get_post_meta() and prevent ACF plugin from loading on the frontend entirely. See these two posts for details:
http://www.billerickson.net/code/disable-acf-frontend/
http://www.billerickson.net/advanced-custom-fields-frontend-dependency/
There is a wordpress function for that:
is_plugin_active('advanced-custom-fields/acf.php');
Note: You might run into issues when changing to the premium version of a plugins.
Yes, it's a good way to check if the plugin function exists.
You can also try is_plugin_active function to check if the plugin is activated, because the function can be redeclared somewhere.
I think the main reason you are doing that is to prevent Fatal Errors, so it doesn't matter which way you can use.

Categories