Data is not saving to options table from cutsom post type

Viewed 18

I have a custom plugin which create a custom post type on activation. I have created one sub menu page under that custom post type.My codes worked fine upto that, now am trying to save from fields to options table.But data is not saving to table. Please find the codes below.

I followed this tutorial https://tommcfarlin.com/save-wordpress-submenu-page-options/

The above tutorial works fine if i create a sub menu page under 'Tools' menu, but it is not working for page cretaed under custom post type. Please advice me what are the changes required.

Codes For Creating Sub menu

function alumns_reps_submenu()
{
add_submenu_page(
    'edit.php?post_type=alumns-reps',
    __( 'Settings', 'alumns-reps-settings' ),
    __( 'Settings', 'alumns-reps-settings' ),
    'manage_options',
    'alumns-settings',
    'alumns_reps_settings_callback'
);

}

Codes For View

function alumns_reps_settings_callback(){
  ?>
<?php
/**
 * Renders the content of the submenu page for the Reps-Alumns Settings page.
 *
 *
 */
$settings_title       = __( 'General Settings' );
?>

<div class="wrap">
      <h1><?php echo esc_html( $settings_title  ); ?></h1>
      <form method="post" action="">
        <table class="form-table" role="presentation">
          <tr>
            <th scope="row"><label for="state-reps-headding"><?php _e( 'Headding' ); ?></label></th>
            <td><input name="state-reps-headding" type="text" id="state-reps-headding" value="<?php echo get_option('state-reps-main-headding')  ?>" class="regular-text" /></td>
          </tr>
          <tr>
            <th scope="row"><label for="state-reps-text"><?php _e( 'State Reps Title' ); ?></label></th>
            <td><input name="state-reps-text" type="text" id="state-reps-text" aria-describedby="tagline-description" value="<?php echo get_option('state-reps-text')  ?>" class="regular-text" />
            </td>
          </tr>

          <tr>
            <th scope="row"><label for="current-reps-tab-title"><?php _e( 'Current Rep Tab Title' ); ?></label></th>
            <td><input name="current-reps-tab-title" type="text" id="current-reps-tab-title" aria-describedby="tagline-description" value="<?php echo get_option('current-reps-tab-title')  ?>" class="regular-text" />
            </td>
          </tr>

          <tr>
            <th scope="row"><label for="alumini-tab-title"><?php _e( 'Alumini Tab Title' ); ?></label></th>
            <td><input name="alumini-tab-title" type="text" id="alumini-tab-title" aria-describedby="tagline-description" value="<?php echo get_option('alumini-tab-title')  ?>" class="regular-text" />
            </td>
          </tr>

          <tr>
            <th scope="row"><label for="current-reps-text"><?php _e( 'Current Reps Title' ); ?></label></th>
            <td><input name="current-reps-text" type="text" id="current-reps-text" aria-describedby="tagline-description" value="<?php echo get_option('current-reps-text')  ?>" class="regular-text" />
            </td>
          </tr>

          <tr>
            <th scope="row"><?php _e( 'No Current Reps' ); ?></th>
            <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'No Current Reps' ); ?></span></legend>
            <textarea name="no-current-reps-text" rows="5" cols="50" id="no-current-reps-text" class="large-text code"><?php echo esc_textarea( get_option('no-current-reps-text') ); ?></textarea>
            </p>
            </fieldset></td>
          </tr>

          <tr>
            <th scope="row"><?php _e( 'No State Reps' ); ?></th>
            <td><fieldset><legend class="screen-reader-text"><span><?php _e( 'No State Reps' ); ?></span></legend>
            <textarea name="no-state-reps-text" rows="5" cols="50" id="no-current-reps-text" class="large-text code"><?php echo esc_textarea( get_option('no-state-reps-text') ); ?></textarea>
            </p>
            </fieldset></td>
          </tr>

        </table>
        <?php submit_button(); ?>
        <?php wp_nonce_field( 'alumns-reps-settings-page-save', 'alumns-reps-settings-page-save-nonce' ); ?>
      </form>
    </div><!-- .wrap -->


  <?php

}

Codes For saving data to options table

add_action( 'load-alumns-settings', 'alumns_reps_save_settings' );
/**
 * The method for saving the options to the database or for deleting them
 * based on what the user has specified on the settings page.
 *
 */
function alumns_reps_save_settings() {


  $action       = 'alumns-settings-save';
  $nonce        = 'alumns-settings-save-nonce';

  // If the user doesn't have permission to save, then display an error message
  if ( ! alums_reps_user_can_save( $action, $nonce ) ) {
    return;
  }
  if ( isset( $_POST['state-reps-headding'] ) ) {
      $main_title = $_POST['state-reps-headding'];
      update_option( 'state-reps-main-headding', $main_title);
  } else {
    delete_option( 'state-reps-headding' );
  }

  if ( isset( $_POST['state-reps-text'] ) ) {
      $state_reps_text = $_POST['state-reps-text'];
      update_option( 'state-reps-text', $state_reps_text);
  } else {
    delete_option( 'state-reps-text' );
  }


  if ( isset( $_POST['current-reps-tab-title'] ) ) {
      $current_reps_tab_title = $_POST['current-reps-tab-title'];
      update_option( 'current-reps-tab-title', $current_reps_tab_title);
  } else {
    delete_option( 'current-reps-tab-title' );
  }

  if ( isset( $_POST['alumini-tab-title'] ) ) {
      $alumini_tab_title = $_POST['alumini-tab-title'];
      update_option( 'alumini-tab-title', $alumini_tab_title);
  } else {
    delete_option( 'alumini-tab-title' );
  }

  if ( isset( $_POST['current-reps-text'] ) ) {
      $current_reps_text = $_POST['current-reps-text'];
      update_option( 'current-reps-text', $current_reps_text);
  } else {
    delete_option( 'current-reps-text' );
  }

  if ( isset( $_POST['no-current-reps-text'] ) ) {
      $no_current_reps_text = $_POST['no-current-reps-text'];
      update_option( 'no-current-reps-text', $no_current_reps_text);
  } else {
    delete_option( 'no-current-reps-text' );
  }

  if ( isset( $_POST['no-state-reps-text'] ) ) {
      $no_state_reps_text = $_POST['no-state-reps-text'];
      update_option( 'no-state-reps-text', $no_state_reps_text);
  } else {
    delete_option( 'no-state-reps-text' );
  }
    
}

/**
 * Determines if the user has permission to save the information from the settings page
 *
 *
 * @return   bool                True if the user has permission to save; false, otherwise.
 */
function alums_reps_user_can_save( $action, $nonce ) {


  $is_nonce_set   = isset( $_POST[ $nonce ] );
  $is_valid_nonce = false;
  
  if ( $is_nonce_set ) {
    $is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action );
  }

  return ( $is_nonce_set && $is_valid_nonce );

}
1 Answers

You are using the wrong hook on this line:

add_action( "load-{$page_hook}", 'alumns_reps_save_settings' );

The plugin Query Monitor is a must-have to a developer. You need the screen ID:

So, your load-{$page_hook} action should be:

add_action('load-alumns-reps_page_alumns-settings', function () {
    $action = 'alumns-settings-save';
    $nonce = 'alumns-settings-save-nonce';

    if (!alums_reps_user_can_save($action, $nonce)) {
        return;
    }
    // DO YOUR THING
});

This is the self-contained code I used for testing. Note the wp_die() to debug the save action.

/* Required a CPT with slug "alumns-reps" */

add_action('admin_menu', function () {
    add_submenu_page(
        'edit.php?post_type=alumns-reps',
        __('Settings', 'alumns-reps-settings'),
        __('Settings', 'alumns-reps-settings'),
        'manage_options',
        'alumns-settings',
        'alumns_reps_settings_callback'
    );
});

function alumns_reps_settings_callback() {
    $settings_title = __('General Settings');
    ?>
    <div class="wrap">
        <h1><?php echo esc_html($settings_title); ?></h1>
        <form method="post" action="">
            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row"><label for="state-reps-headding"><?php _e('Headding'); ?></label></th>
                    <td><input name="state-reps-headding" type="text" id="state-reps-headding" value="<?php echo get_option('state-reps-main-headding')  ?>" class="regular-text" /></td>
                </tr>
            </table>
            <?php submit_button(); ?>
            <?php wp_nonce_field('alumns-reps-settings-page-save', 'alumns-reps-settings-page-save-nonce'); ?>
        </form>
    </div><!-- .wrap -->
    <?php
}

add_action('load-alumns-reps_page_alumns-settings', function () {
    $action = 'alumns-settings-save';
    $nonce = 'alumns-settings-save-nonce';

    if (!alums_reps_user_can_save($action, $nonce)) {
        return;
    }
    // DO YOUR THING
});


function alums_reps_user_can_save($action, $nonce) {

    $is_nonce_set   = isset($_POST[$nonce]);
    $is_valid_nonce = false;
    //wp_die(printf('<pre>%s</pre>', print_r($_POST, true)));

    if ($is_nonce_set) {
        $is_valid_nonce = wp_verify_nonce($_POST[$nonce], $action);
    }
    return ($is_nonce_set && $is_valid_nonce);
}
Related