Finding hooks Wordpress - wordpress

I know there are lists of hooks for WordPress like --> http://adambrown.info/p/wp_hooks/hook
But if I want to find hooks for a plugin like WC Vendors there is a much shorter list of hooks on their website.
Are 'do_action' and 'apply filter' functions the only thing we can modify?
If given a class like --> https://github.com/wcvendors/wcvendors/blob/master/classes/admin/class-product-meta.php#L10, is there any way to modify it?
Are we limited to the do_action hooks or is there a way to modify other areas as well? Can we use the WordPress hooks to hook into the WC Vendors plugin as well?

Mostly you should try to accomplish any task with hooks, but some tasks are just not possible without actually modifying the actual code. But we all know its not good to modify core code, as all changes disappear after an update. So instead of modifying a class, you can extend it. You can override the current features and also add new ones. Extending a class is as easy as using a relavant hook in functions.php and then extending it in the same file or requiring it from another file. Here is an official tutorial on how to add a new shipping method to the woocommerce shipping class.
Sometimes you dont even need all the hooks, you just need to find the ones that are running on a specific page. For this you can use the code below to list all the current hooks.
$debug_tags = array();
add_action( 'all', function ( $tag ) {
global $debug_tags;
if ( in_array( $tag, $debug_tags ) ) {
return;
}
echo "<pre>" . $tag . "</pre>";
$debug_tags[] = $tag;
} );
Or you can use this plugin "simply show hooks"which is really helpful while development as it gives you an idea of where each hook is being triggered on the page.

Related

A Poll That Is Auto Created For Each Post in Wordpress

I'm looking to have a poll at the end of each of our posts that the viewer can vote on. I don't want to have to manually create a poll for each post.
The only plugins I can find are star ratings. We're looking for a custom poll that asks something like "Would you consider this article: Pro This, Anti This or Right in the Middle". Otherwise I have to create a unique poll for each post and manually insert. We're planning on having dozens of new posts a day.
I've been searching everywhere and cannot find anything like this. I'm hoping someone has a recommendation.
Like I wrote in the comment, you can easily achieve what you want in several ways. For example :
Create child theme ( create custom single.php or post.phpor any other alternative )
Create a child theme with a custom function in functions.php ( see below )
create own plugin with custom function and use the_content filter ( see below )
function whateverContentYouWant($content) {
if(!is_feed() && !is_home()) { // your conditions if any
$content.= "Enjoyed this article?"; // add your content here
}
return $content;
}
add_filter ('the_content', 'whateverContentYouWant');
That being said, I am not sure this site is the best place to search plugin recommendations. better if you have a more specific code-related issues ( with examples )

Is it possible to have a wordpress filter without a corresponding function definition

Is it possible that this statement can exist without a corresponding function definition for woocommerce_rest_check_permissions:
apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, $post_type );
I searched the WooCommerce codebase but couldn't find a corresponding function definition. I rather found four usage references all inside an apply_filter statement.
Yes, this is very common in WP plugins. The developers of the woocommerce plugin put these apply_filter statements in their code so that other developers(You) can modify the behavior of the plugin without having to alter the source code of the plugin itself.
You can checkout a list of all the hooks available in woocommerce here https://docs.woocommerce.com/wc-apidocs/hook-docs.html

Restoring 'post' post-type via a child theme

I am working with a client who has chosen a theme which looks nice but actual removes a lot of WordPress functionality. Whether or not it is deliberate, it has removed the post type of 'post'. I found the hook they used to do this but unfortunately, they added it via a closure not a callback.
Below is a sample piece from the theme:
add_action('admin_menu', function () {
remove_menu_page("edit.php");
remove_menu_page("edit-comments.php");
}
});
The scenario is that I am creating a child theme to add back the WP functionality. The only way I can think to restore this is by adding another action that uses add_menu_page. I just don't really know how to restore it.
I may have to switch themes but they really like this one aesthetically. Guess the downside is that it reinvents the wp-admin backend. It wants us to add data through it's interface and not through the traditional 'post' and 'page' post types.
Anybody have any solutions?
I see only one solution - absolutely remove hook admin_menu and after do full restore (without closure). Of course, need more read and learn about hook admin_menu before operating. You can start from remove_all_actions
Solution No.2:
If closure callback is one in parent theme you can to use simple 'closure' remover :)
//remove closure callback
$hooks = $wp_filter['admin_menu'][10];
foreach ($hooks as $key => $value) {
if (preg_match('|^\d|', $key))
//closure's always started from 00000....(??)
remove_action('admin_menu', $key);
}

Customize search results for Custom Post Types

I am writing a Wordpress plug-in that creates several Custom Post Types (CPT). For they have their own custom fields, that need to be displayed in the search results, I need to customize the search results output.
Do I need to write my own theme for that or is there a hook (or other way) to solve this in my plug-in code?
You could hook into get_the_content and get_the_excerpt filters and test with is_search() to see if you should alter the returned value or not.
Not tested, but this is the idea:
add_filter( 'get_the_excerpt', 'my_search_excerpt' );
add_filter( 'get_the_content', 'my_search_excerpt' );
function my_search_excerpt( $content ) {
if ( is_search() ) {
$content = 'This is a search excerpt for ' . get_the_title();
// maybe add a read more link
// also, you can use global $post to access the current search result
}
return $content;
}
I see four possibilities:
Create a new (child) theme
Override the search template using filters (template_include)
Use client-side code to modify the appearance (CSS / JavaScript, poor workaround)
Hook the_content or the_excerpt
The easiest way might be to copy the search.php file of your installed theme and modify it to fulfill your needs. You can then hook it in using the first or second way. The first requires you to create a child theme, the second to create a plugin. The latter might be more complex so I would suggest to create a theme (take a look at template files of child themes for an explanation).

How to filter Wordpress posts using a hook in a plugin?

I'm creating a Wordpress plugin and, being a newbie in the development on this platform, I'm stuck on this problem.
I'd like to have posts in the loop filtered by categories, defined by the user through an admin page. I would actually like to be able to modify query_post() parameters in the plugin, but the only trick I found is to re-run the query_post() with my user-defined criteria, thing that I would like to avoid.
Also, due to the plugin nature, I think it make no sense to modify the theme's template.
I'm sure the solution is evident, but can't find it!
I thought there was a nicer solution, but this is how I finally solved it:
add_filter ( 'query_vars', 'myplugin_filter_posts');
function myplugin_filter_posts( $content )
{
//WP's query handler
global $wp_query;
//The id of the category whose posts I'd like to show
$catId = 1;
$result = $wp_query->query( 'cat='.$catId );
return $content;
}
If you tips for a better solution, please share :)

Resources