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');
Related
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
I'm learning the basics of creating a wordpress plugin. A function named as register_activation_hook is used to execute a specific function when the plugin is activated. I tried to use it, but it is not working. What am i doing wrong?
register_activation_hook(__FILE__, 'kk_aw_install');
kk_aw_install(){
echo "<script>alert('working')</script>";
}
Try to use:
register_activation_hook(__FILE__, 'kk_al_install');
since your function name is kk_al_install not kk_aw_install
I am setting a WP that it should redirect all the request made to sub-directory to file inside that directory.
For example if there a visit on example.com/link/me then it should send the user to /link/index.php?me
To achieve this, I am trying to add rewrite rule in theme function file.
Here's what my code looks like:
function moin_add_rewrite_rules() {
add_rewrite_rule(
'^([^/]*)/(link)/(?:[a-z][a-z0-9_]*)?$',
'/link/index.php?$matches[1]',
'top'
);
}
add_action( 'init', 'moin_add_rewrite_rules' );
It is not working, visiting example.com/link/meshows a 404 (as link is outside WP).
Is there any problem with regex code? or anywhere else? What could be other possible solution?
Looking at the example in the Codex, the matched expression doesn't have the leading /.
Stripping the leading bit out of your regex, then, I think this should work (sorry, I don't have a test environment handy):
add_rewrite_rule('^link/([a-z][a-z0-9_]*)/?$','/link/index.php?$matches[1]','top');
Alternatively, you could do something similar in your .htaccess file.
As a side note, I think as it's currently coded, you should be using $matches[3] not $matches[1], as the value you're interested in is the third parameter in parentheses (the Codex notes "capture group data starts at 1, not 0").
I am having trouble getting Wordpress to assign the proper values to variables using the add_rewrite_rule() function. My goal is to have a url that looks like this: www.mywebsite.com/catalog/category1/category2/item-slug.
Here is my code from the functions.php file:
add_filter( 'query_vars', 'query_vars_new' );
function query_vars_new($query_vars){
$query_vars[]='category1';
$query_vars[]='category2';
$query_vars[]='catalog_item';
return $query_vars;
}
add_action( 'init', 'rewrite_init' );
function rewrite_init(){
add_rewrite_rule('catalog(/([^/]+))?(/([^/]+))?/?','index.php?page_id=94&category1=$matches[1]&category2=$matches[2]&catalog_item=$matches[3]','top');
}
So the goal is to have three variables $category1, $category2, and $catalog_item with the values from the corresponding url segments. However, when I test this the first two variables are set to the same value.
For example, www.mywebsite.com/catalog/clothes/shirts/polo-shirt should return:
$category1="clothes";
$category2="shirts";
$catalog_item="polo-shirt";
But instead I get:
$category1="/clothes";
$category2="clothes";
$catalog_item="/polo-shirt";
There must be something wrong with my add_rewrite_rule function, but I can't figure it out.
Thanks to the incredibly underwhelming response to this question I ended up just making it work without the help of the Wordpress variables. I just configured Wordpress to allow the variables in the url, then used PHP to fetch the URL and get the different URL segments and analyze them. Not quite as pretty as the Wordpress method, but at least it works.
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.