0

I am developing a simple ranking plugin. Every post has it's own like count, and these counts are saved as custom-fields.

The problem is, no matter how much i research, i still couldn't find the proper way to manipulate theme's design in order to display post meta as i like. I've found some methods about changing theme's content.php file but it doesn't make sense because i want this plugin to work on every theme. Also in wordpress codex, it's told that i can do this by using template tags in the loop however index.php page which i can customize loop in, is not also included to my plugins directory.

What should i do in order to display post's ranking meta properly for all themes ?

  • Your plugin can include a shortcode that will allow users to output the rating anywhere within the content area. Also, your plugin could include a template tag which would allow users to add the rating wherever they would like within their theme (ie fatihint_show_rating() ) – Dave Romsey Apr 1 '17 at 8:23
0

To modify the content of a post, you can use the_content filter. You (probably) only want to add the post meta to the main query. If not, you can skip the is_main_query() conditional. Then you can just get the post meta and format it how you see fit and append it to the content.

//* Add filter to the_content if we're in the main query
add_action( 'the_post', 'wpse_261935_the_post' );
function wpse_261935_the_post( $post ) {
  if( is_main_query() ) {
    add_filter( 'the_content', 'wpse_261935_the_content' );
  }
}
function wpse_261935_the_content( $content ) {
  //* Make sure to add and remove filter for each post
  //* to make sure it's in the main query
  remove_filter( 'the_content', 'wpse_261935_the_content' );

  $wpse_261935_meta = get_post_meta( get_post()->ID, '_wpse_261935_post_meta_key', true );
  $wpse_261935_content = wpse_261935_format_post_meta( $wpse_261935_meta );

  return $wpse_261935_content . $content;
}
function wpse_261935_format_post_meta( $post_meta ) {
  //* Format the post meta however you'd like
  return $html;
}
  • Better solution is add shortcode support or event call output function. – Foxsk8 Mar 30 '17 at 20:38
  • No it's not. Did you even read the question? – Nathan Johnson Mar 30 '17 at 20:46
  • simple ranking plugin when you create plugin that can affect to content, better always do seperate, becouse, content can be output for page, or categories, or single, or custom post types. If users dont want show ranking for example in custom gallery post, there is no options to do. But if you use function, or shortcode, you can output it on everywhere and control the output, via shortcode you can also put in content editor it self. – Foxsk8 Mar 30 '17 at 20:53
  • Forcing a user of a plugin to add a shortcode to every post is terrible UX, and more importantly here, doesn't answer the question that was asked by the OP. – Nathan Johnson Mar 30 '17 at 21:04
  • Ahh, you can open your single.php template file and just do: echo do_shortcode( '[my-plugin-rating]' ); for example, and that is showing only on posts, not for custom posts or anywhere else in site where output the_content. Your solution yes, it is simply, but dirty. If author say, i want make a plugin, not a function. – Foxsk8 Mar 30 '17 at 21:09
0
<?php

/*
Plugin Name: My post rating
Plugin URI: 
Version: 0.1
Author: NONE
Author uri: 
Description: My post rating with meta key output
Text Domain: rating
Domain Path: /languages
*/


/* Usage:


Code in template in single or loop.

Via function:

<?php echo my_simply_rating(); ?>

Via shortcode:

Post rating count: do_shortcode['myrating'] */


function my_simply_rating() {
    $count = get_post_meta( get_post()->ID, '_post_meta_key', true );
    return $count;
}
add_shortcode('myrating', 'my_simply_rating');


?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.