在管理员添加/编辑帖子页面上,我有一个带有提交按钮的新元框。我想单独提交。例如,如果有人单击“发布/更新”按钮,则会保存所有帖子内容;如果有人单击“元框提交”按钮,则会将元框字段保存在新的DB表中。代码已附加。
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>";
}
}
现在我打电话给save\\u 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;
}
这一切代码都很好,除了两件事:1。它在save\\u post中执行查询两次。2、所有代码要么按publish按钮,要么按meta box submit按钮执行
最合适的回答,由SO网友:brasofilo 整理而成
在我看来,用Ajax解决这个问题要好得多。也许你的提议是可能的,但从用户体验的角度来看,这似乎有点令人困惑:两个相似的按钮可以做不同的事情
如果用户修改了内容和自定义字段,更新了其中一个,而丢失了另一个,该怎么办?也许save_post
动作挂钩应该只是一个备份功能。
它可以通过以下步骤来实现,这些步骤只涉及Ajax方面。您可以在此处找到完整的Ajaxified元框:Unattaching images from a post
1) Enqueue and localize the Javascript
我们可以将数组中的任何值传递给JS,如\'custom_value\' = $something
并在JS中使用wp_ajax.custom_value
. 的第二个参数wp_localize_script
, wp_ajax
, 是要在JS中引用的对象名称。
add_action( "admin_print_scripts-post.php", \'enqueue_ajax_wpse_97845\' );
function enqueue_ajax_wpse_97845()
{
// Check post type
global $typenow;
if( \'post\' != $typenow )
return;
wp_enqueue_script( \'wpse_97845_js\', plugins_url( \'/\', __FILE__ ) . \'wpse_97845.js\' );
wp_localize_script(
\'wpse_97845_js\',
\'wp_ajax\',
array(
\'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
\'ajaxnonce\' => wp_create_nonce( \'wpse_97845_validation\' )
)
);
}
2) Add the Ajax function that will be called by JS这里,我们将根据Javascript文件中Ajax调用发送的数据保存数据。首先,我们进行安全检查。然后检查
$_POST
数据已发送。最后做我们的事<我不确定
global $post; $post->ID
此时可用。如果没有,则必须抓取它并用jQuery/JS发送。
add_action( \'wp_ajax_update_metabox_wpse_97845\', \'update_metabox_wpse_97845\' );
function update_metabox_wpse_97845()
{
check_ajax_referer( \'wpse_97845_validation\', \'ajaxnonce\' );
if( !isset( $_POST[\'wpse_97845_custom_field\'] ) ) {
wp_send_json_error( array(
\'error\' => __( \'Post ID not set\' )
));
}
wp_send_json_success( \'Success\' );
}
3) Javascript/jQuery file这里,我们检测到我们的自定义更新按钮被按下。从输入字段中获取信息并将其发送到之前的Ajax函数
这是文件
wpse_97845.js
这是插件文件的旁边。
jQuery(document).ready(function($)
{
/**
* Handle error messages
*/
function wpse_97845_handle_error( error )
{
// Do something
}
/**
* Process Ajax response
*/
function wpse_97845_do_response( response )
{
// Error
if( !response.success )
{
wpse_97845_handle_error( response.data.error );
return;
}
// do something
}
/**
* Ajax button call
*/
$(\'#wpse_97845_update_button\').click(function()
{
origin_value = $(\'#input_field_id\').val();
/* Prepare data */
var data = {
action: \'query_external_site\',
ajaxnonce: wp_ajax.ajaxnonce,
wpse_97845_custom_field: origin_value
};
$.post( wp_ajax.ajaxurl, data, wpse_97845_do_response );
});
});
<小时>
[Update] 工作示例检查代码注释
mu-copy-post.php
<?php
/**
* Plugin Name: Multisite - Copy Post to Other Site
* Plugin URI: https://wordpress.stackexchange.com/q/97845/12615
* Version: 1.0
* Author: brasofilo
* Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
*/
add_action( \'add_meta_boxes\', \'add_custom_box_wpse_94701\' );
add_action( "admin_print_scripts-post.php", \'enqueue_ajax_wpse_97845\' );
add_action( \'wp_ajax_update_metabox_wpse_97845\', \'update_metabox_wpse_97845\' );
function add_custom_box_wpse_94701()
{
add_meta_box(
\'sectionid_wpse_94701\',
__( \'Copy Post\' ),
\'copy_post_box_wpse_94701\',
\'post\',
\'side\'
);
}
function copy_post_box_wpse_94701()
{
global $wpdb,$post;
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = \'{$wpdb->siteid}\'
AND spam = \'0\'
AND deleted = \'0\'
AND archived = \'0\'
AND mature = \'0\'
AND public = \'1\'
");
$original_blog_id = get_current_blog_id();
// Dropdown
echo \'<select name="other-blog-id" id="other-blog-id">
<option value="">- Select -</option>\';
foreach ( $blogs as $blog )
{
// Don\'t display current blog
if( $original_blog_id == $blog->blog_id )
continue;
switch_to_blog($blog->blog_id);
printf(
\'<option value="%d"> %s</option>\',
$blog->blog_id,
get_option(\'blogname\')
);
}
echo \'</select>\';
// Publish button
echo "<a class=\'button-secondary\' href=\'#\' title=\'Publish to other blog\' id=\'publish-to-other-blog\' name=\'$post->ID\'>Publish</a>";
// Error and success messages
echo \'<div class="updated below-h2" id="ajax-success" style="display:none">updated</div>\';
echo \'<div class="form-invalid" id="ajax-error" style="display:none">form-invalid</div>\';
switch_to_blog( $original_blog_id );
}
function enqueue_ajax_wpse_97845()
{
// Check post type
global $typenow;
if( \'post\' != $typenow )
return;
wp_enqueue_script( \'wpse_97845_js\', plugins_url( \'/\', __FILE__ ) . \'mu-copy-post.js\' );
wp_localize_script(
\'wpse_97845_js\',
\'wp_ajax\',
array(
\'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
\'ajaxnonce\' => wp_create_nonce( \'wpse_97845_validation\' )
)
);
}
function update_metabox_wpse_97845()
{
check_ajax_referer( \'wpse_97845_validation\', \'ajaxnonce\' );
if( empty( $_POST[\'wpse_97845_blog_id\'] ) || empty( $_POST[\'wpse_97845_post_id\'] ) ) {
wp_send_json_error( array(
\'error\' => __( \'Blog ID not set\' )
));
}
// Grac actual post data and current blog ID
$actual_post = get_post( $_POST[\'wpse_97845_post_id\'] );
$original_blog_id = get_current_blog_id();
// Prepare cloned post
$copy_post = array(
\'post_title\' => $actual_post->post_title,
\'post_content\' => $actual_post->post_content,
\'post_status\' => $actual_post->post_status,
\'post_type\' => $actual_post->post_type
);
// Switch to destination blog, grab its name and insert cloned post
switch_to_blog($_POST[\'wpse_97845_blog_id\']);
$blog_name = get_option( \'blogname\' );
$result = wp_insert_post($copy_post);
// Restore current blog and return response
switch_to_blog( $original_blog_id );
wp_send_json_success( "Post added to $blog_name with the ID of $result" );
}
mu-copy-post.js
jQuery(document).ready(function($)
{
/**
* Handle error messages
*/
function wpse_97845_handle_error( error )
{
$(\'#ajax-error\').html(error).show();
}
/**
* Process Ajax response
*/
function wpse_97845_do_response( response )
{
// Error
if( !response.success )
{
wpse_97845_handle_error( response.data.error );
return;
}
// Display success response
$(\'#ajax-success\').html(response.data).show();
}
/**
* Ajax button call
*/
$(\'#publish-to-other-blog\').click( function(event)
{
// Block button default
event.preventDefault();
// Clear previous messages
$(\'#ajax-success\').hide();
$(\'#ajax-error\').hide();
// Grab info
blog_id = $(\'#other-blog-id\').val();
post_id = $(\'#publish-to-other-blog\').attr(\'name\');
// Prepare data
var data = {
action: \'update_metabox_wpse_97845\',
ajaxnonce: wp_ajax.ajaxnonce,
wpse_97845_blog_id: blog_id,
wpse_97845_post_id: post_id
};
// Send Ajax request
$.post( wp_ajax.ajaxurl, data, wpse_97845_do_response );
});
});