Last active
December 12, 2015 09:19
-
-
Save enigmaticape/4751121 to your computer and use it in GitHub Desktop.
Wordpess plugin that adds an extra post meta box for a short (or just additional) excerpt. Bung it in your wp-content/plugins directory, get the short excerpt using : get_post_meta($post->ID, "ape_short_excerpt", true) ) :
This file contains hidden or 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
<?php | |
/* | |
Plugin Name: Add Short Excerpt Meta | |
Plugin URI: https://gist.github.com/enigmaticape/4751121 | |
Description: Add an additional excerpt box for front page | |
Version: 1.0 | |
Author: Steve Trewick | |
Author URI: http://www.enigmaticape.com | |
License: Public Domain | |
*/ | |
function ape_save_post_class_meta( $post_id, $post ) { | |
// Verify nonce, user perms | |
if ( !isset( $_POST['ape_short_nonce'] ) || | |
!wp_verify_nonce( $_POST['ape_short_nonce'],basename( __FILE__ ))) | |
return $post_id; | |
$post_type = get_post_type_object( $post->post_type ); | |
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) | |
return $post_id; | |
$new_meta_value = (isset( $_POST['ape-post-class'] ) ? $_POST['ape-post-class'] : ''); | |
$meta_key = 'ape_short_excerpt'; | |
$meta_value = get_post_meta( $post_id, $meta_key, true ); | |
// CREATE | |
if ( $new_meta_value && ('' == $meta_value) ) { | |
add_post_meta( $post_id, $meta_key, $new_meta_value, true ); | |
} // UPDATE | |
elseif ( $new_meta_value && ($new_meta_value != $meta_value) ) { | |
update_post_meta( $post_id, $meta_key, $new_meta_value ); | |
} // DELETE | |
elseif ( ('' == $new_meta_value) && $meta_value ) { | |
delete_post_meta( $post_id, $meta_key, $meta_value ); | |
} | |
} | |
function ape_short_excerpt_class_meta_box( $object, $box ) { | |
$label_txt = _e( "<p>Short excerpt to be displayed on front page.</p>", 'example' ); | |
echo wp_nonce_field( basename( __FILE__ ), 'ape_short_nonce', true, false ); | |
echo '<label for="ape-post-class">'. $label_txt . '</label>'; | |
echo '<textarea name="ape-post-class"'. | |
' class="widefat"'. | |
' id="ape-post-class"'. | |
' rows="6"'. | |
' cols = "40">'. | |
esc_attr( | |
get_post_meta( $object->ID, 'ape_short_excerpt', true ) | |
) . | |
'</textarea>'; | |
} | |
function ape_add_post_meta_boxes() { | |
add_meta_box( | |
'ape-post-class', | |
esc_html__( 'Short Excerpt' ), | |
'ape_short_excerpt_class_meta_box', | |
'post', | |
'normal', | |
'default' | |
); | |
} | |
function ape_short_excerpt_meta_boxes_setup() { | |
add_action( 'add_meta_boxes', 'ape_add_post_meta_boxes' ); | |
add_action( 'save_post', 'ape_save_post_class_meta', 10, 2 ); | |
} | |
add_action( 'load-post.php', 'ape_short_excerpt_meta_boxes_setup' ); | |
add_action( 'load-post-new.php', 'ape_short_excerpt_meta_boxes_setup' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment