On the admin add/edit post page, I have a new meta box with submit button. I want to submit them separately. For example if some one click on Publish / update button then it save all post content and when some one click on meta box submit button then it should save meta box fields in a new DB table. Code is attached.
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'Copy', 'cd_meta_box_cb', 'post', 'side', 'high', $post->ID);
}
function cd_meta_box_cb( $post )
{
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
global $wpdb, $wp_query;
$wpdb->show_errors();
$table = $wpdb->base_prefix . 'post_copy';
$parent_post_id = $post->ID;
$parent_blog_id = get_current_blog_id();
$query= "SELECT *
FROM {$table}
WHERE child_post_id= '".$parent_post_id."'
AND child_blog_id = '".$parent_blog_id."'
";
$resultset = $wpdb->get_results($query);
if(!empty ($resultset)){
foreach ( $resultset as $result )
{
$copy_type = $result->copy_type;
}
}
$if_post_child = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} where child_post_id = '".$post->ID."' and child_blog_id ='".$parent_blog_id."' and copy_type = '".$copy_type."'");
if( $if_post_child == 0 ){
?>
<div id="scrollbox">
<input type="hidden" value="<?php echo $parent_blog_id ?>" name= "parent_blog_id">
<ul>
<?php $user_id = get_current_user_id();
$user_blogs = get_blogs_of_user( $user_id );
//echo "<pre>"; print_r($user_blogs); echo "</pre>";
foreach ($user_blogs as $user_blog) {
$child_blog_id = $user_blog ->userblog_id;
$if_child = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} where parent_post_id = '".$parent_post_id."' and child_blog_id ='".$child_blog_id."'");
$querystr = "SELECT *
FROM {$table}
WHERE parent_blog_id= '".$parent_blog_id."'
AND parent_post_id = '".$parent_post_id."'
AND child_blog_id = '".$child_blog_id."'
";
$result = $wpdb->get_results($querystr);
if(!empty ($result)){
foreach ( $result as $res )
{
$sel_child_blog_id = $res->child_blog_id;
$set_post_type = $res->copy_type;
}
}
//print_r($result);
if($if_child == 0){
?>
<li style="margin-bottom:10px;"><label for="all_blog_name">
<input type="hidden" name="blog_check_name[]" id="blog_check_name_<?php echo $user_blog ->userblog_id ?>" value="<?php echo $user_blog ->blogname; ?>"/>
<input class="chk-copy" type="checkbox" name="blog_check[]" id="blog_check_<?php echo $user_blog ->userblog_id ?>" value="<?php echo $user_blog ->userblog_id; ?>" />
<span>
<?php
echo $user_blog->blogname;
?>
</span>
<br> <input type="radio" name="child_post_<?php echo $user_blog ->userblog_id; ?>" id="child_post_<?php echo $user_blog ->userblog_id; ?>"checked value="draft" /> Draft
<input type="radio" name="child_post_<?php echo $user_blog ->userblog_id; ?>" id="child_post_<?php echo $user_blog ->userblog_id; ?>" value="publish" /> Published
<input type="radio" name="child_post_<?php echo $user_blog ->userblog_id; ?>" id="child_post_<?php echo $user_blog ->userblog_id; ?>" value="future" /> Scheduled
<br>
</li>
<?php
} else { ?>
<input type="hidden" name="blog_check_name[]" id="blog_check_name_<?php echo $user_blog ->userblog_id ?>" value="<?php echo $user_blog ->blogname; ?>"/>
<li style="margin-bottom:10px;"><label for="all_blog_name">
<?php
if($sel_child_blog_id == $user_blog ->userblog_id){
$chked = "checked";
}
?>
<input class="chk-copy" type="checkbox" name="blog_check[]" id="blog_check_<?php echo $user_blog ->userblog_id ?>" value="<?php echo $user_blog ->userblog_id; ?>" <?php echo $chked ?>/>
<span>
<?php
echo $user_blog->blogname;
?>
</span>
<br> <input type="radio" name="child_post_<?php echo $user_blog ->userblog_id; ?>" id="child_post_<?php echo $user_blog ->userblog_id; ?>" value="update" checked /> Update
<input type="radio" name="child_post_<?php echo $user_blog ->userblog_id; ?>" id="child_post_<?php echo $user_blog ->userblog_id; ?>" value="no_update" /> No Update
<br>
</li>
<?php } // End If
} // End For
?>
</label>
</ul>
</div>
<div class="btncopy">
<?php
$if_not_parent = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} where parent_post_id = '".$parent_post_id."'");
if($if_not_parent == 0){
submit_button( __( 'Copy', 'Copy-post' ), 'button button-primary button-large', 'copy-post', false );
}else{
?>
<input type="button" name="copy-post" id="copy-post" class="button button-primary button-large" value="Copy" onClick ="openbox_parent('Copy Confirm', 1)" />
<?php } ?>
</div>
<input type="hidden" name="copy-post" id="copy-post" class="button button-primary button-large" value="Copy" />
<div id="shadowing-parent"></div>
<div id="box-parent">
<span id="boxtitle-parent"></span>
<label for="confirmation_copy">
<p> Update the following child posts : </p>
<div id="checkboxContainer"></div>
<p>
<input type="submit" name="Copy" value="Copy">
<input type="button" name="cancel" value="Cancel" onClick="closebox_parent()">
</p>
</label>
<!--/form-->
</div>
<?php
} // End of $if_post_child
else if ($if_post_child > 0 && $copy_type == 'child_linked'){
echo "This is a child post of CEBBlogs. Any edits made here will break the link with the parent post and will disable it from being updated in future by the parent.
To maintain the link with the Parent post please make all edits at the Parent post and copy them down.<br><br>";
echo "<a href='#'>View Parent Post</a>";
}else if ($if_post_child > 0 && $copy_type == 'child_unlinked'){
echo "This is a child post of CEBBlogs. It has been updated and is no longer linked to the parent post.<br><br>";
echo "<a href='#'>View Parent Post</a>";
}
}
Now I called the save_post
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
//if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
$registerFormParameterHandler = new FormParameterHandler($_POST);
if($_POST['copy-post'] == 'Copy'){
echo 'here';
global $wpdb, $wp_query;
$wpdb->show_errors();
$id = $wp_query->post->ID; // get post ID
$table = $wpdb->base_prefix . 'post_copy';
// update popularpostsdata table
$blog_checked = $_POST[blog_check];
for ($i =0; $i < count($blog_checked); $i++){
$child_blog_id = $blog_checked[$i];
// $child_blog_type = Published, Draft, Schedule, update, No Update
$child_blog_type = $_POST['child_post_'.$child_blog_id];
$parent_blog_id = $_POST['parent_blog_id'];
$parent_post_id = $_POST['post_ID'];
// Date time for New Inserted post
$post_date = date("Y-m-d h:m:s");
$post_date_gmt = gmdate("Y-m-d h:m:s");
// If the post is Published, Draft, zzzzzz,--- Copy content
if($child_blog_type =='publish' || $child_blog_type =='draft' || $child_blog_type =='future')
{
$vals = sprintf("'%s', '%s', '%s', '%s', '%s', '%s',
'%s', '%s', '%s', '%s','%s', '%s', '%s',
'%s', '%s', '%s',
'%s', '%s', '%s', '%s','%s', '%s'",
mysql_real_escape_string($registerFormParameterHandler->post_author), mysql_real_escape_string($post_date), mysql_real_escape_string($post_date_gmt), mysql_real_escape_string($registerFormParameterHandler->post_content), mysql_real_escape_string($registerFormParameterHandler->post_title), mysql_real_escape_string($registerFormParameterHandler->post_excerpt),
mysql_real_escape_string($child_blog_type), mysql_real_escape_string($registerFormParameterHandler->comment_status), mysql_real_escape_string($registerFormParameterHandler->ping_status), mysql_real_escape_string($registerFormParameterHandler->post_password), mysql_real_escape_string($registerFormParameterHandler->post_name), mysql_real_escape_string($registerFormParameterHandler->to_ping), mysql_real_escape_string($registerFormParameterHandler->pinged),
mysql_real_escape_string($registerFormParameterHandler->post_modified), mysql_real_escape_string($registerFormParameterHandler->post_modified_gmt), mysql_real_escape_string($registerFormParameterHandler->post_content_filtered),
mysql_real_escape_string($registerFormParameterHandler->post_parent), mysql_real_escape_string($registerFormParameterHandler->guid), mysql_real_escape_string($registerFormParameterHandler->menu_order), mysql_real_escape_string($registerFormParameterHandler->post_type), mysql_real_escape_string($registerFormParameterHandler->post_mime_type), mysql_real_escape_string($registerFormParameterHandler->comment_count)
);
$cols = "post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,
post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged,
post_modified, post_modified_gmt, post_content_filtered,
post_parent, guid, menu_order, post_type, post_mime_type, comment_count";
echo $sql = "insert into cebblogs_".$child_blog_id."_posts (".$cols.") values(".$vals.")";
$wpdb->query ($sql);
$new_post_id = mysql_insert_id();
}else if($child_blog_type =='update'){
// Code will go here
$get_child_post_id = "Select child_post_id from {$table} where parent_post_id = {$parent_post_id} and child_blog_id = {$child_blog_id} and copy_type= 'child_linked'";
$result = $wpdb->get_results($get_child_post_id);
if(!empty ($result)){
foreach ( $result as $res )
{
$child_post_id = $res->child_post_id;
}
}
$vals = sprintf("'%s','%s', '%s', '%s', '%s', '%s', '%s',
'%s', '%s', '%s', '%s','%s', '%s', '%s',
'%s', '%s', '%s',
'%s', '%s', '%s', '%s','%s', '%s'",
mysql_real_escape_string($child_post_id), mysql_real_escape_string($registerFormParameterHandler->post_author), mysql_real_escape_string($post_date), mysql_real_escape_string($post_date_gmt), mysql_real_escape_string($registerFormParameterHandler->post_content), mysql_real_escape_string($registerFormParameterHandler->post_title), mysql_real_escape_string($registerFormParameterHandler->post_excerpt),
mysql_real_escape_string($child_blog_type), mysql_real_escape_string($registerFormParameterHandler->comment_status), mysql_real_escape_string($registerFormParameterHandler->ping_status), mysql_real_escape_string($registerFormParameterHandler->post_password), mysql_real_escape_string($registerFormParameterHandler->post_name), mysql_real_escape_string($registerFormParameterHandler->to_ping), mysql_real_escape_string($registerFormParameterHandler->pinged),
mysql_real_escape_string($registerFormParameterHandler->post_modified), mysql_real_escape_string($registerFormParameterHandler->post_modified_gmt), mysql_real_escape_string($registerFormParameterHandler->post_content_filtered),
mysql_real_escape_string($registerFormParameterHandler->post_parent), mysql_real_escape_string($registerFormParameterHandler->guid), mysql_real_escape_string($registerFormParameterHandler->menu_order), mysql_real_escape_string($registerFormParameterHandler->post_type), mysql_real_escape_string($registerFormParameterHandler->post_mime_type), mysql_real_escape_string($registerFormParameterHandler->comment_count)
);
$cols = "ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt,
post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged,
post_modified, post_modified_gmt, post_content_filtered,
post_parent, guid, menu_order, post_type, post_mime_type, comment_count";
//echo "<pre>";print_r($child_post_id ); echo "</pre>";
$sql = "REPLACE INTO cebblogs_".$child_blog_id."_posts (".$cols.") values(".$vals.")";
$wpdb->query ($sql);
}
$numberofpost = $wpdb->get_var( "SELECT COUNT(*) FROM {$table} where parent_blog_id = '".$parent_blog_id."' and parent_post_id = '".$parent_post_id."' and child_blog_id ='".$child_blog_id."'");
if($numberofpost == 0) {
$result = $wpdb->query("INSERT INTO {$table} VALUES ('' , '".$parent_blog_id."', '".$parent_post_id."', '".$child_blog_id."', '".$new_post_id."', 'child_linked', '2013-04-09 07:00:00', '2013-04-17 06:00:00')");
}
}
}
exit;
}
This all code fine except two things:
1. It execute query two times in save_post.
2. All code execute either you press publish button or you press meta box submit button
IMO, it’s much better to solve this with Ajax. Maybe it’s possible the way you’re proposing, but it seems a bit confusing from an User Experience perspective: two similar buttons that do different things.
What if the user modifies the content and the custom fields, updates one and loses the other? Maybe the
save_post
action hook should be just a backup function.It can be implemented with the following steps covering just the Ajax side. You’ll find a full Ajaxified meta box here: Unattaching images from a post
1) Enqueue and localize the Javascript
We can pass any value to JS in the array, like
'custom_value' = $something
and retrieve it in JS withwp_ajax.custom_value
. The second parameter ofwp_localize_script
,wp_ajax
, is the object name to be referenced in JS.2) Add the Ajax function that will be called by JS
Here we’ll save our data based on the data sent by the Ajax call in the Javascript file. First, we do a security check. Then check if the correct
$_POST
data was sent. And finally do our stuff.I’m not sure if
global $post; $post->ID
is available at this point. If not, it has to be grabbed and sent with jQuery/JS.3) Javascript/jQuery file
Here, we detect that our custom Update button was pressed. Grab the information from the input fields and send it to the previous Ajax function.
This is the file
wpse_97845.js
that’s alongside the plugin file.[Update]
Working example
Check code comments.
mu-copy-post.php
mu-copy-post.js