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've installed Meta Box wordpress plugin and create a text field meta box with clone option:

add_action( 'admin_init', 'rw_register_meta_boxes' );
function rw_register_meta_boxes()
{
    $meta_boxes = array();
    $meta_boxes[] = array(
            'id'    => 'vip',
            'title'    => 'VIP Links',
            'pages'    => array( 'post'),
            'fields' => array(
                array(
                    'name'  => 'Download links',
                    'id'    => "vip",
                    'desc'  => 'Type here',
                    'type'  => 'text',
                    'std'   => '',
                    'size' => 100,
                    'clone' => true,
                ),
            ),
            'validation' => array(
                'rules' => array(
                    "vip" => array(
                        'required'  => false,
                    ),
                )
            )
        );
    foreach ( $meta_boxes as $meta_box )
    {
        new RW_Meta_Box( $meta_box );
    }
}

it works and meta box is added. But the problem is that, I need to do some works on values while post publishing and then update the meta box values:

$VIP = get_post_meta($pid, 'vip', false);
/*
    do some operations here
 */
update_post_meta($pid, 'vip', $NEW_VIP);

This snippet is within a function that's called on post publish: add_action('publish_post', 'function_name');

But It won't update! I tried to see if function runs and which values $NEW_VIP contains. So I added a line before update_post_meta() function to assure myself:

print_r($NEW_VIP); die();

Output is as expected and $NEW_VIP has correct values (is an array) but update_post_meta doesn't save it and meta box value(s) will always remain unchangeable.

Any help would really be appreciated.

Update #1

Full Function:

function dl_url($pid)
{
    global $wpdb;
    $VIP = array_filter(get_post_meta($pid, 'vip', false));
    //$urls = array();
    //print_r($VIP); die();
    if(!wp_is_post_revision($pid) && $VIP[0])
    {
        foreach ($VIP[0] as $mid => $link)
        {
            if(preg_match("/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(\/.*)?$/i", $link))
            {
                $rand = rand(1000,99999999);
                $hash = strrev(md5($link.$rand));
                $file = pathinfo($link);
                $filename = $file['basename'];
                $size = get_size($link);
                $price = get_post_meta($pid, 'price', true);
                if(!$price)
                    $price = 0;
                if($row = $wpdb->get_row("SELECT * FROM wp_dlurl WHERE pid = '{$pid}' AND hash = '{$hash}'",ARRAY_A)) // check if existed in db
                {
                    // if existed update url
                    $arr = array('price' => $price);
                    $format = array(0 => '%d');
                    $home = str_replace(array("/",":"),array("\/","\:"),get_bloginfo('url'));
                    if(!preg_match("/".$home."\/\?download\=\w{32}/", $link))
                    {
                        $arr += array('url' => $link, 'filename' => $filename, 'size' => $size, 'hash' => $hash);
                        $format += array(1 => '%s', 2 => '%s', 3 => '%d', 4 => '%s');
                        $urls[] = get_bloginfo('url')."/?download=".$hash;
                        //update_post_meta($pid, 'vip', get_bloginfo('url')."/?download=".$hash);
                    }
                    $wpdb->update(REV_DL, $arr, array('pid' => $pid, 'hash' => $row['hash']), $format, array('%d','%s'));
                }
                else
                {
                    // if not existed insert
                    $wpdb->insert(REV_DL, array('pid' => $pid, 'price' => $price, 'url' => $link, 'filename' => $filename, 'size' => $size, 'hash' => $hash), array('%d','%d','%s','%s','%d','%s'));
                    $urls[] =  get_bloginfo('url')."/?download=".$hash;
                    //print_r($urls);
                    //die();
                }
            }
        }
        if(isset($urls))
        {
            //print_r($urls);
            //die();
            $VIP = $urls;
        }
        //print_r($VIP); die();
        update_post_meta($pid, 'vip', $VIP);
        //print_r(get_post_meta($pid, "vip", true));
        //die();

    }
}
add_action('publish_post', 'dl_url', 99);
share|improve this question
    
I come across similar situation. Reason 1: my hook was called earlier than WP hooks therefore my changes were overwritten. Please set priority parameter (third parameter) of add_action to 99. –  M-R Aug 24 '13 at 19:53
    
@M-R nice idea but that doesn't work either :| –  revo Aug 24 '13 at 20:10
    
Post all of the relevant code please. –  s_ha_dum Aug 24 '13 at 20:12
    
@s_ha_dum see update. –  revo Aug 24 '13 at 20:18
    
@s_ha_dum any help? –  revo Aug 26 '13 at 6:17
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.