Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:53:05+00:00 2026-06-13T21:53:05+00:00

I am trying to write a content rating script where the user can select

  • 0

I am trying to write a content rating script where the user can select what type of rating to give to their article (for instance, what age group the articles suits for).

I am using a WordPress star rating script as a template.

This part of the script is where the user selects the rating:

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 '<label for="pn_apr_rating">' . __( 'Choose a rating for this post:', 'author-post-ratings' ) . '</label> ';
    echo '<select name="pn_apr_rating" id="pn_apr_rating">';
    echo '<option value="unrated"' . selected( $current_post_rating, 0, false ) . '>' . __( 'Unrated', 'author-post-ratings' ) . '</option>';
    for ( $i = 1; $i <= 5; $i++ ) {
        echo '<option value="' . $i . '"' . selected( $current_post_rating, $i, false ) . '>' . sprintf( _n( '%1s Star', '%1s Stars', $i, 'author-post-ratings' ), $i ) . '</option>';
    }
    echo '</select>';
}

This part of the script outputs the ratings:

if ( $rating ) {

    $output = null;

    $output .= '<div class="author-post-rating">';

    $output .= '<span class="author-post-rating-label">' . esc_attr( $pn_apr_settings['label_text'] ) . '</span> ';

    $output .= '<span class="author-post-rating-stars" title="' . sprintf( __( '%1$d out of %2$d stars', 'author-post-ratings' ), $rating, 5 ) . '">';

    // Output active stars
    for ( $i = 1; $i <= $rating; $i++ ) {

        $output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-active.png" />';

    }

    // Output inactive stars
    for ( $i = $rating + 1; $i <= 5; $i++ ) {

        $output .= '<img src="' . PN_APR_PLUGIN_DIR_URL . 'images/star-inactive.png" />';

    }

    $output .= '</span>' . "\n";

    $output .= '</div><!-- .author-post-rating -->';

    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;

}

Now, I want to change this script so that it becomes a content rating script (rather than star rating one). I want to offer these choices for the user:

G — Suitable for all audiences

PG — Possibly offensive, usually for audiences 13 and above

R — Intended for adult audiences above 17

X — Even more mature than above

Question: How can I change the script so that if the user selects for instance PG, then it will output the text Suitable for age 13 and up.

EDIT:

To Shawn, observe the following code:

function rating_select_cb( $post ) {
    global $wpdb;
    $value = get_post_meta($post->ID, 'rating', true);
    echo '<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Select a rating:<br></label>';

    // build an array of each available rating
    $ratings = array(
        1 => 'G — Suitable for all audiences',
        2 => 'PG — Possibly offensive, usually for audiences 13 and above',
        3 => 'R — Intended for adult audiences above 17',
        4 => 'X — Even more mature than above'
    );

    echo '<select name="rating">';
    echo '<option value=""' . ((($value == '') || !isset($ratings[$value])) ? ' selected="selected"' : '') . '> None... </option>';

    // output each rating as an option
    foreach ($ratings as $id => $text) {
        echo '<option value="' . $id . '"' . (($value == $id) ? ' selected="selected"' : '') . '">' . $text. '</option>';
    }
    echo '</select>';

    echo '</span></div>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:53:06+00:00Added an answer on June 13, 2026 at 9:53 pm

    Here’s something that should get you to where you need to be:

    Add to your functions.php:

    add_action( 'add_meta_boxes', 'rating_select_box' );
    function rating_select_box() {
        add_meta_box(
            'rating_select_box', // id, used as the html id att
            __( 'Select rating:' ), // meta box title, like "Page Attributes"
            'rating_select_cb', // callback function, spits out the content
            'post', // post type or page. We'll add this to posts only
            'side', // context (where on the screen
            'low' // priority, where should this go in the context?
        );
    
    }
    
    function rating_select_cb( $post )
    {
        global $wpdb;
        $value = get_post_meta($post->ID, 'rating', true);
        echo '<div class="misc-pub-section misc-pub-section-last">
             <span id="timestamp">'
             . '<label>Select a rating:<br></label>';
    
             $selected = ($value == $result->post_name) ? ' selected="selected" ' : null;
    
             echo '<select name="rating">';
             echo '<option value="" default="default"> None... </option>';
             echo '<option value="0" '.$selected.'> G — Suitable for all audiences </option>';
             echo '<option value="1" '.$selected.'> PG — Possibly offensive, usually for audiences 13 and above </option>';
             echo '<option value="2" '.$selected.'> R — Intended for adult audiences above 17 </option>';
             echo '<option value="3" '.$selected.'> X — Even more mature than above </option>';
             echo '</select>';      
    
        echo '</span></div>';
    }
    
    add_action( 'save_post', 'save_metadata');
    
    function save_metadata($postid)
    {   
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
        if ( !current_user_can( 'edit_page', $postid ) ) return false;
        if( empty($postid) ) return false;
    
    
        if ( is_null($_REQUEST["rating"]) ) {
            delete_post_meta($postid, 'rating');
        } else {
            update_post_meta($postid, 'rating', $_REQUEST['rating']);
        }
    
    }
    

    The above code does this:
    1. Adds a new metabox to your posts
    2. Adds a select box with the rating values
    3. Saves the metadata with the post

    To access your metadata in your templates:

    $meta = get_post_custom($post->ID);
    echo $meta['rating'][0];
    

    To have your template display a custom string use something like:

    switch ( $meta['rating'][0] ) {
        case 0:
            echo "This is rated PG";
            break;
        case 1:
            echo "This is rated G";
            break;
        case 2:
            echo "This is rated R";
            break;
        case 3:
            echo "Ug oh! This is rated X!";
            break;
        default:
            echo "This is not yet rated.";
    }
    

    **Edit: This code provides full functionality.. you could abandon your current solution if it works for you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying write a script where when a user clicks on a link,
I'm trying to write a script that 1. lists the content of a directory,
I'm trying to write a simple script that effectively fades out the page content,
I'm trying to write a script that reads the file content below and extract
I'm trying to write a line in Bash that gets the Content-Type (without encoding)
I'm trying to write an embedded ( NOT web, not enterprise ) content management
I'm trying to write binary search tree's content to temporary array in order to
i am trying to write a regex that produces the content in a string
I am trying to write XML data using Stax where the content itself is
I'm trying to write a code, what would save the content of a picturebox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.