is it possible to copy an action hook in wordpress? - wordpress

Is there any way to duplicate an action in wordpress and edit it ?
I want to copy the "woocommerce_single_product_summary" hook (with a different name) and then remove / add some action to it so i will have 2 function.
let's say woocommerce_single_product_summary and woocommerce_single_product_summary_2.
Each one should be independant and if i do something like (on function.php) :
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
It will not remove it on woocommerce_single_product_summary_2
Thanks you in advance :)

You are missing something in your question. By duplicating a hook, you are trying to make some changes to an existing function. You can do that by removing the old function and adding a new function to the hook.
If you are looking to attach another function to the same hook say, woocommerce_single_product_summary you can use add_action to attach a function to the hook.
If you want to remove the function from hook without removing your function, then you have the option,
remove_action( $tag, $function_to_remove, $priority );
This will only remove the function which is specified in the $function_to_remove position

Related

wp_list_comments returning empty

I am trying to remove the default comments/reviews from the WooCommerce product page and replace them with my own.
Removing them was easy as removing them from the hooks that call them. However putting them back is more difficult than I thought it would be. I am actually removing the tabs as well - just want the reviews at the bottom of the page.
In the woocommerce_after_single_product_summary hook I am calling a function that calls the wp_list_comments function. What I find odd (or simply do not understand why) is that the wp_list_comments function only retrieves the comments if I add the per_page argument.
I have tested it with a custom callback (that only var_dumps the comment object) and the woocommerce-comments callback. Both work as long as the 'per_page' argument is supplied.
Is this normal behaviour? When I look at the woocommerce template single-product-reviews.php I can see that they are calling wp_list_comments as such....
<?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
They are only passing the callback argument (I cannot find any filters that are adding any addtional arguments)
Should wp_list_comments be able to be called without arguments and retrieve the comments/reviews for the current product?

Override Wordpress plugin hook

I am using the Wordpress plugin for Stripe, which has a hook action called:
do_action( 'wc_stripe_checkout_receipt_page_before_form' );
This action is located above the Stripe payment form & I would like to display some text here, so how I tap into this hook?
This action can be found in the source below on line 541
https://github.com/woocommerce/woocommerce-gateway-stripe/blob/master/includes/class-wc-gateway-stripe.php
This is what I have tried
remove_action('wc_stripe_checkout_receipt_page_before_form');
add_action('foobar', 'wc_stripe_checkout_receipt_page_before_form');
function foobar(){
echo 'foo';
}
which produces the following warning, but does not display my echo 'foo'
Missing argument 2 for remove_action(),
Your error tells you everything you need to know.
remove_action takes 2 arguments at least - action name and function name which was hooked to it.
In your case, you shouldn't remove it, but instead hook to it. Which you've done wrong. First argument for add_action is an action which you're trying to hook to (wc_stripe_checkout_receipt_page_before_form in your case), second is function which should execute on that action (foobar in your case). The correct call:
add_action('wc_stripe_checkout_receipt_page_before_form', 'foobar');
This way, your 'foo' will be displayed before the form, since that's where the action is called.

Replacing a core WordPress function

I am trying to replace the core WP function wp_delete attachment. In my functions.php file I am adding the following code:
add_action( 'init', 'remove_my_action' );
function remove_my_action(){
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_filter('delete_attachment','wp_delete_attachment',10,2);
}
And then the copy of the replaced function goes here with edited code.
This does not work. I get the error: Cannot redeclare wp_delete_attachment(). I've tried a number of other methods, but can't get it to work.
The bottom line is that I need to add some code to the middle of wp_delete_attachment function. If I can replace that function with my version somehow or add the code to the existing function without editing the actual code in the wp-includes/post.php file (so that version updates don't override my code), I would be satisfied with either. How can this be done? All the options I found through questions have not solved the issue. Thanks!!
You'll need to name your copy of wp_delete_attachment to a unique name. Perhaps namespace it with your site name like function my_site_wp_delete_attachment().
Also, I believe you'll need to use add_action instead of add_filter.
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_action( 'delete_attachment', 'my_site_wp_delete_attachment');

Wordpress URL rewrite not working/registering

I've been trying to get my head around Wordpress URL rewrites, but I'm having no luck.
What I want to do:
I building a custom plugin where a user can build products from various options. The options collectively build a code which refers to the unique product the customer has built.
The code might be something like 140-3-WPA-ABC-2.
The plugin will appear on a single dedicated page:
http://wordpress-site/configurator/
I want a customer with a prexisiting code to be able to enter it into the url like this:
http://wordpress-site/configurator/140-3-WPA-ABC-2/
Whereupon, the plugin gets the variable, and uses it to build the correct product.
Problem
It should be fairly simply but I can't get anything to work using the Wordpress URL rewriting rules, I can't even get anything to seemingly get registered as a Wordpress query var.
I've been trying the following in the main plugin initialisation code:
add_filter( 'query_vars', 'conf_query_vars' );
add_action( 'init', 'cong_rewrites' );
function conf_query_vars($query_vars){
$query_vars[] = 'product_code';
return $query_vars;
}
function conf_rewrites(){
add_rewrite_rule(
'configurator/([^/]+)/?$',
'index.php?product_code=$matches[1]',
'top'
);
}
If I then try and open http://wordpress-site/configurator/140-3-WPA-ABC-2/ I get a page not found error. Echoing query_vars seems to show the variable 'product_code' is not created.
ps I've tried flushing the rewrite cache. Apologies for cross-posting to Wordpress.stackexchange.com - but seems programming question better here?
Try this, I had the same problem.
add_action('init', function() {
add_rewrite_endpoint('sponsor', EP_ALL);
});
add_filter('request', function($args) {
print_r($args);
return $args;
});
I think you should just integrate the function add_rewrite_endpoint.

Wordpress > Create category as child of "uncategorized" (catid=1)

I've got a script in my functions.php file that checks for the existence of several categories that are required by my theme, and if they are not present, it creates them...
if(!get_cat_ID('my-category')){wp_create_category('my-category');}
I need to modify this script in two ways...
First, I need to create the category so that its parent category is the "Uncategorized" category (or id 1).
Second, if the category already exists, but its parent is not id 1, I need to make it so.
First, test if category exists. If it does, use get_category_parents() to get the parents of an existing category.
$parentID = get_category_parents(my-category-ID, false);
Second, the second accepted argument of wp_create_category() is the category you wish to assign as the parent category.
if(!get_cat_ID('my-category')){wp_create_category('my-category',parent category ID);}
Third, if the category does exist, you can use wp_update_term() to change it's attributes.
wp_update_term( $term_id, $taxonomy, $args );
To answer the question asked in your comment on my previous answer...How to run the category modification function when a user activates your theme?
You'll want to use an action hook. Specifically, 'switch_theme'. This is the codex page for all action hooks, I can't link to switch_theme specifically, but scroll down and you'll find it. There is no specific information on this hook, but usage is simple. You can include your function in functions.php or in a plugin file, and after the function definition, include this hook:
function add_my_categories($my-theme-name){
//if $my-theme-name == 'my_theme_name
//test if category exists
//if exists, update
//if doesn't exist, create and assign parent
}
add_action('switch_theme','add_my_categories');
the 'add_action()' call will run the named function when the named hook is encountered in wordpress. The 'switch_theme' hook runs after a theme is changed.
It's important to know that this hook will provide the name of the new current theme to your function, which can accept it as an argument if you need it. For instance, to make sure the function only runs if your theme is the one activated. I suppose if this function is in your theme's functions.php file, it will NEVER run unless your theme is activated, so you can determine if you need to double check the theme name.

Resources