admin管理员组文章数量:1134249
I have a basic function attached to a button on the settings page of the dashboard
add_action('admin_menu', 'add_streamline');
function add_streamline() {
add_options_page('Streamline', 'Streamline', 'manage_options', 'functions','create_streamline');
}
function create_streamline() { ?>
<div>
<form action="" method="post">
<input type="hidden" name="update-streamline">
<input type="submit" value="Update">
</form>
</div>
<?php
}
Checking if the parameter set in the above form in is there ( in functions.php ) and then running a function based on that
if(isset($_POST['update-streamline'])){
create_properties();
}
The create_properties() function is supposed to make a call to an API and just dump everything to the error log
function create_properties() {
require(get_stylesheet_directory() . '/streamline_sdk/streamline.php' );
$params = array();
$params['status_id'] = 1;
$test = streamline('GetPropertyList', $params );
error_log($test);
}
When I click that button I created above in the backend I expect a bunch of data to get dumped into the error log but all I get is a blank Curl Error.
[20-Aug-2023 02:03:28 UTC] Curl error:
If I put the exact same code from the create_properties() functions into the page.php template I get the expected results in the error log of a bunch of dumped-out data.
What am I doing wrong in functions.php?
Ultimately, when that "update" button is clicked, I want to make some posts in a CPT based on data returned using wp_insert_post()
Thanks in advance..
I have a basic function attached to a button on the settings page of the dashboard
add_action('admin_menu', 'add_streamline');
function add_streamline() {
add_options_page('Streamline', 'Streamline', 'manage_options', 'functions','create_streamline');
}
function create_streamline() { ?>
<div>
<form action="" method="post">
<input type="hidden" name="update-streamline">
<input type="submit" value="Update">
</form>
</div>
<?php
}
Checking if the parameter set in the above form in is there ( in functions.php ) and then running a function based on that
if(isset($_POST['update-streamline'])){
create_properties();
}
The create_properties() function is supposed to make a call to an API and just dump everything to the error log
function create_properties() {
require(get_stylesheet_directory() . '/streamline_sdk/streamline.php' );
$params = array();
$params['status_id'] = 1;
$test = streamline('GetPropertyList', $params );
error_log($test);
}
When I click that button I created above in the backend I expect a bunch of data to get dumped into the error log but all I get is a blank Curl Error.
[20-Aug-2023 02:03:28 UTC] Curl error:
If I put the exact same code from the create_properties() functions into the page.php template I get the expected results in the error log of a bunch of dumped-out data.
What am I doing wrong in functions.php?
Ultimately, when that "update" button is clicked, I want to make some posts in a CPT based on data returned using wp_insert_post()
Thanks in advance..
Share Improve this question asked Aug 20, 2023 at 2:10 grzybowski1911grzybowski1911 898 bronze badges2 Answers
Reset to default 1Sounds like you've got a fun challenge on your hands.
When you're checking $_POST
directly in functions.php
, it might be too early in the WordPress lifecycle for the data to be available, or the action may be bypassing the logic you're expecting.
Instead of using an empty action attribute in your form, it's typically best to handle admin-side form submissions with WordPress's admin_post
action hook.
Change your form's action attribute to the following:
<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
<input type="hidden" name="action" value="handle_update_streamline">
<input type="hidden" name="update-streamline">
<input type="submit" value="Update">
</form>
Then, in your functions.php, you'll want to add:
add_action('admin_post_handle_update_streamline', 'my_streamline_update_handler');
function my_streamline_update_handler() {
if(isset($_POST['update-streamline'])){
create_properties();
}
// After processing, you might want to redirect somewhere like the settings page
wp_redirect(admin_url('options-general.php?page=functions'));
exit;
}
Give that a try! And don't forget to adjust the code accordingly.
Removing the require from the create_properties solved the issue.
I also implemented the code from @ray-happyforms to adhere to the standard way of handling these types of calls.
本文标签: custom function works on pagephp template but not in functionsphp
版权声明:本文标题:custom function works on page.php template but not in functions.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736832084a1954742.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论