• I have been caught on this issue for a while now. I have created a plugin that adds a custom rating to comments, I have created the logic to calculate the average and display it. My issue now is, I can display this in the comments, however I want to display it on my custom post type template.

    I have tried using `GLOBALS[‘$variable’] = 1; and when I echo the $variable in the CPT, it does not show.

    I have also tried global $variable

    Simply put, how do I echo a variable from a plugin to a custom post type?

    i.e.
    How do I get from my plugin: $variable = 1 to echo 1 on my custom post template?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Write a function that retrieves the result you want in your plugin.

    function retrieve_custom_rating( $post_id ) {
    
        // use $post_id to look up your custom rating
    
        return $rating;
    
    }

    Then, that function will be available in your template files.

    Thread Starter christhomas32

    (@christhomas32)

    Thanks for the replay Andrew, I am working a plugin I have created. In a comment addition plugin, I have added the ability to add a rating (1-5). I am now calculating and trying to display the average of the ratings $avg_rating = ($tot_stars / $no_of_comments);. I have logic that allows this to work and be displayed within the comments themselves, however I want them to be displayed on my custom post template. I have tried adding my calculations to a function as you have recommended as such:

    function calc_avg( $post_id ) {
    	// Calculate rating averages
    	$args = array(
        'ID' => $post_id,
        'status' => 'approve',
    );
    $comments = get_comments( $args );
    foreach( $comments as $comment ) {
        $tot_stars +=  get_comment_meta( $comment->comment_ID, 'rating', true );
    }
    
    	$no_of_comments = get_comments_number( $post_id );
    	$GLOBALS['$avg_rating'] = ($tot_stars / $no_of_comments);
    
    	if ($no_of_comments > 0) {
    		$avg_return = $avg_return;
    		return $avg_return;
    	} else {
    		return $avg_return;
    	}
    }

    however echo $avg_return does nothing. Nor does echo calc_avg($post_id) and using GLOBALS or global has not helped either. Any other ideas?

    Are you sure that $post_id is being set properly? Have you done a

    echo $post_id;
    die();

    As the first two lines of the function to verify that you are passing something?

    If you’re in the loop you could pass get_the_ID() too.

    while( have_posts() ) {
      the_post();
      echo calc_avg( get_the_id() );
    }

    First thing is to verify that you are indeed passing the correct id to the function. If the function wasn’t included in the template file, you would have received a fatal error, so we’re on the right track.

    Thread Starter christhomas32

    (@christhomas32)

    So I have tried passing echo $post_id; and `while( have_posts() ) {
    the_post();
    echo calc_avg( get_the_id() );
    }`

    Both did not pass any value, however there were no fatal errors. My loop is slightly adjusted, maybe this could affect it?

    <?php query_posts(array('post_type'=>'products')); ?>
    	<?php $mypost = array( 'post_type' => 'products' );
    	$loop = new WP_Query( $mypost ); ?>
    	<?php wp_reset_query(); ?>

    EDIT: echo the_ID(); returns an integer

    Can you put the whole template in a pastebin or gist please?

    Thread Starter christhomas32

    (@christhomas32)

    Sure,

    The template:
    http://pastebin.com/82eQw7pT

    and the function as it is currently in my comment plugin:

    function calc_avg( $post_id ) {
    	// Calculate rating averages
    	$args = array(
        'ID' => $post_id,
        'status' => 'approve',
    );
    $comments = get_comments( $args );
    foreach( $comments as $comment ) {
        $tot_stars +=  get_comment_meta( $comment->comment_ID, 'rating', true );
    }
    
    	global $testvar;
    	$testvar = 1;
    	$no_of_comments = get_comments_number( $post_id );
    	$GLOBALS['$avg_rating'] = ($tot_stars / $no_of_comments);
    	global $avg_return;
    	if ($no_of_comments > 0) {
    		$avg_return = $avg_return;
    		return $avg_return;
    	} else {
    		return $avg_return;
    	}
    }
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Retrieving a variable from a plugin and inputing to template’ is closed to new replies.