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 8779625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:50:24+00:00 2026-06-13T19:50:24+00:00

Adding the following script to the function.php file in WordPress: add_action( ‘add_meta_boxes’, ‘rating_select_box’ );

  • 0

Adding the following script to the function.php file in WordPress:

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']);
    }

}

Will add a box in the post page with a drop down list. I want the default option to be on None... so I added a default="default" to that option, but it does not work. By default, the highest number value is selected, in this case, it is X — Even more mature than above.

What am I doing wrong? How can I make it so that the None... option is selected by default?

  • 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-13T19:50:26+00:00Added an answer on June 13, 2026 at 7:50 pm

    In a select list, the correct way to “select” an option is with selected="selected", not default="default". Changing your ‘None’ option to the following should fix it:

    echo '<option value="" selected="selected"> None... </option>';
    

    However, you’re not dynamically creating your select list, you’re just outputting a full list and you’ll run into issues selecting the other values too. To fix this, if the options won’t be changing, you can put them in an array to loop through them. You can modify your rating_select_cb() method with the following new code to achieve this:

    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>';
    }
    

    This method will default-select the “None” option if the selected $value is not in the available list of ratings. After that, it will loop through each rating and output it as an option. If the rating’s $id matches the selected $value, it will be marked as selected.

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

Sidebar

Related Questions

I have the following jQuery/ajax script: jQuery(input.button).click(function(){ jQuery.ajax({ url: 'addfav.php', type: 'POST', data: {'id':jQuery(this).closest(div).attr(id),is_ajax:
I' m trying to enable adding/deleting rows to/from table using folowing code: Script: $(document).ready(function()
I am dynamically adding tabs to my page using the following script: var tabs
I've got the following in my <head> section: <script type=text/javascript> $(function(){ $(h5).live(click, function(){ alert($(this).tmplItem());
Consider the following script: #!/bin/bash function long_running { for i in $(seq 1 10);
I am querying a php script using js with following func call: $.post(http://localhost/abc/create.php,{ name:data
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {
the following plotLine object on the xAxis is adding lines to both panes how
In the following code segment when built I keep breaking when adding the OnClickListener
I am using the following code for adding a listener in prefernceactivity. But its

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.