Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I built a post interface to accept specific data in the form of custom meta boxes [rant]but now the end user says it's too much work to cut and paste 4 times[/rant]. So now (completely out of original scope) I need to parse the data entered into a new text area meta box and assign each line as a value to 2 existing keys, the_tile and a substr 'd excerpt. (see image)

Actually it really doesn't need to use the existing keys. It can use new ones or just be assigned to a new $variable (I would rather parse on save than on query).

The meta boxes are built using a class that handles the saving and updating, I just need to know how to split of the new field. I also have to conditionally determine if they used the individual boxes or the new text area box.

One of the meta boxes is for an external url. I have to check if it exists. If it's null the the template uses the permalink.

The use case is very specific as all the data is output in specific places on various sized boxes. See below:

enter image description here

The order I need to parse the data:

title <---- I'm saying this one is optional (They still have to enter a post title)

source `<---- meta_key is $prefix.'article-source'

excerpt <--- the_excerpt but can be a new key It can only output 115 characters to avoid breaking the layout. We also have to check if anything was entered into the real WordPress excerpt box before we decide to do any of this.

click-through-url <--- meta_key is $prefix.'external_link

All this code works fine. Just adding it to show how the data is used.

while ($box_query->have_posts()) : $box_query->the_post(); global $post; global $prefix;

    $box_size = c3m_get_field( $prefix.'box_size', FALSE );
    $box_image = c3m_get_field( $prefix.'post_box_image', FALSE );
    $overlay_class = c3m_get_field( $prefix.'overlay_class', FALSE );
    $ad_code = c3m_get_field( $prefix.'ad_code', FALSE );
    $source = c3m_get_field( $prefix.'article-source', FALSE );

    if ( c3m_get_field($prefix.'external_link', FALSE) ) {
    $post_link = c3m_get_field($prefix.'external_link', FALSE) ;
    } else
            { $post_link = post_permalink(); 
    } ?>

           <li class="ui-state-default <?php echo $box_size;?>">
             <article <?php post_class() ?>>
        <?php   if ( $ad_code ) { ?> <div class="ad-box <?php echo $overlay_class;?>"> <?php echo $ad_code; } else { 
             echo  '<a href="'.$post_link.'" ><img src="'.esc_url($box_image).'" alt="Image via xxxxxxxxxx.com" /></a>'; ?>

                    <div class="post-box <?php echo $overlay_class;?>">

                    <?php if ( c3m_get_field( $prefix.'text_display', FALSE) ) {  ?>

                        <h2><a href="<?php echo $post_link?>"><?php the_title();?></a></h2>

                        <p><?php 
                            if ( $source ) { echo '(' .$source. ') '; }
                            if ( $box_size == 'double-single' ) { echo substr( $post->post_excerpt, 0, 300) .'...'; ?></p> <?php
                            } else {
                            echo substr( $post->post_excerpt, 0, 115) .'...' ; ?></p> <?php } ?>

                        <?php } ?>

                        <?php } ?>

So the question is How do take 4 cr break lines pasted into 1 meta box and assign a variable to each line.

Thanks and I'll hang up and listen.

Edit:

The information pasted will be 4 lines

First Line: Title

Second Line: Source

Third Line: Short excerpt

Fourth Line: Link Url

Example:

After 4 Years, Egypt Reopens Its Border With Gaza Strip

New York Times

Hundreds of Palestinian residents of the Gaza Strip arrived here by the busload Saturday to pass through the reopened border into Egypt. http://www.nytimes.com/2011/05/29/world/middleeast/29egypt.html?_r=1&hp

enter image description here

That input will look like this on the front end. The title links to the link url:

enter image description here

share|improve this question
    
Can you str_replace the /r into /n for the meta box? I'm not sure if this is what you need, just an idea. –  Wyck May 28 '11 at 2:57
    
So the user has the content somewhere and needs to paste it into the post editor? Would it be an idea to do everything in Javascript on the client side? So keep the separate custom fields, but add an extra custom field that, when pasted into, splits up the content and fills in the other fields. This way you have the benefit of separate data entry (and manipulation later!) and easy initial entry. Do you have an example of what the data that will be copied looks like? –  Jan Fabry May 28 '11 at 9:39
    
@Jan yes the user collects all the information into a text file then wants to be able to quickly add it to the site. I edited the question with an example. Javascript would be GREAT idea and wouldn't require changing all the templates. –  Chris_O May 28 '11 at 16:50
    
Do you need help with the Javascript? I won't be able to reply until tomorrow, but it's not rocket science from here on. –  Jan Fabry May 28 '11 at 17:35
    
@Jan Yes Please! –  Chris_O May 28 '11 at 18:14

1 Answer 1

up vote 2 down vote accepted

This is a very simple script that will split up the text of the box in different lines and fill them in in the title, excerpt and two custom fields.

jQuery( function( $ ) {
    $( '#wpse18528_paste_box' ).keyup( function() {
        var text = $( '#wpse18528_paste_box' ).val();
        var lines = text.split( /[\n\r]+/ );
        $( '#title' ).val( lines[0] );
        $( '#title' ).siblings( '#title-prompt-text' ).css( 'visibility', 'hidden' );
        $( '#excerpt' ).val( lines[2] );
        $( '#wpse18528_source' ).val( lines[1] );
        $( '#wpse18528_link' ).val( lines[3] );
    } );
} );

The current regex is one or more linebreaks - I don't know if you want this?

Also, instead of triggering this on each keyup (which works nicely with pasting and editing small errors in the "lazy box"), you can also add a "Split" button and use the click event of it.

share|improve this answer
    
Worked great. Thank you. –  Chris_O May 29 '11 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.