我正在阅读save_post Codex 我发现您需要在编辑窗口中检查$\\u请求[\'post\\u meta\\u data\',以连接到save\\u post。
我修改了示例代码:
function av_subscribe_set_parent($post_id){
/* in production code, $slug should be set only once in the plugin,
preferably as a class property, rather than in each function that needs it.
*/
$slug = \'page\';
/* check whether anything should be done */
$_POST += array("{$slug}_edit_nonce" => \'\');
if ( $slug != $_POST[\'post_type\'] ) {
return;
}
if ( !current_user_can( \'edit_post\', $post_id ) ) {
return;
}
if ( !wp_verify_nonce( $_POST["{$slug}_edit_nonce"],
plugin_basename( __FILE__ ) ) )
{
return;
}
/* Request passes all checks; update the post\'s metadata */
if ($_REQUEST[\'_wp_page_template\'] == \'av_comment_subscribe.php\') {
update_post_meta($post_id, \'post_parent\', 13570);
}
因此,基本上,我要检查页面模板是否设置为自定义模板,如果设置为true,则修改post\\u父元数据。
这不起作用。
SO网友:bueltge
您可以在保存之前检查请求值,并通过wp\\u die()获得提示
add_action( \'save_post\',\'wpse46583_save\', 10, 2 );
function wpse46583_save( $post_id, $post ) {
// verify this is not an auto save routine.
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return;
// You should check nonces and permissions here
if ( ! current_user_can( \'edit_page\', $post_id ) )
return;
if ( $_REQUEST[\'page_template\'] !== \'page-contact.php\' ) {
// No page template assigned - do something here.
wp_die( \'wrong template\' );
}
return;
}
请参见
Gist 3226847