0

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
}
2
  • This is a PHP error that has nothing to do with your code, except that it is apparently eating memory. You can solve it by increasing the memory limit in your config.php with something like this: define( 'WP_MAX_MEMORY_LIMIT', '512M' );
    – cjbj
    Commented May 5 at 15:09
  • Unfortunatly it's not the solution. So I prefer to know why I get this error rather than bypassing it. I used 268435456 bytes before , after increasing limit memory it used 536870912 bytes. The issue is still there ! As far as I'm concerned, the other plugins are not responsible. Must I use add_option_page() instead add_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 at 19:24

2 Answers 2

1

You 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():

  1. auto_delete_settings_callback() is called.
  2. In auto_delete_settings_callback(), do_settings_sections('manage-auto-delete-page'); is executed.
  3. WordPress finds settings fields registered to 'manage-auto-delete-page'.
  4. default_auto_delete_field is found, with a registered callback of auto_delete_settings_callback.
  5. 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.

0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.