我的第一篇帖子。
我想问一问,是否有人可以看看这个。
它应该是什么样子:,我希望在创建新页面时,在Wordpress管理中包含复选框的自定义元框。如果我选中这个复选框并发布页面,并且有人希望看到它,弹出的18+确认就会出现。
我试图找出如何连接它或让它分配一个属性,它将有弹出确认内置。
这里是我到目前为止所拥有的:(感谢wordpress代码)
function adult_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'adult_sectionid',__( 'Adult content', 'adult_textdomain' ), 'adult_meta_box_callback', $screen, 'side' );
}
}
add_action( 'add_meta_boxes', 'adult_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function adult_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'adult_meta_box', 'adult_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="adult_new_field">';
_e( '<input type="checkbox" name="Adult" value="adult-content">' . " 18+" );
echo '</label> ';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function adult_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['adult_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['adult_meta_box_nonce'], 'adult_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['adult_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['adult_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'adult_save_meta_box_data' );
?>正如你所看到的,到目前为止这是一场混乱。我将感激任何帮助或改进。
发布于 2014-09-03 18:25:54
我建议您使用高级自定义字段-plugin来为页面和帖子制作metabox。你可以在他们的网站http://www.advancedcustomfields.com/上读到更多关于ACF的信息。
对于这个Adult page输入,您可能要检查这个http://www.advancedcustomfields.com/resources/true-false/
使用高级自定义字段,您可以很容易地在管理面板上创建那些元数据。例如,您可以创建标记为Adult page的复选框,其中有键adult_page。然后您可以打开主题header.php并添加以下内容:
<?php if ( get_field( "adult_page" ) ) : ?>
<script type="text/javascript">
if ( window.confirm( 'Are you older than 18?' ) ) {
// user pressed 'ok'
} else {
// user pressed 'cancel'
}
</script>
<?php endif; ?>https://stackoverflow.com/questions/25650512
复制相似问题