admin管理员组文章数量:1331849
I was wondering how I would go about adding an option to add a site disclaimer or message for a published post in Word Press.
Something like this (Please See Below)
Which by checking the box will result in it being in the HEADER of the site (Please See Below)
Below I have the HTML code but I am not sure WHERE I would need to input the code so it would result in me being able to have the option to check it when creating and publishing a post.
*** <div class="main-wrap">
<div class="site_header" style="background-color:;">
<div class="wrap">
<div class="site_text" style="color:; font-size:14px; line-height:14px; text-align:center; padding:5px 0;">SITE</div>
</div>
</div>
<div id="main-head" class="main-head">
<div class="wrap"> ***
I was wondering how I would go about adding an option to add a site disclaimer or message for a published post in Word Press.
Something like this (Please See Below)
Which by checking the box will result in it being in the HEADER of the site (Please See Below)
Below I have the HTML code but I am not sure WHERE I would need to input the code so it would result in me being able to have the option to check it when creating and publishing a post.
*** <div class="main-wrap">
<div class="site_header" style="background-color:;">
<div class="wrap">
<div class="site_text" style="color:; font-size:14px; line-height:14px; text-align:center; padding:5px 0;">SITE</div>
</div>
</div>
<div id="main-head" class="main-head">
<div class="wrap"> ***
Share
Improve this question
edited Jul 26, 2020 at 1:05
Nik
asked Jul 23, 2020 at 7:45
NikNik
11 bronze badge
1 Answer
Reset to default 0The thing you're talking about is called a custom meta box. They can be used to save post specific settings and data. You can create your own meta boxes with the help of add_meta_box()
function.
Here's a quick example. This would go to your (child) theme's functions.php
file. Alternatively if you want to make the code theme independent, then create a custom plugin for the code below. The example works in both Classic and Block editors.
// Register metabox only for "post" post type
add_action('add_meta_boxes_post', 'my_prefix_register_my_custom_metabox', 10, 1);
function my_prefix_register_my_custom_metabox( $post ) {
add_meta_box(
'my-metabox', // id
esc_html__( 'My Meta Box', 'textdomain' ), // title
'my_prefix_render_my_custom_metabox', // callback
'post', // screen - typically post type
'side', // context - normal, side, advanced
'high', // priority - high, low
array() // optional callback args
);
}
// Callback function that add_meta_box uses to render the contents of the metabox
function my_prefix_render_my_custom_metabox( $post, $callback_args ) {
$checked = get_post_meta( $post->ID, '_my_checkbox_value', true );
wp_nonce_field( 'my_custom_metabox_nonce_action_' . $post->ID, 'my_custom_metabox_nonce' );
?>
<label>
<input type="checkbox" name="_my_checkbox_value" value="1" <?php checked( $checked, 1); ?>>
<span><?php esc_html_e( 'Display disclaimer', 'textdomain' ); ?></span>
</label>
<?php
}
// Callback for saving the metabox form fields when the post is saved
// Executes only when saving "post" post type
add_action( 'save_post_post', 'my_prefix_save_my_custom_metabox', 10, 3 );
function my_prefix_save_my_custom_metabox( $post_id, $post, $update ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if (
wp_is_post_autosave( $post_id ) ||
wp_is_post_revision( $post_id )
) {
return;
}
if (
empty( $_POST['my_custom_metabox_nonce'] ) ||
! wp_verify_nonce( $_POST['my_custom_metabox_nonce'], 'my_custom_metabox_nonce_action_' . $post_id )
) {
return;
}
if ( isset( $_POST['_my_checkbox_value'] ) ) {
update_post_meta( $post_id, '_my_checkbox_value', 1 );
} else {
delete_post_meta( $post_id, '_my_checkbox_value', 1 );
}
}
You can also add to the same file a helper function which takes care of displaying the disclaimer message.
function my_prefix_display_disclaimer() {
if (
is_single() &&
get_post_meta( get_the_ID(), '_my_checkbox_value', true )
) {
?>
<aside class="disclaimer">
<p>This is a disclaimer.</p>
</aside>
<?php
}
}
You can then either use the helper function directly in some (child) theme template file.
<?php my_prefix_display_disclaimer(); ?>
Or hook it into an action hook that your theme might provide.
// in functions.php or custom plugin file
add_action( 'some_theme_template_action_hook', 'my_prefix_display_disclaimer' );
本文标签: customizationAdding an option to post editor to show a site disclaimer or message
版权声明:本文标题:customization - Adding an option to post editor to show a site disclaimer or message 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742232765a2437524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论