Lorraine
Lorraine

Reputation: 486

save_post action isnt triggered in wordpress

Im developing a theme which includes logic to add custom fields in admin panel to all pages except the ones I have included in an exclussion array. This is showed by a function showSection(). Its works fine but seem save_post_page action isnt triggered so data isnt updated:

function __construct() {

  if ($this->showSection()) {

    $this->title = get_post_meta(get_the_ID(), 'lorraine_landing_title', true);
    $this->subtitle = get_post_meta(get_the_ID(), 'lorraine_landing_subtitle', true);

    $this->addAction('add_meta_boxes_page', array($this, 'addMetaBox') );
    $this->addAction('save_post_page', array($this, 'saveMetaBox') );

  }

}

public function showSection() {
  if (isset($_GET['post'])) {
    $slug = get_post_field('post_name', $_GET['post']);
    return !in_array($slug, $this->pageWithoutSections);
  }
} 

function saveMetaBox($postId) { 

  if (isset($_POST['lorraine_landing_title'])) {

    check_admin_referer('lorraine_admin_landing_save', 'lorraine_admin_landing_nonce_field');

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
      return;
    }

    //Save data
    update_post_meta($postId, 'lorraine_landing_title', sanitize_text_field( $_POST['lorraine_landing_title']));
    update_post_meta($postId, 'lorraine_landing_subtitle', sanitize_text_field( $_POST['lorraine_landing_subtitle']));

  }

}

If I dont call showSection() save_post_page action is triggered. It seem that $_GET['post'] is causing the conflict but I need to get page id to show or hide sections and I havent access to get_the_ID() function.

Update

get_post_meta works because I have another function printTemplate which requires metabox html file and inside it I instantiate the class:

function addMetaBox() {
  add_meta_box('lorraine-admin-landing', __('Landing'), array($this, 'printTemplate'), 'page', 'normal', 'default');
}

function printTemplate($post, $box) {
  require_once( get_stylesheet_directory() . '/components/admin/landing/landing-html.php');
}

landing-html.php file

<?php $adminLanding = new AdminLanding(); ?>

<?php wp_nonce_field('lorraine_admin_landing_save', 'lorraine_admin_landing_nonce_field'); ?>

<div class="form__item">
  <label for="name"><?php _e('Título', 'lorraine'); ?></label>
  <input name="lorraine_landing_title" type="text" class="form__input" value="<?php echo $adminLanding->getTitle(); ?>" maxlength="60" placeholder="<?php _e('Introduce el título de la landing', 'lorraine'); ?>">
</div>

<div class="form__item">
  <label for="name"><?php _e('Subtítulo', 'lorraine'); ?></label>
  <input name="lorraine_landing_subtitle" type="text" class="form__input" value="<?php echo $adminLanding->getSubtitle(); ?>" maxlength="60" placeholder="<?php _e('Introduce el subtítulo de la landing', 'lorraine'?>">
</div>

Update 2

So... in action hook "add_meta_boxes_page" get_the_ID() is available and in the codex is written this:

add_meta_boxes 
Runs when "edit post" page loads.

Which hook is "edit post" page loads?

https://codex.wordpress.org/Plugin_API/Action_Reference#Administrative_Actions

Upvotes: 0

Views: 179

Answers (1)

Lorraine
Lorraine

Reputation: 486

Solution:

Remove showSection method from constructor and add it inside addMetabox:

function __construct() {

  $this->title = get_post_meta(get_the_ID(), 'lorraine_landing_title', true);
  $this->subtitle = get_post_meta(get_the_ID(), 'lorraine_landing_subtitle', true);

  $this->addAction('add_meta_boxes_page', array($this, 'addMetaBox') );
  $this->addAction('save_post_page', array($this, 'saveMetaBox') );

}

function addMetaBox() {
  if ($this->showSection()) {
    add_meta_box('lorraine-admin-landing', __('Landing'), array($this, 'printTemplate'), 'page', 'normal', 'default');
  }
}

Upvotes: 0

Related Questions