admin管理员组文章数量:1415664
I'm trying to do the folllowing.
I have a button in my template (in templates folder of my plugin). When they press the button I want to call a function and send and id to the function.
In that function I want to create an api call and send the results back to the template. How can I do this?
I'm trying to do the folllowing.
I have a button in my template (in templates folder of my plugin). When they press the button I want to call a function and send and id to the function.
In that function I want to create an api call and send the results back to the template. How can I do this?
Share Improve this question asked Aug 30, 2019 at 12:05 nielsvnielsv 1731 gold badge5 silver badges17 bronze badges1 Answer
Reset to default 0Based on the limited detail of your question something as per the following is a rough gist of what you could do.
In your theme functions.php file:
<?php
function handle_request () {
if (isset($_GET['custom_id']) && isset($_GET['custom_id_nonce']) ) {
if ( wp_verify_nonce($_GET['custom_id_nonce'], 'custom_id_action') ) {
$id = $_GET['custom_id'];
// FIRST
// do something with $id
// THEN
// possibly redirect somewhere else or back to referrer
// e.g. wp_safe_redirect( wp_get_referer() );
// see https://codex.wordpress/Function_Reference/wp_get_referer
// see https://codex.wordpress/Function_Reference/wp_safe_redirect
} else {
// handle failure state, nonce value is incorrect...
}
}
// if here, the request was likely not for you
}
add_action( 'init', 'handle_request' );
In your theme template file:
<a href="<?php echo wp_nonce_url( home_url('?custom-id=123'), 'custom_id_action', 'custom_id_nonce' );?>">CLICK ME</a>
In the above example, where home_url('?custom-id=123')
is stated, you may want to change the basis for this URL to be the current URL the user is on.
Important reading:
- https://codex.wordpress/WordPress_Nonces
- https://codex.wordpress/Function_Reference/wp_nonce_url
- https://codex.wordpress/Function_Reference/wp_get_referer
- https://codex.wordpress/Function_Reference/wp_safe_redirect
本文标签: pluginsCall function with button and return response
版权声明:本文标题:plugins - Call function with button and return response 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745199103a2647284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论