• I have:

    /*
     * Routine to display a custom meta box editor
     * Note: In this example our custom meta data is saved as a row in the postmeta database table.
     * $post PostObject An object representing a WordPress post that is currently being loaded in the post editor.
     */
    function camere_meta_box_render( $post ){
    
        // Get the number and display it in a numeric field
        $number = get_post_meta( $post->ID, "function_camere", true );
        echo '<div><select name="function_camere">';
    for( $i = 0; $i < 13; $i++ ) {
      echo '<option value="'.$i.'" '. selected( $number, $i, false ) .' >'.$i.'</option>';
    }
    echo '</select></div>';
     
    }
    
    // Hook into the save routine for posts
    add_action( 'save_post', 'camere_meta_box_save');
    
    /*
     * Routine to save the custom post meta data from the editor
     * Note: We retrieve the form data through the PHP Global $_POST
     * $post_id int The ID of the post being saved
     */
    function camere_meta_box_save( $post_id ){
         
        // Check to see if this is our custom post type (remove this check if applying the meta box globally)
        if($_POST['post_type'] == "post"){
            
            // Retrieve the values from the form
            $number = $_POST['function_camere'];
            
    
            // Clean, sanitize and validate the input as appropriate
    
            // Save the updated data into the custom post meta database table
            update_post_meta( $post_id, "function_camere", $number );
    

    I dont need numbers now, but the options are gettexted sentences, how can I echo these sentences from a selector?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Gettext requires fixed, static strings so you cannot dynamically generate a select list. You’ll need to define each option one at a time. For example:

    echo '<select name="function_camere">';
    $i = __('one','sacconicase');
    echo '<option value="one" '. selected( $saved, 'one', false ) .' >'.$i.'</option>';
    $i = __('two','sacconicase');
    echo '<option value="two" '. selected( $saved, 'two', false ) .' >'.$i.'</option>';
    //etc....

    In this example, the saved value is always in English, but the displayed options are in the current language. This approach has a lot of redundant code, there are likely ways to streamline it, but the option label displayed does need to be done one at a time.

    Thread Starter sacconi

    (@sacconi)

    I have a problem because in italian you use the apostrophe and I suppose this is interpreted as a single quotation mark from the system. This is what I wrote:

    function introduzione_meta_box_render( $post ){
    
        // Get the number and display it in a numeric field
        $introduzione = get_post_meta( $post->ID, "function_camere", true );
     echo '<div><select name="function_camere">';
    $i = __('L'appartamento si compone di:','sacconicase');
    echo '<option value="L'appartamento si compone di:" '. selected( $saved, 'L'appartamento si compone di:', false ) .' >'.$i.'</option>';
    $i = __('La casa si compone di:','sacconicase');
    echo '<option value="La casa si compone di:" '. selected( $saved, 'La casa si compone di:', false ) .' >'.$i.'</option>';
     }
    echo '</select></div>';
    
    

    I cant avoid writing “L’appartamento”

    Moderator bcworkz

    (@bcworkz)

    Only the “straight”(') apostrophe is seen as a quote. You can use the “curly” kind () and it will not be seen as a quote. Or use the equivalent entity code & rsquo; (remove the space after the & that I had to insert to prevent the forum’s parser from converting it into a )

    Another way to use a straight apostrophe in code is to use double quotes:
    echo "L'appartamento";
    If you need a double quote to be seen as a character in a double quoted string, it can be escaped:
    echo "He replied \"Nuts\".";

    Thread Starter sacconi

    (@sacconi)

    If I do

    function introduzione_meta_box_render( $post ){
    
        // Get the number and display it in a numeric field
        $introduzione = get_post_meta( $post->ID, "function_camere", true );
     echo '<div><select name="function_camere">';
    $i = __(''L'appartamento si compone di:'','sacconicase');
    echo '<option value="L'appartamento si compone di:" '. selected( $saved, 'L'appartamento si compone di:', false ) .' >'.$i.'</option>';
    $i = __('La casa si compone di:','sacconicase');
    echo '<option value="La casa si compone di:" '. selected( $saved, 'La casa si compone di:', false ) .' >'.$i.'</option>';
     }
    echo '</select></div>';

    with

    $i = __(''L'appartamento si compone di:'','sacconicase');

    PhpChecker tells me it’s an error

    Moderator bcworkz

    (@bcworkz)

    Use one double quote character, not two single quote characters.
    $i = __("L'appartamento si compone di:",'sacconicase');

    Thread Starter sacconi

    (@sacconi)

    I still have a problem with

    $i = __("L'appartamento si compone di:",'sacconicase');

    inside

    function introduzione_meta_box_render( $post ){
        // Get the number and display it in a numeric field
        $introduzione = get_post_meta( $post->ID, "function_camere", true );
     echo '<div><select name="function_camere">';
    $i = __("L'appartamento si compone di:",'sacconicase');
    echo '<option value="L'appartamento si compone di:" '. selected( $saved, "L'appartamento si compone di:", false ) .' >'.$i.'</option>';
    $i = __("La casa si compone di:",'sacconicase');
    echo '<option value="La casa si compone di:" '. selected( $saved, 'La casa si compone di:', false ) .' >'.$i.'</option>';
     }
    echo '</select></div>';

    Your syntax is breaking on the echo line because you are not using quotes properly. You need to escape quotes when you have nested quotes:

    function introduzione_meta_box_render($post)
    {
        // Get the number and display it in a numeric field
        $introduzione = get_post_meta($post->ID, 'function_camere', true);
        echo '<div><select name="function_camere">';
        $i = __("L'appartamento si compone di:",'sacconicase');
        echo '<option value="L\'appartamento si compone di:" ' . selected($saved, "L'appartamento si compone di:", false) . ' >' . $i . '</option>';
        $i = __('La casa si compone di:', 'sacconicase');
        echo '<option value="La casa si compone di:" ' . selected($saved, 'La casa si compone di:', false) . ' >' . $i . '</option>';
        echo '</select></div>';
    }
    

    Or store the value in a variable and use that variable if you are getting confused:

    function introduzione_meta_box_render($post)
    {
        // Get the number and display it in a numeric field
        $introduzione = get_post_meta($post->ID, 'function_camere', true);
        $original_text = "L\'appartamento si compone di:";
        echo '<div><select name="function_camere">';
        $translated_text = __($original_text, 'sacconicase');
        echo '<option value="' . $original_text . '" ' . selected($saved, $original_text, false) . ' >' . $translated_text . '</option>';
    
        $original_text = 'La casa si compone di:';
        $translated_text = __($original_text, 'sacconicase');
        echo '<option value="' . $original_text . '" ' . selected($saved, $original_text, false) . ' >' . $translated_text . '</option>';
        echo '</select></div>';
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Modifying a function’ is closed to new replies.