views:

357

answers:

4

Okay let me see if I can explain this right. In wordpress we have a box to insert an excerpt. We need to add a second excerpt box. Instead of manually adding a custom field to every post I have placed a function to automatically add a custom field in the form of a Meta box on the admin post page.

Okay so this is the problem that i'm having this function is working except for the fact that whatever you enter into this field it loses it's line breaks. So when our writers are contrubuting to this field in order to keep formatting of the block of text I have to manually add
to the end of the paragraph.

Here is my code:

function my_create_post_meta_box() {
    add_meta_box( 'my-meta-box', 'Second Excerpt', 'my_post_meta_box', 'post', 'normal', 'high' );
}

function my_post_meta_box( $object, $box ) { ?>
<p>
    <label for="second-excerpt">
        <strong>Second Excerpt With Images for Post List Page</strong>
    </label>
    <textarea name="second-excerpt" id="second-excerpt" cols="60" rows="4" tabindex="30" style="width: 97%;" wrap="hard"><?php echo wp_specialchars( get_post_meta( $object->ID, 'Second Excerpt', true ), 1 ); ?></textarea>
    <input type="hidden" name="my_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php
}

function my_save_post_meta_box( $post_id, $post ) {

    if ( !wp_verify_nonce( $_POST['my_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
        return $post_id;

    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    $meta_value = get_post_meta( $post_id, 'Second Excerpt', true );
    $new_meta_value = stripslashes( $_POST['second-excerpt'] );

    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, 'Second Excerpt', $new_meta_value, true );

    elseif ( $new_meta_value != $meta_value )
        update_post_meta( $post_id, 'Second Excerpt', $new_meta_value );

    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, 'Second Excerpt', $meta_value );
}

Thanks and any help would do.

A: 

Just add this line after $new_meta_value = ...:

$new_meta_value = nl2br($new_meta_value);

And instead of comparing your values to '', it's better to use empty(). Also some of the comparisons are unneeded. Thus, the add/update/delete part of your save function can be written like this:

if(empty($meta_value)) {
    add_post_meta( $post_id, 'Second Excerpt', $new_meta_value, true );
} elseif(empty($new_meta_value)) {
    delete_post_meta( $post_id, 'Second Excerpt', $meta_value );
} else {
    update_post_meta( $post_id, 'Second Excerpt', $new_meta_value );
}

Note that it's always advisable to use curly braces even if your statement is only one row long. It improves readability and doesn't mess things up if/when you have to add another row to the if clause.

Tatu Ulmanen
Thanks! I was going crazy... and it was just this simple.
Matthew
One last question. When I update my posts more than just once for example if there was an edit made it keeps adding linebeaks to that second excerpt. Can I have it only apply to the post only once?
Matthew
Use `str_replace('<br />', "\n", wp_specialchars( get_post_meta( $object->ID, 'Second Excerpt', true ), 1 ))` to turn the line break tags back to line breaks when inserting the excerpt into the textarea. Now it sounds like the line break tags are inserted in the excerpt and this somehow messes it up when saved.
Tatu Ulmanen
Well it's not really messing up. for example:This is sample text <br> - this is correctIf I go back into my post and lets say add something to the content and save again I get:this is sample text <br><br> - I just want to limit the adding of <br> to only once.
Matthew
You are adding a line break after the <br>, that's the only reason why it is adding new <br> tags. Without seeing the page, it's hard to say why this happens.
Tatu Ulmanen
This is also giving me break for every line I would only need a <br> at the end of a paragraph
Matthew
Tatu... thanks for all your help. Here is a page to view:http://blog.standforisrael.org/This is what is happening on the admin side when the post is updated:Last week, we told you how Mohammad Mansour Azimzadeh<br />Ardebili accidentally included Israel's national soccer<br />association on an e-mail list that sent new year's to<br />associations in the international soccer<br />federation.(Ardebili was supposed to send it to all of the<br />teams Israel.)
Matthew
Maybe the solution would be to add <p> tags instead???
Matthew
A: 

Okay I have found another solution to my problem. Thank you Tatu for getting my brain workin. For those who are looking for a solution this is what I did:

$new_meta_value = "<p>" . implode( "</p>\n\n<p>", preg_split( '/\n(?:\s*\n)+/', $new_meta_value ) ) . "</p>";
Matthew
A: 

Don't modify the data saved to the database. Save exactly what the user enters. Instead modify the content when you need to display it. This way when the user comes back to edit the field the edit what they put in, not what you've made of their content.

Use wpautop to do the same translation on your text that WordPress applies to the raw content entered in the post-content field and do it when the content is requested for display.

Gipetto
I'm not sure I get you Gipetto? This solution actually works just fine for what we need but if you think there is a better way please can you be a little more descriptive.Thanks!
Matthew
You're saving modified data to the database. Just save the raw data as input by the user. That way when the user comes back to edit the text they don't have to contend with <br /> elements in their content. Since the <br /> is only necessary for proper front end output I'd only apply the nl2br function when outputting the content on the front end.This isn't an absolute, but its a practice that I've gotten used to. Its just seems easier for the user in the long run to make it display ready only when you need to display it.
Gipetto
Also, by using the wpautop function that's built in to wordpress you'll get better output than nl2br.
Gipetto