I switched from intelliJ to VIM a few days ago and I have a few problems with my syntax highlighting. I'm mainly working with php and html and in some files some of the ; are marked with a red background (maybe meaning there is an error in my syntax?). This most likely happens when the ; is followed by a comment //. I'm pretty sure there are no errors...
Also a few files do not provide php syntax highlight at all after a blockcomment /**/ until the php tag ?> gets closed.
Any idea what im missing? Also I'd appreciate suggestions on alternatives for syntax highlighting. It's hard to find useful stuff for webdev since VIM isn't that popular in that sector I guess.
Here my current vim settings:
https://github.com/raQai/.vim
I'd appreciate your help
Edit:
Here is the code thats causing the problems:
<?php namespace KF\LINKS;
defined ( 'ABSPATH' ) or die ( 'nope!' );
/**
* Plugin Name: KF Attachment Links
* Description: Adding attatchemts to post, pages and teams (Kong Foos Team Manager)
* Version: 1.0.0
* Author: Patrick Bogdan
* Text Domain: kfl
* License: GPL2
*
* Copyright 2015 Patrick Bogdan
* TODO: Settings for post_types with checkboxes
*/
new KFLinksMetaBox();
class KFLinksMetaBox {
const kfl_key = 'kf-links-information';
function __construct() {
if (is_admin ()) {
add_action ( 'admin_enqueue_scripts', wp_enqueue_style ( 'kfl-admin-style', plugins_url( 'includes/css/admin-styles.css', plugin_basename( __FILE__ ) ) ) );
add_action ( 'admin_enqueue_scripts', wp_enqueue_script ( 'kfl-admin-js', plugins_url( 'includes/js/kfl-admin-scripts.js', plugin_basename( __FILE__ ) ) ) );
add_action ( 'add_meta_boxes', array( &$this, 'kf_links_meta_box_add' ) );
add_action ( 'save_post', array( &$this, 'kf_links_meta_box_save' ) );
}
register_deactivation_hook( __FILE__, array( &$this, 'kf_links_uninstall' ) );
add_filter( 'the_content', array( $this, 'kf_links_add_to_posts' ) );
}
function kf_links_uninstall() {
delete_post_meta_by_key( self::kfl_key );
}
function kf_links_add_to_posts( $content ) {
$links = $this->kf_links_explode( get_post_meta( get_the_ID(), self::kfl_key, true ) );
if ( $links['is_active'] == '1' && count($links['items']) > 0 ) {
$links_html = '';
if ( !empty($links['title']) ) {
$links_html .= '<strong>' . $links['title'] . '</strong>';
}
foreach ( $links['items'] as $link ) {
if ( !empty( $link['name'] ) && !empty( $link['url'] ) ) {
$links_html .= '<br />» ' . $link['name'] . '';
}
}
if ( !empty( $links_html ) ) {
$content .= '<p class="attachment-links">' . $links_html . '</p>';
}
}
return $content;
}
function kf_links_meta_box_add() {
$screens = array( 'post', 'page', 'teams' );
foreach( $screens as $screen)
{
add_meta_box (
'kf_links_meta_box', // id
'Linksammlung', // title
array( &$this, 'kf_links_meta_box_display' ), // callback
$screen, // post_type
'normal', // context
'high' // priority
);
}
}
function kf_links_meta_box_display( $post ) {
wp_nonce_field( 'kf_links_meta_box', 'kf_links_meta_box_nonce' );
$this->kf_links_meta_box_display_html( $post );
}
function kf_links_meta_box_display_html( $post )
{
$post_string = get_post_meta( $post->ID, self::kfl_key, true );
$links = $this->kf_links_explode( $post_string );
?>
<div class="kf-meta-box-checkbox">
<input onClick="kfl_checkboxDivDisplay( this.id, 'kf-links' ); kfl_creaetLinksString();" <?php if ( $links['is_active'] ) echo 'checked '; ?>type="checkbox" id="kf-links-checkbox" value="1" />
<label id="kf-links-checkbox-label" for="kf-links-checkbox">Linksammlung aktivieren</label>
</div>
<div id="kf-links" <?php if ( !$links['is_active'] ) echo 'style="display:none" '; ?>>
<div class="kf-meta-box-full">
<label for="kf-links-title">Titel der Linksammlung</label>
<input onChange="kfl_creaetLinksString()" id="kf-links-title" value="<?php echo $links['title']; ?>" placeholder="Titel der Linksammlung" />
</div>
<div class="kf-links-header">
<label>Name</label>
<label>URL</label>
</div>
<div id="kf-links-items">
<?php
foreach ( $links['items'] as $ID => $arr ) {
$this->kf_links_item_display_html( $ID, $arr, $links['is_active'] );
}
if ( count( $links['items'] ) < 1 ) {
$this->kf_links_item_display_html( 0, array( 'name' => '', 'url' => '' ), false );
}
?>
</div>
<h4>+ Weiteren Link hinzufügen</h4>
<input type="hidden" id="kf-links-counter" value="<?php echo ( ( count($links['items']) < 1 ) ? 1 : count($links['items']) ); ?>" />
<input type="hidden" name="<?php echo self::kfl_key; ?>" id="<?php echo self::kfl_key; ?>" value="<?php echo $post_string; ?>" />
</div>
<?php
}
function kf_links_item_display_html( $ID, $arr, $is_active )
{
?>
<div id="kf-links-item[<?php echo $ID; ?>]" class="kf-links-item">
<input onChange="kfl_creaetLinksString();" value="<?php echo $arr['name']; ?>" <?php if ( $is_active ) echo 'required '; ?>placeholder="Name" />
<input onChange="kfl_creaetLinksString();" value="<?php echo $arr['url']; ?>" <?php if ( $is_active ) echo 'required '; ?>placeholder="http://..." />
<input onClick="kfl_deleteLink( 'kf-links-item[<?php echo $ID; ?>]' ); kfl_creaetLinksString();" value="✗" type="button" class="button button-small button-primary" />
</div>
<?php
}
function kf_links_meta_box_save($post_id)
{
if ( !isset( $_POST['kf_links_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['kf_links_meta_box_nonce'], 'kf_links_meta_box' )) {
return $post_id;
}
$post_type = get_post_type_object( $_POST['post_type'] );
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return;
}
$new_meta_value = ( isset( $_POST[self::kfl_key] ) ? $_POST[self::kfl_key] : '');
update_post_meta( $post_id, self::kfl_key, $new_meta_value );
}
function kf_links_explode( $string )
{
if ( empty($string) || !is_string($string) ) {
$links['is_active'] = 0;
return $links;
}
$explode = explode( ';$;', $string );
$links['is_active'] = ( isset( $explode[0] ) ? $explode[0] : 0 );
$links['title'] = ( isset( $explode[1] ) ? $explode[1] : '' );
$links['items'] = array();
for ( $i = 2; $i < count( $explode ); $i++ ) {
$explode2 = explode( ';?;', $explode[$i] );
$link = array(
'name' => $explode2[0],
'url' => $explode2[1]
);
$links['items'][] = $link;
}
return $links;
}
}
And here some screenshots:
https://www.dropbox.com/sh/jj3gluc7ok001hu/AABq_hRiKcbbDo1E0rKpP2Jxa?dl=0
Try :syntax sync fromstart.
If that works, add autocmd BufEnter * :syntax sync fromstart to your .vimrc
Explanation: if the syntax highlighter does not process every line of code from the beginning, it may not receive a critical piece of information to understand what type of code it's processing. E.g., if you're in an HTML file and deep within a script tag, the syntax highlighter may not look back far enough to see the script tag and therefore processes your JS code as HTML.
Related
I would like to be able to change the Page Attribute "Templates" dropdown to radio buttons, to allow me to have corresponding thumbnails next to them.
It looks like this question was already asked here - Is it possible to make WordPress page attribute meta box select option display as images? - however no code was ever supplied and looking at the comments it appears that the user answered their own question?
I have already removed and replaced the Page Attributes meta box on functions.php, as per below:
add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
function wpse44966_add_meta_box( $post_type ){
remove_meta_box(
'pageparentdiv',
'page',
'side');
add_meta_box(
'wpse44966-meta-box',
'page' == $post_type ? __('Page Style Templates') : __('Attributes'),
'wpse44966_meta_box_cb',
'page',
'side',
'low');
}
I am then able to call back the "Templates" dropdown in my own meta box - I have also included the page_template_dropdown function (renamed 'page_template_dropdown_show_it').
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$selected = selected( $default, $templates[ $template ], false );
echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<select name="page_template" id="page_template">
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
</select>
<?php endif;
}
However, when I then amend both page_template_dropdown_show_it and wpse44966_meta_box_cb to show radio buttons, the changes are applied visually but nothing happens when you select them?
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$checked = checked( $default, $templates[ $template ], false );
echo "\n\t<input type='radio' name='page_template' value='" . esc_attr( $templates[ $template ] ) . "' $checked>" . esc_html( $template );
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<input type='radio' name='page_template' value="default"><?php echo esc_html( $default_title ); ?>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
<?php endif;
}
It's obviously not as simple as a straight swap-out (as it isn't working), but the only thing that I can see that is missing is now there is nothing carrying the id="page_template", whereas before it was included in the below:
<select name="page_template" id="page_template">
So, it appears that I was correct that the issue was that the id "page_template" was no longer being passed.
To solve this, all I have done is applied some Javascript (using the admin_enqueue_scripts function) which adds the id "page_template" to the radio button that has been selected.
Ask to everyone, i have problem. Here i try to use multiple chechbox to my custom post metabox.
<?php
function prodetail() {
add_meta_box('pro_metabox', 'Detail Property', 'pro_metabox', 'property', 'normal', 'default');
}
function pro_metabox() {
global $post;
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );
$elements = array(
'pool' => 'Pool',
'garage' => 'Garage',
'balcon' => 'Balcon',
'yard' => 'Yard',
'internet' => 'Internet'
);
foreach ( $elements as $id => $element) {
if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
$checked = 'checked="checked"';
} else {
$checked = null;
}
?>
<div class="pro-inn">
<div class="procols">
<div class="pro-inn">
<input type="checkbox" name="multval[]" value="<?php echo $id; ?>" <?php echo $checked; ?> />
<?php echo $element;?>
</div>
</div>
</div>
<?php
}
}
function pro_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if ( ! empty( $_POST['multval'] ) ) {
update_post_meta( $post_id, 'elements', $_POST['multval'] );
} else {
delete_post_meta( $post_id, 'elements' );
}
}
add_action('save_post', 'pro_meta', 1, 2);
?>
help me to add code to show this checked result to single.php because my code use foreach just show Array text not show text like Pool Garage Balcon ect.
Thanks
Use this code in your single.php file for your custom post
$meta_value = get_post_meta( $post->ID, 'elements', true );
foreach($meta_value as $key=>$value){
echo $value . ' ';
}
It will show results same as you mentioned in the question ie:
(Pool Garage Balcon ect.)
Hello guys i got new WP theme and i cant install im getting this error on website
Parse error: syntax error, unexpected '}', expecting end of file in
/storage/h3/665/790665/public_html/wp-content/themes/gameaddict/post_templates.php
on line 1
<?php
class Single_Post_Template_Plugin {
function __construct() {
add_action( 'admin_menu', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'metabox_save' ), 1, 2 );
add_filter( 'single_template', array( $this, 'get_post_template' ) );
}
function get_post_template( $template ) {
global $post;
$custom_field = get_post_meta( $post->ID, '_wp_post_template', true );
if( !$custom_field )
return $template;
/** Prevent directory traversal */
$custom_field = str_replace( '..', '', $custom_field );
if( file_exists( get_stylesheet_directory() . "/{$custom_field}" ) )
$template = get_stylesheet_directory() . "/{$custom_field}";
elseif( file_exists( get_template_directory() . "/{$custom_field}" ) )
$template = get_template_directory() . "/{$custom_field}";
return $template;
}
function get_post_templates() {
$templates = wp_get_theme()->get_files( 'php', 1 );
$post_templates = array();
$base = array( trailingslashit( get_template_directory()), trailingslashit( get_stylesheet_directory()) );
foreach ( (array) $templates as $file => $full_path ) {
if( $full_path == get_theme_root().'/gameaddict/post_templates.php'){continue;}else{
if ( !preg_match( '|Single Post Template:(.*)$|mi', file_get_contents( $full_path ), $header ))
continue;
$post_templates[ $file ] = _cleanup_header_comment( $header[1] );
}}
return $post_templates;
}
function post_templates_dropdown() {
global $post;
$post_templates = $this->get_post_templates();
/** Loop through templates, make them options */
foreach ( (array) $post_templates as $template_file => $template_name ) {
$selected = ( $template_file == get_post_meta( $post->ID, '_wp_post_template', true ) ) ? ' selected="selected"' : '';
$opt = '<option value="' . esc_attr( $template_file ) . '"' . $selected . '>' . esc_html( $template_name ) . '</option>';
echo $opt;
}
}
function add_metabox() {
$screens = array( 'post', 'portfolio' );
foreach ( $screens as $screen ) {
add_meta_box(
'pt_post_templates',
__( 'Sidebar position', 'addict' ),
array( $this, 'metabox' ),
$screen,'normal', 'high'
);
}
}
function metabox( $post ) {
?>
<input type="hidden" name="pt_noncename" id="pt_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
<label class="hidden" for="post_template"><?php _e( 'Post Template', 'addict' ); ?></label><br />
<select name="_wp_post_template" id="post_template" class="dropdown">
<?php $this->post_templates_dropdown(); ?>
</select>
<?php
}
function metabox_save( $post_id, $post ) {
/*
* Verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times
*/
if(isset($_POST['pt_noncename'])){
if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename( __FILE__ ) ) )
return $post->ID;
}
/** Is the user allowed to edit the post or page? */
if(isset($_POST['post_type'])){
if ( 'page' == $_POST['post_type'] )
if ( !current_user_can( 'edit_page', $post->ID ) )
return $post->ID;
else
if ( !current_user_can( 'edit_post', $post->ID ) )
return $post->ID;
}
/** OK, we're authenticated: we need to find and save the data */
/** Put the data into an array to make it easier to loop though and save */
if(isset($_POST['_wp_post_template'])){
$mydata['_wp_post_template'] = $_POST['_wp_post_template'];
}
/** Add values of $mydata as custom fields */
if(isset($mydata)){
foreach ( $mydata as $key => $value ) {
/** Don't store custom data twice */
if( 'revision' == $post->post_type )
return;
/** If $value is an array, make it a CSV (unlikely) */
$value = implode( ',', (array) $value );
/** Update the data if it exists, or add it if it doesn't */
if( get_post_meta( $post->ID, $key, false ) )
update_post_meta( $post->ID, $key, $value );
else
add_post_meta( $post->ID, $key, $value );
/** Delete if blank */
if( !$value )
delete_post_meta( $post->ID, $key );
}}
}
}
add_action( 'init', 'post_templates_plugin_init' );
/**
* Instantiate the class after theme has been set up.
*/
function post_templates_plugin_init() {
new Single_Post_Template_Plugin;
}
Assuming the theme is this one https://themeforest.net/item/game-addict-clan-war-gaming-theme/6771881 and it's purchased from themeforest, it should work right out of the box. Try downloading it again, make sure to check if there is not a zipped file inside of the zip you are downloading to make sure you are uploading the right file, sometimes the zip you download has a lot of files like documentation and demos, but the actual theme file is another zip inside.
If nothing of the above helps, write to their customer support.
Basically, it's complaining that wp-content/themes/gameaddict/post_templates.php has a } on line 1, while PHP was expecting end of a file. The code you've included seems fine. The issue is most likely about the developer closing a bracket without opening it, somewhere in the code. It seems like you're using a pre-made theme, in that case contacting the developer would be the best option.
im creating a custom metabox as such:
<?php
/**
* Plugin Name: Relaterade sidor
* Description: lägg till relaterade sidor
* Version: 1.0
* Author: test
* Author URI: test
*
*/
function relaterade_sidor_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function relaterade_sidor_add_meta_box() {
add_meta_box(
'relaterade_sidor-relaterade-sidor',
__( 'relaterade sidor', 'relaterade_sidor' ),
'relaterade_sidor_relaterade_sidor_html',
'post',
'normal',
'default'
);
add_meta_box(
'relaterade_sidor-relaterade-sidor',
__( 'relaterade sidor', 'relaterade_sidor' ),
'relaterade_sidor_relaterade_sidor_html',
'page',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'relaterade_sidor_add_meta_box' );
function relaterade_sidor_relaterade_sidor_html( $post) {
wp_nonce_field( '_relaterade_sidor_relaterade_sidor_nonce', 'relaterade_sidor_relaterade_sidor_nonce' ); ?>
<p>relaterade sidor</p>
<p>
<label for="relaterade_sidor_relaterade_sidor_url"><?php _e( 'Url', 'relaterade_sidor' ); ?></label><br>
<input type="text" name="relaterade_sidor_relaterade_sidor_url" id="relaterade_sidor_relaterade_sidor_url" value="<?php echo relaterade_sidor_get_meta( 'relaterade_sidor_relaterade_sidor_url' ); ?>">
</p><?php
}
function relaterade_sidor_relaterade_sidor_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['relaterade_sidor_relaterade_sidor_nonce'] ) || ! wp_verify_nonce( $_POST['relaterade_sidor_relaterade_sidor_nonce'], '_relaterade_sidor_relaterade_sidor_nonce' ) ) return;
if ( ! current_user_can( 'edit_post' ) ) return;
if ( isset( $_POST['relaterade_sidor_relaterade_sidor_url'] ) )
update_post_meta( $post_id, 'relaterade_sidor_relaterade_sidor_url', esc_attr( $_POST['relaterade_sidor_relaterade_sidor_url'] ) );
}
add_action( 'save_post', 'relaterade_sidor_relaterade_sidor_save' );
/*
Usage: relaterade_sidor_get_meta( 'relaterade_sidor_relaterade_sidor_url' )
*/
?>
the text remains in the textfield after saving/updating so I guess that means it is saved to the database, but when trying to display the data in a page or post I only get blank.
I tried with both these ones:
<?php if (have_posts()) : while (have_posts()) : the_post();
$intro = get_post_meta(get_the_ID(), 'relaterade_sidor-relaterade-sidor', true);
echo "content: " . $intro;
?>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'relaterade_sidor-relaterade-sidor', true);
wp_reset_query();
?>
But I only get blank, cant get it to display the content saved in the custom meta textbox.
Sorry, was trying to connect it to the metabox name when i should connect the key,
$intro = get_post_meta(get_the_ID(), 'relaterade_sidor_relaterade_sidor_url', true);
echo "content: " . $intro;
works just fine inside the loop
I suggest CMB2 library. No more headaches with metaboxes ;)
https://stackoverflow.com/a/31662024/5162081
I got a WordPress Rating plugin from "http://www.smashingmagazine.com/2012/05/08/adding-custom-fields-in-wordpress-comment-form/"
I would like to count the total number of rating and print that number under the Blog Post. Is this possible?
I added some customisation in this code.
<?php
// Add fields after default fields above the comment box, always visible
add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );
function additional_fields () {
echo '<p class="comment-form-rating">'.
'<label for="rating">'. __('Rating') . '<span class="required">*</span></label>
<span class="commentratingbox">';
for( $i=1; $i <= 5; $i++ )
echo '<span class="commentrating"><input type="radio" checked name="rating" id="rating" value="'. $i .'"/>'. $i .' Star</span>';
echo'</span></p>';
}
// Save the comment meta data along with comment
add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') )
$rating = wp_filter_nohtml_kses($_POST['rating']);
add_comment_meta( $comment_id, 'rating', $rating );
}
// Add the filter to check if the comment meta data has been filled or not
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST['rating'] ) )
wp_die( __( 'Error: You did not add your rating. Hit the BACK button of your Web browser and resubmit your comment with rating.' ) );
return $commentdata;
}
//Add an edit option in comment edit screen
add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box() {
add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}
function extend_comment_meta_box ( $comment ) {
$phone = get_comment_meta( $comment->comment_ID, 'phone', true );
$title = get_comment_meta( $comment->comment_ID, 'title', true );
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
?>
<p>
<label for="rating"><?php _e( 'Rating: ' ); ?></label>
<span class="commentratingbox">
<?php for( $i=1; $i <= 5; $i++ ) {
echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"';
if ( $rating == $i ) echo ' checked="checked"';
echo ' />'. $i .' </span>';
}
?>
</span>
</p>
<?php
}
// Update comment meta data from comment edit screen
add_action( 'edit_comment', 'extend_comment_edit_metafields' );
function extend_comment_edit_metafields( $comment_id ) {
if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;
if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') ) :
$phone = wp_filter_nohtml_kses($_POST['phone']);
update_comment_meta( $comment_id, 'phone', $phone );
else :
delete_comment_meta( $comment_id, 'phone');
endif;
if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ):
$title = wp_filter_nohtml_kses($_POST['title']);
update_comment_meta( $comment_id, 'title', $title );
else :
delete_comment_meta( $comment_id, 'title');
endif;
if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ):
$rating = wp_filter_nohtml_kses($_POST['rating']);
update_comment_meta( $comment_id, 'rating', $rating );
else :
delete_comment_meta( $comment_id, 'rating');
endif;
}
// Add the comment meta (saved earlier) to the comment text
// You can also output the comment meta values directly in comments template
add_filter( 'comment_text', 'modify_comment');
function modify_comment( $text ){
$plugin_url_path = WP_PLUGIN_URL;
if( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
$commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
$text = $commenttitle . $text;
}
if( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
$commentrating = '<p class="comment-rating"> <img src="'. $plugin_url_path .
'/ExtendComment/images/'. $commentrating . 'star.gif"/><br/>Rating: <strong>'. $commentrating .' / 5</strong></p>';
$text = $text . $commentrating;
return $text;
} else {
return $text;
}
}
It would go something like this. Appending the total for all ratings:
if( !is_admin() )
add_filter( 'the_content', 'content_so_23778986' );
function content_so_23778986( $content )
{
if( !is_single() )
return $content;
# Get all comments from current post
$comms = get_comments( array( 'post_id'=>get_the_ID() ) );
$counter = null;
if( $comms )
{
$total = count( $comms );
# Iterate all comments and add its rating to the counter
foreach( $comms as $c )
{
$rating = (int) get_comment_meta( $c->comment_ID, 'rating', true );
$counter += $rating;
}
# Calculate and print the totals
$counter = '<br />' . $counter / $total . ' de 5';
}
return $content . $counter;
}