Wordpress dynamically add hook to theme template.php - php

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

Related

WordPress - using Ultimate Member plugin in your custom theme

I'm trying to use Ultimate Member plugin in my custom theme which I created from scratch. Well actually I downloaded this theme (freelancer) from startbootstrap.com and used it as a basis. I activated the UM plugin and added the default pages, but I'm not able to view those pages in my custom theme. Do you need to add any pages or functions in order to activate the UM plugin in a custom theme?
The plugin does work on default themes like twentynineteen. So I believe I should be missing something in my custom theme.
Here is the directory structure of my theme:
wordpress
wp-content
plugins
ultimate-member
themes
mytheme
assets //copied from bootstrap theme
author.php
css
style.css
footer.php
functions.php
header.php
index.php
js //copied from bootstrap theme
page-authors.php
page-{category-posts).php
single-post.php
style.css
ultimate-member
login-to-view
...
(and other files based on the article below)
I referred to this official article for template structure under /themes/mytheme/ultimate-member/ :
https://docs.ultimatemember.com/article/1516-templates-map
I was able to move forward when I added a singular.php file. I made additional changes on top of that but this file seemed to be the file I was missing.

How to add custom hooks to a custom plugin for Woocommerce

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

How do I unhook an action? My current method does not seem to be working

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');
});

Wordpress Coding a custom action hook

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.

How to place plugin hook in a wordpress template

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

Categories