A good action to hook in would be template_redirect
. There you can query for your endpoint like this:
add_action('template_redirect', function() {
global $wp_query;
// Not your endpoint and not a page/post, we do nothing.
if (!isset( $wp_query->query_vars['download']) || ! is_singular()) {
return;
}
// Here goes your magic!
[…]
// And probably a good idea to exit if you serve files.
exit;
});
There’s also a great tutorial about endpoints on Make WordPress.
Related Posts:
- Where can I find a list of WordPress hooks?
- Difference between after_setup_theme and init action hooks?
- get $post in init filter or action?
- How to intercept a 404 error
- Move excerpt meta box to above content editor
- Trigger custom action when setting button pressed
- Whats worth using add_action when we can simply use add_filter?
- add_action(‘wp_ajax_[action name]’, myfunction) problem
- Hook on trash post
- Which hook if user profile information is updated?
- Adding onload to body
- What is the earliest possible hook for safely using `is_front_page`?
- Send data to 3rd party api with wp_remote_post on wp_login
- How to customize the WP admin default help contents
- Hide gutenberg option blocks
- Make W3 Total Cache “Empty All Caches” function purge varnish [closed]
- admin_post hook not called
- deactivated_plugin hook: get the name of the plugin
- Is there a hook for user activation (after they click the email confirm)?
- Setcookie works on admin but not front end
- How can I add a default description to uploaded files?
- Hook Widget to Woo Theme in functions.php
- Bulk action hook for admin pages which uses WP_List_Table
- How to pass arguments to add_action() [duplicate]
- action lifecycle
- How to use the post_updated hook before and after arguments
- Redirect users on specific post category or category page
- Returning ACF custom field from publish_post
- How get list all users who edited post?
- Stuck in redirect loop after using wp_login action
- I don’t understand why I shoud use lostpassword_url hook?
- How to get session token of current user in wp_login hook?
- Can I remove WooCommerce main content hook?
- How to alter the query using pre_get_posts hook and is_post_type_archive
- How to update WordPress core or themes and still have my child theme hooks work
- Hook any php file into the wordpress api
- WP CLI Get all Enqueued Scripts and Styles
- Delay an action until current action is completed
- How to properly setup an activation hook
- How to remove scripts/style added to customize_controls_enqueue_scripts hook by current active theme
- Help to change the text for new website notification (wpmu_welcome_notification)
- How to remove query string from current page URL?
- How can I count post views of REST API calls and update them in an ACF field?
- Event-Driven Pattern vs MVC?
- Plugins rewrite rules the right way
- new_to_publish fires multiple times
- Strange behaviour with add_{$meta_type}_metadata | add_post_metadata filter
- `rest_user_query` can’t access post author in post edit screen
- Add nofollow to custom widget posts
- Change Password Confirmed Email Text
- How do I trigger a post update within a get_posts() foreach loop?
- Hook when editing user
- get_post_metadata causing some meta data to fail
- Register form: add custom field BEFORE default fields
- Custom Front End Registration – How Does the Key work in the Password Set Request?
- How to modify how the_content outputs a link to an internal page
- Lock user information once fields have been filled in
- save_post hook to add terms getting deleted when using bulk edit
- Add Different Actions To Different Page’s wp_footer
- ‘save_post’ hook not working in WP 3.5
- Getting the post ID in parse_query
- Remove genesis_404 hook from genesis_loop [closed]
- What hooks do I need to hook into to capture ever wp_user creation/change?
- Neccessary to call add_submenu_page and add_menu_page from admin_menu hook?
- “Seen by” Feature in posts [closed]
- Adding custom Bulk Actions
- How to hook in to the “output buffer” and add html content
- hook for loading page
- What hook to use to redirect based on $post
- How to call do_action() to pass arguments to hooked functions that take different number of args?
- Is it likely that do_action will have unexpected results?
- How to export category name and category link in wordpress json
- Error later wordpress 5.3.2 update. Please help
- How to abort a save operation with a WordPress hook?
- is_page not triggering
- admin_post_(action) hook create console log error
- Check if front page within function passed to action
- Hook after creating a post and retrieve infos from this post immediatly
- calculate values from a field and insert them into custom fields when publishing or updating post
- $new_pass always returns null – password_reset hook
- How to resolve 500 error in post listing page?
- Object oriented programming, add_action in constructor not firing
- Get new (not old) post inside transition_post_status hook
- How to Show Different Information to your authors/contributers
- Cannot override hooks.php
- Removing action from template class
- Remove “enqueued” script from array
- theme functions (hooks)
- Function is Missing an Action Hook
- frontend show edit profile with selected custom options
- How to access variables in the function where apply_filters() is called?
- PHP Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered
- How to add action in wordpress custom plugin?
- How can I add new attributes in a Class when my addon is active?
- wp_footer content appearing in admin area
- getSaveContent.extraProps hook for core/post-title doesn’t add new attributes on to the frontend
- Why callback function is not working in wp_head hook?
- woocommerce_add_to_cart_validation max quantity depending on user geoip [closed]
- theme.json is altering Group block HTML
- Not able to set userId and email at wp_set_current_user in Worpdress