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'm currently working my first plugin (to deal with my CPT) and am at the process of saving the values of a meta box.

I wish to add an underscore (_) to the meta key so that it is hidden on the custom field part.

I have some trouble to get the data saved when this _ is added (no problem when there's no _).

So I try to investigate what goes in the meta array, but I can't manage to have an echo or print_r within a function in my plugin that would print on my admin page.

I have wrote the following

function ccp_portfolio_item_info_meta_box_save( $post_id, $post ) {

    if ( !isset( $_POST['ccp-portfolio-item-info-nonce'] ) || !wp_verify_nonce( $_POST['ccp-portfolio-item-info-nonce'], basename( __FILE__ ) ) )
        return;

    $meta = array(
        '_portfolio_item_url' => esc_url( $_POST['ccp-portfolio-item-url'] )
    );

    print_r($meta);
    // ccp_split_meta_save ($meta, $post_id, $post); // calling my function to split the metas
}

but that would not print my $meta on top of my admin page, what am I doing wrong?

@brasofilo

thank you for your answer

first my form is as follow

function ccp_portfolio_item_info_meta_box_display( $post, $metabox ) {

    wp_nonce_field( basename( __FILE__ ), 'ccp-portfolio-item-info-nonce' ); ?>

    <p>
        <label for="ccp-portfolio-item-url"><?php _e( 'Project <abbr title="Uniform Resource Locator">URL</abbr>', 'custom-content-portfolio' ); ?></label>
        <br />
        <input type="text" name="ccp-portfolio-item-url" id="ccp-portfolio-item-url" value="<?php echo esc_url( get_post_meta( $post->ID, '_portfolio_item_url', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" />
    </p>
    <?php

    /* Allow devs to hook in their own stuff here. */
    do_action( 'ccp_item_info_meta_box', $post, $metabox );
}

then my save function is :

function ccp_portfolio_item_info_meta_box_save( $post_id, $post ) {

    if ( !isset( $_POST['ccp-portfolio-item-info-nonce'] ) || !wp_verify_nonce( $_POST['ccp-portfolio-item-info-nonce'], basename( __FILE__ ) ) )
        return;

    $meta = array(
        '_portfolio_item_url' => esc_url( $_POST['ccp-portfolio-item-url'] )
    );


    ccp_split_meta_save ($meta, $post_id, $post); // calling my function to split the metas
}

up to there all is fine, when I print_r the $metal, it show the right thing

then within the previous function I call this one

function ccp_split_meta_save ($meta, $post_id, $post) {

echo 'je suis dans ccp_split_meta_save <br />';
print_r($meta);
    //exit();

    foreach ( $meta as $meta_key => $new_meta_value ) {

            /* Get the meta value of the custom field key. */
            $meta_value = get_post_meta( $post_id, $meta_key, true );

            echo '<br />la meta key est '.$meta_key.'</ br>';
            echo '<br />la meta value  est '.$meta_value.'</ br>';
            // exit();

            /* If there is no new meta value but an old value exists, delete it. */
            if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value )
                {//delete_post_meta( $post_id, $meta_key, $meta_value );
                echo '<br />je suis dans delete <br />';
                }

            /* If a new meta value was added and there was no previous value, add it. */
            elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
            {
                //{add_post_meta( $post_id, $meta_key, $new_meta_value, true );
                //echo '<br /> c est une nouvelle meta value <br />'; exit();
                echo '<br />je suis dans add <br />';
            }   
                //}

            /* If the new meta value does not match the old value, update it. */
            elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $new_meta_value && $new_meta_value != $meta_value )
            {   //update_post_meta( $post_id, $meta_key, $new_meta_value );
                echo '<br />je suis dans update <br />';
            }
            else 
            {
            echo '<br /><b>je suis nul part</b> <br />';
            echo 'add_post_meta : ' .current_user_can( 'add_post_meta', $post_id, $meta_key ).'<br />';
            }

    exit();
    }

}

as you see it is a working/denug function since I have put some pointer to see where my datas are

the strange thing is that if I have

$meta = array( 'portfolio_item_url' => esc_url( $_POST['ccp-portfolio-item-url'] )

then it goes well to elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value ) so a new entry is add to the db

if I have

$meta = array( '_portfolio_item_url' => esc_url( $_POST['ccp-portfolio-item-url'] )

then it goes to my last else, which means nowhere

if I print $meta_value = get_post_meta( $post_id, $meta_key, true );

then it well return an empty value so that would direct me to

        /* If a new meta value was added and there was no previous value, add it. */
        elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
        {
            //{add_post_meta( $post_id, $meta_key, $new_meta_value, true );
            //echo '<br /> c est une nouvelle meta value <br />'; exit();
            echo '<br />je suis dans add <br />';
        }   

but no it is not

I just don't understant that why adding only the underscore makes trouble

thank you in advance for your help

@ toscho, sorry I did not really understand how to update my question, but it looks like I found out.. thank you and sorry for my rookiness

share|improve this question

1 Answer 1

After the save_post hook is executed, the page is redirected, so you're not able to see the print_r output.

A simple method, if you can afford it, is to do a wp_die() just after the print_r or var_dump.

Or you can check other debugging methods, like FirePHP.

share|improve this answer
    
thank you brasofilo, I have ended up to use the exit(); function to see what prints... the strange hing is that if $meta = array( '_portfolio_item_url' => esc_url( $_POST['ccp-portfolio-item-url'] ) then it does not go well thru my check if it is $meta = array( 'portfolio_item_url' => esc_url( $_POST['ccp-portfolio-item-url'] ) (no underscore it works..) –  Steffy Kristiensen Mar 31 '13 at 6:37
    
@SteffyKristiensen, can you post the "meta save" function? You're free to update the Question. –  brasofilo Mar 31 '13 at 14:15

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.