最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to bind a function to a filter hook via Ajax?

programmeradmin2浏览0评论

I'm trying to bind a function to a filter hook via Ajax, like that:

 function ajax_called_function_contains() {
    add_filter( 'wp_handle_upload', 'save_landscaped_version_of_img' );
 };

The goal is to call the wp_handle_upload only when I click an "add image button" on a given field, so the save_landscaped_version_of_img function triggers on wp_handle_upload hook but only after I've clicked the "add image button" on that given field.

I'm trying to bind a function to a filter hook via Ajax, like that:

 function ajax_called_function_contains() {
    add_filter( 'wp_handle_upload', 'save_landscaped_version_of_img' );
 };

The goal is to call the wp_handle_upload only when I click an "add image button" on a given field, so the save_landscaped_version_of_img function triggers on wp_handle_upload hook but only after I've clicked the "add image button" on that given field.

Share Improve this question asked Jun 14, 2019 at 15:26 AmintaCodeAmintaCode 1771 gold badge4 silver badges16 bronze badges 4
  • No, not really. Not the way you've attempted at least. Could you include more information and code about this image button, and how it's handled, and where you want this filter to work? – Jacob Peattie Commented Jun 14, 2019 at 15:49
  • I get the click on this id="a" button, so I know that the "a button" is pressed and, when the media uploader appears and I upload an image, the function save_landscaped_version_of_img triggers because I've added the function to the wp_handle_upload via Ajax... But it doesn't work.... – AmintaCode Commented Jun 14, 2019 at 15:56
  • 1 it sounds like this is an XY problem, where your question is about Y, which is a solution you've devised for a problem X you've not mentioned. Can you tell us what you were originally trying to do and what the problem was that pushed you to devise this solution you're trying to fix? Also can you explain what these buttons are? It's not clear what you're referring to or what the fields you mention are, or where save_landscaped_version_of_img comes from or what it does. Please update your question and assume we know nothing – Tom J Nowell Commented Jun 14, 2019 at 16:12
  • I think I figured out what you're trying to ask, and now understand why it doesn't make sense to other people. You're asking if you can unhook something on a request so that when the next request happens the behaviour is different. I've left an answer explaining why that isn't possible ( that's not how PHP/WP work ) – Tom J Nowell Commented Jun 14, 2019 at 17:04
Add a comment  | 

1 Answer 1

Reset to default 2

Is it possible to bind a function to a filter hook via Ajax?

No, because page requests are self contained.

When you request something from PHP, everything gets loaded from a fresh clean slate. At the end of the request, that slate is discarded.

This is different from say a Node application that is always running.

So, if you make an AJAX request and unhook a filter, it's only unhooked for that request. A second page load, or AJAX request will not be affected by the unhooking, so it would need to be done on every request.

If you want something to persist across requests, you need to store it somewhere that persists, such as the database of the filesystem. Either way what you want to do won't work, and there's no change to WordPress or PHP you could make that would allow it to work the way you proposed.

发布评论

评论列表(0)

  1. 暂无评论