admin管理员组文章数量:1122832
I'm creating a plugin to auto delete post/event. I created a WP settings page to manage the default settings for auto delete.
I followed each step to register_setting
, add_settings_field
and add_settings_section
for the $callback function.
After that I call settings_fields
, do_settings_sections
and `submit_button' inside this $callback
My settings_fields must be a checkbox, so I use the wp checked
function inside input to check data value stored into wp_options.
All display correctly without do_settings_sections
use. As soon as use this function I get this error "memory size exhausted" and the html part replicates itself indefinitely.
Finally, the checked attribute is not registered
I can't find my mistake:
Add to menu :
function stm_addmenu_auto_delete_event() {
add_menu_page(
'Automatic deletion',
'Automatic <br/> deletion',
'manage_options',
'manage-auto-delete-page',
'auto_delete_settings_callback',
'dashicons-fas fa-trash',
21
);
}
add_action( 'admin_menu', 'stm_addmenu_auto_delete_event','manage_options');
add settings to page:
function new_settings_auto_delete() {
add_settings_section(
'default_auto_delete_section',
'Manage the automatic deletion of posts',
'',
'manage-auto-delete-page'
);
register_setting(
'manage-auto-delete-page',
'default_auto_delete_field',
array(
'type' => 'string',
'sanitize_callback' =>'sanitize_text_field',
'default' => ''
)
);
add_settings_field(
'default_auto_delete_field',
'By default activate automatic deletion',
'auto_delete_settings_callback',
'manage-auto-delete-page',
'default_auto_delete_section'
);
}
add_action( 'admin_init', 'new_settings_auto_delete' );
the callback function :
<?php
function auto_delete_settings_callback(){
$option_auto_delete = get_option('default_auto_delete_field');
?>
<stmwrapper>
<form action="options.php" method="post" id="stm_auto_delete_manager_form_style">
<?php
do_settings_sections('manage-auto-delete-page');
?>
<div id="stm_auto_delete_manager_container">
<property class="default_activation">
<!--<span>By default activate automatic deletion</span>-->
<?php settings_fields('manage-auto-delete-page'); ?>
<input class="default_auto_deletion_toggle" type="checkbox" id="default_auto_deletion_toggle" name="default_auto_delete_field" value="" <?php checked( '1', $option_auto_delete); ?>>
<label class="slider-btn" for="default_auto_deletion_toggle">
<span class="off">Non</span>
<span class="on">Oui</span>
</label>
</property>
<?php
submit_button( 'Save' );
?>
</div>
</form>
</stmwrapper>
<?php
}
I'm creating a plugin to auto delete post/event. I created a WP settings page to manage the default settings for auto delete.
I followed each step to register_setting
, add_settings_field
and add_settings_section
for the $callback function.
After that I call settings_fields
, do_settings_sections
and `submit_button' inside this $callback
My settings_fields must be a checkbox, so I use the wp checked
function inside input to check data value stored into wp_options.
All display correctly without do_settings_sections
use. As soon as use this function I get this error "memory size exhausted" and the html part replicates itself indefinitely.
Finally, the checked attribute is not registered
I can't find my mistake:
Add to menu :
function stm_addmenu_auto_delete_event() {
add_menu_page(
'Automatic deletion',
'Automatic <br/> deletion',
'manage_options',
'manage-auto-delete-page',
'auto_delete_settings_callback',
'dashicons-fas fa-trash',
21
);
}
add_action( 'admin_menu', 'stm_addmenu_auto_delete_event','manage_options');
add settings to page:
function new_settings_auto_delete() {
add_settings_section(
'default_auto_delete_section',
'Manage the automatic deletion of posts',
'',
'manage-auto-delete-page'
);
register_setting(
'manage-auto-delete-page',
'default_auto_delete_field',
array(
'type' => 'string',
'sanitize_callback' =>'sanitize_text_field',
'default' => ''
)
);
add_settings_field(
'default_auto_delete_field',
'By default activate automatic deletion',
'auto_delete_settings_callback',
'manage-auto-delete-page',
'default_auto_delete_section'
);
}
add_action( 'admin_init', 'new_settings_auto_delete' );
the callback function :
<?php
function auto_delete_settings_callback(){
$option_auto_delete = get_option('default_auto_delete_field');
?>
<stmwrapper>
<form action="options.php" method="post" id="stm_auto_delete_manager_form_style">
<?php
do_settings_sections('manage-auto-delete-page');
?>
<div id="stm_auto_delete_manager_container">
<property class="default_activation">
<!--<span>By default activate automatic deletion</span>-->
<?php settings_fields('manage-auto-delete-page'); ?>
<input class="default_auto_deletion_toggle" type="checkbox" id="default_auto_deletion_toggle" name="default_auto_delete_field" value="" <?php checked( '1', $option_auto_delete); ?>>
<label class="slider-btn" for="default_auto_deletion_toggle">
<span class="off">Non</span>
<span class="on">Oui</span>
</label>
</property>
<?php
submit_button( 'Save' );
?>
</div>
</form>
</stmwrapper>
<?php
}
Share
Improve this question
asked May 5, 2024 at 14:18
imagIneimagIne
10511 bronze badges
2
|
2 Answers
Reset to default 1You seem to be creating a recursive loop.
In stm_addmenu_auto_delete_event()
, you register an admin page via add_menu_page()
that has a callback of auto_delete_settings_callback
:
function stm_addmenu_auto_delete_event() {
add_menu_page(
'Automatic deletion',
'Automatic <br/> deletion',
'manage_options',
'manage-auto-delete-page',
'auto_delete_settings_callback',
In new_settings_auto_delete
you register a settings field with a callback of auto_delete_settings_callback
:
function new_settings_auto_delete() {
// …
add_settings_field(
'default_auto_delete_field',
'By default activate automatic deletion',
'auto_delete_settings_callback',
'manage-auto-delete-page',
Thus, when you navigate to the Automatic deletion page you registered in stm_addmenu_auto_delete_event()
:
auto_delete_settings_callback()
is called.- In
auto_delete_settings_callback()
,do_settings_sections('manage-auto-delete-page');
is executed. - WordPress finds settings fields registered to
'manage-auto-delete-page'
. default_auto_delete_field
is found, with a registered callback ofauto_delete_settings_callback
.- We loop back to step 1 recursively, until the server runs out of memory.
It seems you may have meant to register a different callback for either the add_menu_page()
call or the add_settings_field()
call.
Thanks to Wongjn for his advice.
I followed him and rearrange my scripts. I separated into two files my script, one part for the register and add_ functions and other part to display the setting information. (settings_field, do_settings_sections and submit button) .
Before I have renamed the callback to add_menu_page into "auto_delete_menu_page_callback". It was this callback that created the infinite loop.
Of course I call functions after admin_menu add_action:
include plugin_dir_path( __FILE__ ) . 'manage-shedule/functions.php'
So my callback functions looks like this (register and add_ functions remain unchanged) (into my_plugin/manage-shedule/functions.php
):
function auto_delete_settings_callback(){
$get_option_auto_delete = esc_attr(get_option('default_auto_delete_field'));
$is_checked_auto_delete = $get_option_auto_delete == 'on' ? 'checked="checked"' : '';
echo '
<input class="default_auto_deletion_toggle" type="checkbox" id="default_auto_deletion_toggle" name="default_auto_delete_field" value="on" '. $is_checked_auto_delete .' >
<label class="slider_btn_auto_setting_page" for="default_auto_deletion_toggle">
<span class="off">Non</span>
<span class="on">Oui</span>
</label>
';
}
function auto_delete_menu_page_callback() {
include plugin_dir_path( __FILE__ ).'manage.php';
}
and manage.php
(inside "manage-schedule" folder) :
<form action="options.php" method="post" id="stm_auto_delete_manager_form_style">
<stmwrapper>
<?php
settings_fields('manage_auto_delete_page');
do_settings_sections('manage_auto_delete_page');
?>
<?php submit_button( 'Save' ); ?>
</stmwrapper>
</form>
本文标签:
版权声明:本文标题:plugin development - the function do_settings_section($page) generate error "Allowed memory size of ... bytes exhausted 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307998a1933506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
config.php
with something like this:define( 'WP_MAX_MEMORY_LIMIT', '512M' );
– cjbj Commented May 5, 2024 at 15:09add_option_page()
insteadadd_menu_page()
?. What I would like to know is which function is used to add the option to the wp_options table in the database. – imagIne Commented May 5, 2024 at 19:24