I am trying to develop a custom wordpress plugin to manage my membership types. To handle payments I want to have 2 custom action hooks one for payment success and one for payment failure. And if possible I want these hooks to work in all the themes.
Could not come up with a proper solution. Does anybody knows where to place the templates for membership types.
You can use the following code to execute your own action hook:
do_action( 'payment_success' )
Then you can 'hook onto' this hook with:
add_action( 'payment_success', 'your_function' )
Since the theme's functions.php file is loaded after all the plugins are loaded, your hook will be available in all themes.
Related
We have develop a custom plugin for Woocommerce payment integration and we need to add woocommerce hooks, in our plugin file without including it in Theme folder's function.php.
How can we Add custom hooks to our plugin that we can invoke from function.php file? is there any workaround for it?
(when we add custom woocommerce hooks and action code in function.php file in wordpress theme then code work fine but we need it in our custom plugin file.)
Any help will be highly appreciated.
There is 2 kind of hooks: Action hooks and filter hooks.
An action hook is like a gate or a door in some code, that allow you to run some custom code, in a specific code location. It will be executed or "triggered", when the code that handle that door or gate runs. So it's event based.
Filter hooks are a bit different than action hooks. They are not use to trigger some custom code (not event based). They allow to alter or manipulate some existing code values, as strings, arrays, objects… So filter hooks have always at minima one argument to be manipulated and will always return that manipulated argument.
Create custom hooks in a plugin:
1) For action hooks: do_action() Wordpress function
do_action('woocommerce_my_custom_action', $some_variable, $another_variable );
2) For filter hooks: apply_filters() Wordpress function (where $value is the manipulated argument)
$value = apply_filters('woocommerce_my_custom_action', $value, $some_variable, $another_variable );
There is a bunch of documentation, tutorials and ressources on internet regarding hooks
Wordpress filter Vs. action
what is difference between action hook and filter hook in wordpress?
Wordpress Coding a custom action hook
Your plugin should have its own functions in its own php files, it almost sounds like you are pasting the code into the theme's function.php file? And its being overwritten when the theme is updated? Is this what you are doing?
Please read the wordpress plugin development docs.
https://developer.wordpress.org/plugins/intro/
If your plugin requires other plugins, there are several ways of making the user activate the other plugin(s) first:
https://wordpress.stackexchange.com/questions/127818/how-to-make-a-plugin-require-another-plugin
I am working on a WordPress website, with WooCommerce functionality.
I am currently trying to remove the social sharing feature, which is displayed on the Product Pages. I am trying to achieve this through the removal of the Action from its Hook, by placing the following code in the functions.php file:
<?php
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
?>
Said functions.php is placed within the Child Theme I have created.
Unfortunately, this is not removing the social sharing feature. Does anyone know why? To see if there was an issue with the Hooks, I added an Action to the same Hook, to which this worked.
Could it be possible that the Social Sharing feature has been placed in some parent folder which overrides/has higher priority over the functions.php file in the Child theme? If this is the case, what would be my options in getting said feature removed. I can only think of placing the affected file in the Child theme and then manually removing the relevant coding. This is a last resort as it may cause problems, at a later date, when parent files are updated etc.
woocommerce_template_single_sharing() calls the Woocommerce template located at woocommerce/templates/single-product/share.php where there's a do_action call for other plugins & themes to add social sharing. If you want to "block" plugins from using that, your best bet (if you're building a theme) would be to use the built-in Woocommerce template override feature and simply prevent this do_action from firing. This would not cause any problems unless Woocommerce did a massive, structural rework. But otherwise, updating Woocommerce and any other plugins or a parent theme wouldn't affect your child theme.
In your child theme, create a directory called woocommerce, add a sub-directory called single-product, and then copy the share.php file from the above location in the plugin to that location in your theme.
Comment out the do_action and voila! No more social sharing and anything that hooks into that do_action won't run.
As an alternative...if you know exactly what's hooking into that do_action, you would run a remove_action on that bad boy, but then you need to hunt these things down and manually update your call any time a plugin tries to hook into it.
You could use https://codex.wordpress.org/Function_Reference/remove_all_actions
add_action('plugins_loaded', function(){
remove_all_actions('woocommerce_share_hook');
});
I am developing a plugin for Woocommerce. I thought it would be nice if the plugin looked nice in different themes. So I downloaded a theme and installed and activated it.
So I checked the plugin on the product page. But it wasn't there!
I changed back to some other theme and it showed up again!
So my question is, is this my fault for developing a not-so dynamic plugin?
Or is this the fault of the theme creator?
And what are the possible explanations/solutions for those kind of problems?
Example:
Twenty fifteen:
Other theme:
The elements of my plugin are not there as far as I've seen.
Edit
I am using the woocommerce_after_main_content hook.
Which I do like this:
add_action( 'woocommerce_after_main_content', 'ws_action_woocommerce_after_main_content', 10);
This calls my function which runs a shortcode.
Like this:
function ws_action_woocommerce_after_main_content() {
do_shortcode('[ws_frame]');
}
This shortcode ofcourse has the function with <h1>Hello, I'm here!</h1>
Summary of how my plugin works
My plugin is a designer plugin for shirts (May aswell be for other products).
The plugin has an options page in the backend.
My plugin only gets loaded if Woocommerce is active. Also, the plugin only gets shown on the product page IF the product is in the specified category.
Using shortcodes, the front end layout is generated in a function, where some enqueued scripts and css is present. The function also has some HTML.
As mentioned above, I am using woocommerce_before_main_content hook. This calls a function which has the do_shortcode('[ws_frame]') in it. This shortcode as the enqueued scripts, css and has some HTML. Yet, I do have ob_flush(); before the do_shortcode('[ws_frame]'). So this might be something?
Also, there isn't much more going on for the front end of this plugin. This is what have done to make it appear on the product page. The options page in the admin panel still works.
The question still remains, is it me, who needs to fix this, or is this something the theme creator is responsible for?
And how can I fix this myself? How can I make sure all of the hooks I'm using are still available in that theme? Even if it is the theme's creators fault, I would like to know how I can fix this myself.
I may be wrong but I guess in your case the theme for which your plugin is not displayed has the particular action removed. (you may check that in the respective theme's functions file.) Well, it is better idea to hook your plugin (functionality related into some action hook which is related to functionality or which renders some funcitonality (rather than being an UI related action).
Like, the action you have used to hook your plugin woocommerce_after_main_content along with another action, only outputs an html element wrapper. (which some themes might remove and use their custom html wrapper.
In your case since your plugin is related to / displayed on single product page, I would recommend to use relevant action to hook your plugin. e.g. woocommerce_after_single_product_summary or woocommerce_after_single_product, which are generally not removed by woocommerce compatible themes.
Hope this helps.
In many WordPress plugins, there are instructions that Place <?php do_action('plugin_name_hook'); ?> in your templates.
where exactly this code needs to be placed and what should be the plugin_name_hook?
hooks are actions performed at a time.
If you want a custom hook then
1.create your custom hook in your plugin or theme's function.php
add_action('my_action','my_function');
function my_function()
{
// do something
}
2.and call it in your template as
do_action('my_action');
and you can read more here
https://codex.wordpress.org/Function_Reference/do_action
I don't really like plugins that you still need to modify your theme's template file manually. What is the use then of a plugin.
To come back to you question though, your do_action() call will need to go in the template where you need to display the output of the plugin. Say it is a plugin that add social share buttons to the content, you will need to add the do_action after the_content() in your template files.
The second part of the question, the plugin_name_hook will be specified by the specific plugin. So far that you have to follow the installation instructions of the plugin
Is there a way to dynamically add hooks to the themes template.php, like in top-a, or bottom-b?
You can try to register in another hook. The hook setup_theme is called before theme functions.php is included and after_setup_theme afterwards.
So you register one hook which then registers the other hooks.
See as well:
Wordpress 3.0 Program Flow File Inclusions