Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values in meta boxes are never saved for new posts #4183

Closed
BruceOnTheGo opened this issue Dec 27, 2017 · 1 comment · Fixed by #4184
Closed

Default values in meta boxes are never saved for new posts #4183

BruceOnTheGo opened this issue Dec 27, 2017 · 1 comment · Fixed by #4184
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen

Comments

@BruceOnTheGo
Copy link

BruceOnTheGo commented Dec 27, 2017

Issue Overview

When a plugin adds a meta box to the editor and the meta box has some default values the default values are not saved when creating a new post. The values are only saved if the default values are changed. This is not the normal behaviour with the classic editor. The default values are always saved.

Using master branch.

Steps to Reproduce (for bugs)

  1. Use the following plugin code
  2. Click on new page
  3. Add a title
  4. Publish the post
<?php
/*
Plugin Name: Example for meta box with gutenberg
Author: OnTheGoSystems
Version: 1.0
*/

global $pagenow;

if ( in_array( $pagenow, array( 'post.php', 'post-new.php', 'edit.php' ), true ) ) {
	add_action( 'admin_head', 'gutenberg_new_post_add_meta_box' );
}

function gutenberg_new_post_add_meta_box() {
	add_meta_box( 'gutenberg_div', 'Options', 'gutenberg_meta_box', null, 'side', 'high' );
}

function gutenberg_meta_box() {
	?>
		<select name="gutenberg_select_test">
			<option value="1">Value 1</option>
			<option value="2">Value 2</option>
			<option value="3">Value 3</option>
		</select>

	<?php
}

add_action( 'save_post', 'gutenberg_save_post_example' );

function gutenberg_save_post_example( $post_id ) {

	if ( isset( $_POST['gutenberg_select_test'] ) ) {
		update_post_meta( $post_id, 'gutenberg_select_test', $_POST['gutenberg_select_test'] );
	}
}

Expected Behavior

The 'save_post' action should be triggered and the update_post_meta function is called. This is what happens with the classic editor

Current Behavior

The 'save_post' action is not triggered and the post meta is not updated.

Possible Solution

I think all meta boxes should be considered as 'dirty' for new posts and all the meta box data should be submitted when saving the initial post.

@BruceOnTheGo
Copy link
Author

Possibly related to #3730

@Soean Soean added the [Feature] Meta Boxes A draggable box shown on the post editing screen label Dec 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants