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 am making a function to parse a .csv file uploaded in a custom field of a custom post. The name of the custom phone and the field's name is plan_information . The problem is when I var_dump(get_post_meta($post_id, 'plan_information', TRUE)) , I get an empty string. The code I use is the following:

add_action('publish_phone', 'parse_csv', '99', 2);

function parse_csv($post_id) {

global $post;

$csv_file = get_post_meta($post_id, 'plan_information', TRUE);

$csv_file = wp_get_attachment_url($csv_file);

$title = get_post_meta($post_id, 'manufacturer', TRUE);
$title .= ' '.get_post_meta($post_id, 'model', TRUE);
$title .= ' '.get_post_meta($post_id, 'memory', TRUE);

$url = get_bloginfo('wpurl').'/';

$csv_file = str_replace($url, ABSPATH, $csv_file);

if($csv_file && !$carriers) {
    $fp = fopen($csv_file ,"r");
    if($fp) {
        fgetcsv($fp, 1000, ",");

        while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)
        {

            $new_carrier = array(
                'post_title' => $title. ' '. $data[0] . ' ' . $data[3],
                'post_status' => 'publish',
                'post_type' => 'carrier'
                );
            $post_id = wp_insert_post($new_carrier);
            add_post_meta($post_id, 'operator', $data[0], true);
            add_post_meta($post_id, 'logo', $data[1], true);
            add_post_meta($post_id, 'cost', $data[2], true);
            add_post_meta($post_id, 'plan_name', $data[3], true);
            add_post_meta($post_id, 'hero', $data[4], true);
            add_post_meta($post_id, 'summary', $data[5], true);
            add_post_meta($post_id, 'phone_id', $post_id, true);
        }
    }

    fclose($fp);
}

}
share|improve this question
    
What's the publish_phone hook? Have you checked that your function gets called? –  Stephen Harris Jun 9 '12 at 19:11
    
It's this hook adambrown.info/p/wp_hooks/hook/… . Yes if I die("test") it shows test on publish. –  Bdesign Jun 9 '12 at 19:15
    
So the meta data is in the database? Does $data contain what you expect, and what does add_post_meta return? –  Stephen Harris Jun 9 '12 at 19:32
    
I have fixed this using save_post action. –  Bdesign Jun 9 '12 at 20:10
    
Post your code as an answer then please :). Also beware of infinite loops: wp_insert_post triggers save_post. –  Stephen Harris Jun 9 '12 at 21:29
add comment

1 Answer 1

This is how I managed to fix it, Thanks to Stephen Harris.

add_action('save_post', 'parse_csv', 10, 2);

function parse_csv($post_id) {

    $_pid = $post_id;
        // Autosave, do nothing
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
            return;
    // AJAX? Not used here
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
            return;
    // Check user permissions
    if ( ! current_user_can( 'edit_post', $post_id ) )
            return;
    // Return if it's a post revision
    if ( false !== wp_is_post_revision( $post_id ) )
            return;
    if('phone' == $_POST['post_type']) {

        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;

        $csv_file = get_post_meta($post_id, 'plan_information', TRUE);
        $csv_file = wp_get_attachment_url($csv_file);
        $title = get_post_meta($post_id, 'manufacturer', TRUE). ' '.get_post_meta($post_id, 'model', TRUE).' '.get_post_meta($post_id, 'memory', TRUE);
        $url = get_bloginfo('wpurl').'/';

        $csv_file = str_replace($url, ABSPATH, $csv_file);

        $carriers = get_posts(array('post_type' => 'carrier', 'meta_key' => 'phone_id', 'meta_value' => $_pid));

        update_post_meta($_pid, "_wp_page_template", "phone-page.php");

        if($csv_file && !$carriers) {
            $fp = fopen($csv_file,"r");
            if($fp) {
                fgetcsv($fp, 1000, ",");

                while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)
                {

                    $new_carrier = array(
                        'post_title' => $title . '-' .$data[3],
                        'post_status' => 'publish',
                        'post_type' => 'carrier'
                        );

                    $post_id = wp_insert_post($new_carrier);
                    add_post_meta($post_id, 'operator', $data[0], true);
                    add_post_meta($post_id, 'logo', $data[1], true);
                    add_post_meta($post_id, 'cost', $data[2], true);
                    add_post_meta($post_id, 'plan_name', $data[3], true);
                    add_post_meta($post_id, 'hero', $data[4], true);
                    add_post_meta($post_id, 'summary', $data[5], true);
                    add_post_meta($post_id, 'phone_id', $_pid, true);
                }

                fclose($fp);
            }
        }
    }

}
share|improve this answer
add comment

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.