Last active
April 2, 2022 16:50
-
-
Save kalamun/fabce318700e61db5b0bb137032c10b7 to your computer and use it in GitHub Desktop.
Add subtitle to wordpress pages, just after the title field nd before the content editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
add to functions.php | |
then get the subtitle with the_subtitle() or get_the_subtitle() functions | |
*/ | |
/* add subtitles */ | |
add_action( "admin_footer-post.php", 'move_subtitle_after_title' ); | |
add_action( "admin_footer-post-new.php", 'move_subtitle_after_title' ); | |
function move_subtitle_after_title() | |
{ | |
// Check post type | |
if( | |
'page' != get_post_type() | |
&& 'post' != get_post_type() | |
// add custom types | |
) | |
return; | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready( function($) | |
{ | |
$( '#custom_subtitle_metabox .inside' ).insertAfter( '#titlewrap' ); | |
$( '#custom_subtitle_metabox' ).css( 'display', 'none' ); | |
}); | |
</script> | |
<?php | |
} | |
function custom_add_meta_boxes_page() { | |
add_meta_box('custom_subtitle_metabox', 'Sottotitolo', 'custom_subtitle_metabox', '', 'normal', 'high'); | |
} | |
add_action( 'add_meta_boxes_page', 'custom_add_meta_boxes_page' ); | |
add_action( 'add_meta_boxes_post', 'custom_add_meta_boxes_page' ); | |
// add custom types | |
function custom_subtitle_metabox() { | |
global $post; | |
wp_nonce_field( 'custom_subtitle', 'custom_subtitle_nonce' ); ## Create nonce | |
$subtitle = get_post_meta($post->ID, 'subtitle', true); ## Get the subtitle | |
?> | |
<p> | |
<label for="sub_title">Sottotitolo</label> | |
<input type="text" name="subtitle" id="subtitle" class="widefat" value="<?= esc_attr( $subtitle ); ?>" /> | |
</p> | |
<?php | |
} | |
function custom_subtitle_save_meta( $post_id ) { | |
global $post; | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return false; ## Block if doing autosave | |
if( !isset($_POST['custom_subtitle_nonce']) ) | |
return false; | |
if ( !current_user_can( 'edit_post', $post_id ) ) | |
return $post_id; ## Block if user doesn't have priv | |
if ( !wp_verify_nonce( $_POST['custom_subtitle_nonce'], 'custom_subtitle' )) { | |
echo 'nonce non valido'; | |
} elseif( isset( $_POST['subtitle'] ) ) { | |
update_post_meta($post_id, 'subtitle', $_POST['subtitle']); | |
} | |
return false; | |
} | |
add_action( 'save_post', 'custom_subtitle_save_meta' ); | |
add_action( 'publish_post', 'custom_subtitle_save_meta' ); | |
add_action( 'draft_post', 'custom_subtitle_save_meta' ); | |
add_action( 'future_post', 'custom_subtitle_save_meta' ); | |
add_action( 'pre_post_update', 'custom_subtitle_save_meta' ); | |
function the_subtitle( $before, $after, $echo ) | |
{ | |
$subtitle = $before . get_post_meta(get_the_ID(), 'subtitle', true) . $after; | |
if($echo) echo $subtitle; | |
else return $subtitle; | |
} | |
function get_the_subtitle() | |
{ | |
return the_subtitle( '', '', false ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment