'top', // Options: top, bottom, or shortcode 'label_text' => __( 'Rating:', 'author-post-ratings' ), 'post_types_enabled' => array( 'post' ) ); return $defaults; } function pn_apr_load_textdomain() { load_plugin_textdomain( 'author-post-ratings', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } add_action( 'plugins_loaded', 'pn_apr_load_textdomain' ); /* Load settings from database; If no settings have been saved, use default settings and update database. */ function pn_apr_settings_init() { // Make settings array global global $pn_apr_settings; // Load saved settings from the database, if they exist $pn_apr_settings = get_option( PN_APR_SETTINGS_KEY ); // If there were no settings saved, get defaults and save them. if ( false == $pn_apr_settings ) { $pn_apr_settings = pn_apr_get_defaults(); update_option( PN_APR_SETTINGS_KEY, $pn_apr_settings ); } } add_action( 'after_setup_theme', 'pn_apr_settings_init' ); function pn_apr_enqueue_css() { wp_enqueue_style( 'author-post-ratings', PN_APR_PLUGIN_DIR_URL . 'author-post-ratings.css' ); } add_action( 'wp_enqueue_scripts', 'pn_apr_enqueue_css' ); function get_author_post_rating( $post_id = null ) { $rating = (int) get_post_meta( $post_id, PN_APR_RATING_META_KEY, true ); return $rating; } function the_author_post_rating( $post_id = null, $return = false ) { // if no post ID is sent, try to obtain it ourselves. if ( $post_id == null ) $post_id = get_the_ID(); $rating = get_author_post_rating( $post_id ); if ( $rating ) { $settings = get_option( PN_APR_SETTINGS_KEY ); $output = null; $output .= '
'; $output .= '' . esc_attr( $settings['label_text'] ) . ' '; $output .= ''; // Output active stars for ( $i = 1; $i <= $rating; $i++ ) { $output .= ''; } // Output inactive stars for ( $i = $rating + 1; $i <= 5; $i++ ) { $output .= ''; } $output .= '' . "\n"; $output .= '
'; if ( true == $return ) { return $output; } // We don't need to use "else" here, since calling return will automatically stop further execution of this function. echo $output; } } /* Add APR shortcode to top or bottom of post, depending on settings. I used shortcodes instead of directly prepending/appending the APR output to the content so that the post rating doesn't appear in post excerpts. That would be bad, because WordPress would strip out the APR star markup, leaving only the label text with no stars. */ function pn_apr_the_content_filter( $content ) { $settings = get_option( PN_APR_SETTINGS_KEY ); $post_type = get_post_type( get_the_ID() ); // Only add the rating shortcode if this post type is enabled in the settings. A user may have added a rating to a post, and then turned off the rating for that post type. if ( in_array( $post_type, $settings['post_types_enabled'] ) ) { if ( 'top' == $settings['position_on_post'] ) { $content = '[author-post-rating]' . $content; } elseif ( 'bottom' == $settings['position_on_post'] ) { $content .= '[author-post-rating]'; } } return $content; } add_filter( 'the_content', 'pn_apr_the_content_filter', 10 ); function pn_apr_shortcode() { return the_author_post_rating( get_the_ID(), true ); } add_shortcode( 'author-post-rating', 'pn_apr_shortcode' ); /* Add the APR meta box to the edit screen for all enabled post types */ function pn_apr_add_meta_boxes() { $settings = get_option( PN_APR_SETTINGS_KEY ); foreach ( $settings['post_types_enabled'] as $post_type_name ) { add_meta_box( 'pn_apr_meta_box', __( 'Post Rating', 'author-post-ratings' ), 'pn_apr_meta_box_form', $post_type_name, 'side', 'default' ); } } add_action( 'add_meta_boxes', 'pn_apr_add_meta_boxes' ); /* Output the content of the APR meta box */ function pn_apr_meta_box_form( $post ) { wp_nonce_field( 'pn_apr_meta_box_nonce', 'pn_apr_meta_box_nonce_field' ); $current_post_rating = get_post_meta( $post->ID, PN_APR_RATING_META_KEY, true ); echo ' '; echo ''; } function pn_apr_save_meta_box_data( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( !isset( $_POST['pn_apr_meta_box_nonce_field'] ) || !wp_verify_nonce( $_POST['pn_apr_meta_box_nonce_field'], 'pn_apr_meta_box_nonce' ) ) return; if ( !current_user_can( 'edit_post', $post_id ) ) return; $post_rating = $_POST['pn_apr_rating']; if ( $post_rating == 'unrated' ) { // Delete the post rating custom field value if the post is unrated delete_post_meta( $post_id, PN_APR_RATING_META_KEY ); } else { update_post_meta( $post_id, PN_APR_RATING_META_KEY, $post_rating ); } } add_action( 'save_post', 'pn_apr_save_meta_box_data', 10, 2 ); function pn_apr_admin_init() { register_setting( 'pn_apr_settings_fields', PN_APR_SETTINGS_KEY, 'pn_apr_settings_validation' ); add_settings_section( 'position', __('Position', 'author-post-ratings'), '__return_false', 'pn_apr_settings' ); add_settings_section( 'appearance', __('Appearance', 'author-post-ratings'), '__return_false', 'pn_apr_settings' ); add_settings_section( 'post-types', __('Post Types', 'author-post-ratings'), '__return_false', 'pn_apr_settings' ); add_settings_field( 'position_on_post', __('Rating position on post', 'author-post-ratings'), 'pn_apr_settings_field_position_on_post', 'pn_apr_settings', 'position' ); add_settings_field( 'label_text', __('Label text', 'author-post-ratings'), 'pn_apr_settings_field_label_text', 'pn_apr_settings', 'appearance' ); add_settings_field( 'post_types', __('Post types enabled', 'author-post-ratings'), 'pn_apr_settings_field_post_types', 'pn_apr_settings', 'post-types' ); } add_action( 'admin_init', 'pn_apr_admin_init' ); /* Init the settings page */ function pn_apr_settings_page_init() { add_options_page( __('Author Post Ratings Settings', 'author-post-ratings' ), __('Author Post Ratings', 'author-post-ratings' ), 'manage_options', 'author-post-ratings', 'pn_apr_settings_page_content' ); } add_action( 'admin_menu', 'pn_apr_settings_page_init' ); function pn_apr_settings_page_content() { ?>

Author Post Ratings is a free plugin developed by Philip Newcomer. If you find it useful, please consider donating so that I can dedicate a larger portion of my time to writing more plugins and providing support to users. All donations, no matter how large or small, are greatly appreciated!', 'author-post-ratings'), 'http://philipnewcomer.net', 'http://philipnewcomer.net/donate/' ); ?>

[author-post-rating]', 'http://philipnewcomer.net/wordpress-plugins/author-post-ratings/'); ?>

Settings Debugging Info
'; print_r( get_option( PN_APR_SETTINGS_KEY ) ); echo '
'; ?>
$post_type_value ) { if ( $post_type_value == 'on' ) { $validated['post_types_enabled'][] = $post_type_name; } } } return $validated; } function pn_apr_settings_field_position_on_post() { $settings = get_option( PN_APR_SETTINGS_KEY ); ?> >
>
> true, '_builtin' => false ) ); $post_types_available = array_merge( $builtin_post_types, $additional_post_types ); foreach ( $post_types_available as $post_type ) { echo '
'; echo '
'; } }