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 have this code.

<?php
add_action('add_meta_boxes', 'ct_meta_add');
add_action('save_post','ct_meta_save');
add_action('save_post','ct_parent');
// add meta functions
function ct_meta_add() {
    add_meta_box(
        'ct_chap', //id
        'Thông tin chương', //title
        'ct_meta_show', //callback function
        'chap', //post type     
        'normal' //priority
    );
}
function get_blogspot_url($string) {
    preg_match_all('/<a href="(.*?)"/', $string, $matches);
    $string = '';
    foreach($matches[1] as $val) {
        $string .= $val."\n";
    }
    $string = str_replace('s1600', 's0', $string);
    return $string;
}
// save meta functions
function ct_meta_save() {
    global $post;   
    /* Save meta */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( !wp_verify_nonce( $_POST['ct_nonce'], plugin_basename( __FILE__ ) ) ) return;
    if( !current_user_can( 'edit_post' ) ) return;  
    $meta = array('ct_download_link', 'ct_list_url', 'ct_list_url2');
    foreach($meta as $dt) {     
        if($dt == 'ct_list_url2') {         
            $_POST[$dt] = get_blogspot_url($_POST[$dt]);
        }
        if(isset($_POST[$dt]) && $_POST[$dt] !== '') {
            update_post_meta($post->ID, $dt, $_POST[$dt]);
        } else {
            delete_post_meta($post->ID, $dt);   
        }       
    }   
}
?>
<?php
function ct_parent() { 
    global $post;
    if($_POST['ct_truyen'] && $_POST['ct_truyen'] != "") {
        //lưu hoặc thay đổi truyện của chap
        $ct_post = array();
        $ct_post['ID'] = $post->ID;
        $ct_post['post_parent'] = $_POST['ct_truyen'];  
        //thay đổi thời gian cập nhật truyện
        $tr_post = array();
        $tr_post['ID'] = $_POST['ct_truyen'];   
        $tr_post['post_modified_gmt'] = current_time('mysql');
        $tr_post['post_modified'] = current_time('mysql',7);        
        remove_action('save_post','ct_parent');     
        remove_action('save_post','tr_parent'); 
        wp_update_post($ct_post);   
        wp_update_post($tr_post);   
        add_action('save_post','ct_parent');
    }
}   
?>
<?php //save_meta function
function ct_meta_show() { ?>
    <?php
        global $post;
        $ct_download_link = get_post_meta($post->ID, 'ct_download_link', true) ? get_post_meta($post->ID, 'ct_download_link', true) : '';
        $ct_list_url = get_post_meta($post->ID, 'ct_list_url', true) ? get_post_meta($post->ID, 'ct_list_url', true) : '';
        $ct_list_url2 = get_post_meta($post->ID, 'ct_list_url2', true) ? get_post_meta($post->ID, 'ct_list_url2', true) : '';
    ?>
    <?php wp_nonce_field(plugin_basename(__FILE__), 'ct_nonce');  ?>    
    <table class="form-table">
    <tbody>    
        <tr>
            <th><label for="ct_download_link">Truyện</label><a class="add-chap-truyen" href="<?php echo admin_url('post-new.php?post_type=truyen'); ?>">thêm truyện</a></th>
            <td>
                <?php
                global $post;
                global $wpdb;
                $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'truyen' AND post_status = 'publish' ORDER BY post_title";
                $results = $wpdb->get_results($query, OBJECT);
                echo '<select name="ct_truyen" id="ct_truyen">';
                if(!$_GET['truyen_id'] || !get_the_title($_GET['truyen_id'])) {
                    echo '<option value = "">None</option>';
                    foreach ($results as $r) {
                        echo '<option value="', $r->ID, '"', $r->ID == $post->post_parent ? ' selected="selected"' : '', '>', $r->post_title, '</option>';
                    }
                } else {
                    foreach ($results as $r) {
                        echo '<option value="', $r->ID, '"', $r->ID == $_GET['truyen_id'] ? ' selected="selected"' : '', '>', $r->post_title, '</option>';
                    }
                }
                echo '</select>';
                ?>
            </td>
        </tr>

        <tr>
            <th><label for="ct_download_link">Địa chỉ tải chương</label></th>
            <td><input type="text" name="ct_download_link" class="regular-text" value="<?php echo $ct_download_link; ?>"/></td>
        </tr>

        <tr>
            <th><label for="ct_list_url">Địa chỉ ảnh đọc online</label></th>
            <td>
                <label for="ct_list_url">Địa chỉ ảnh 1</label>
                <p><textarea name="ct_list_url" rows="20" class="large-text code"><?php echo $ct_list_url; ?></textarea></p>
                <label for="ct_list_url2">Địa chỉ ảnh 2</label>
                <p><textarea name="ct_list_url2" rows="20" class="large-text code"><?php echo $ct_list_url2; ?></textarea></p>
            </td>
        </tr>        
    </tbody>
    </table>    
<?php } ?>
<?php
//auto chap title
add_filter( 'default_title', 'ct_auto_title');
function ct_auto_title($title) {
    if($_GET['truyen_id'] && get_the_title($_GET['truyen_id'])) {
        $chap_number = get_number_chap($_GET['truyen_id']);
        $title = get_the_title($_GET['truyen_id']) . ' chương ' . ($chap_number+1);
        return $title;
    } else {
        return $title;
    }
}
function ct_remove_excess_columns( $columns ) {

    unset($columns['author']);
    unset($columns['comments']);
    unset($columns['wpseo-score']);
    unset($columns['wpseo-title']);
    unset($columns['wpseo-focuskw']);
    unset($columns['wpseo-metadesc']);  
    return $columns;    
}
add_filter( 'manage_edit-chap_columns' , 'ct_remove_excess_columns' );
?>

everything is OK, but when i was saving, the function get_blogspot_url($_POST[$dt]) didn't work because $_POST[$dt].

When i replace get_blogspot_url($_POST[$dt]) with,

get_blogspot_url('<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-sfO5YNlgW0Y/UFCaPberRcI/AAAAAAAAANM/1ccm6QRHxLM/s1600/0000.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-sfO5YNlgW0Y/UFCaPberRcI/AAAAAAAAANM/1ccm6QRHxLM/s320/0000.png" width="240" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-a1bQNURFQU0/UFCaRm88fwI/AAAAAAAAANU/uDnHUsvNvS8/s1600/img000005.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-a1bQNURFQU0/UFCaRm88fwI/AAAAAAAAANU/uDnHUsvNvS8/s320/img000005.png" width="199" /></a></div>
<br />')

it works, so i think $_POST[$dt] have problem, can you help me?

You can download php file: https://dl.dropbox.com/u/52812919/chap-meta.rar :( Sorry about my bad english

share|improve this question
    
I'm missing something. What is the value of $dt? –  marfarma Sep 24 '12 at 15:02

1 Answer 1

From what I can see there is no value set for ct_list_url2, when this code block executes,

$meta = array('ct_download_link', 'ct_list_url', 'ct_list_url2');
foreach($meta as $dt) {     
    if($dt == 'ct_list_url2') {         
        $_POST[$dt] = get_blogspot_url($_POST[$dt]);
    }
    if(isset($_POST[$dt]) && $_POST[$dt] !== '') {
        update_post_meta($post->ID, $dt, $_POST[$dt]);
    } else {
        delete_post_meta($post->ID, $dt);   
    }
...

...in fact that goes for any of the values held with in your $meta array. Your posted values are no where to be found so you need to pass them into your function.

Instead try something like,

foreach($_POST as $key => $val) { 

    $url = get_blogspot_url($val);

    if(isset($key) && $val !== '') {

       update_post_meta($post->ID, $key, $url);

    } else {

       delete_post_meta($post->ID, $key); 

}

You need to be careful though because if any of your submitted POST variables have a $key that returns an empty $val then your else condition will run and delete any meta values stored for the given $key. So if there was a value previously stored in that field it will be deleted. Not sure if this is your intended goal?

If you only want to run your ct_list_url2 POST variable through your get_blogspot_url function, excluding the other $keys then you can do something like,

foreach($_POST as $key => $val) { 

    if(isset($key) && $val !== '') {

        if($key == 'ct_list_url2') {

        $url = get_blogspot_url($val);
        update_post_meta($post->ID, $key, $url);

        } else {

        //uses $val instead of $url to not run through get_blogspot_url() function
        update_post_meta($post->ID, $key, $val); 

        } 

    } else {

       delete_post_meta($post->ID, $key); 

    }

}

I hope that helps!

share|improve this answer
    
thanks for your answer but it still not working. @@ –  Tuan Au Sep 16 '12 at 5:32
    
after testing, I recognize the problem is $_POST and preg_match_all? if I set $string = '<div ...' => it's working; if I set $string = $_POST['string'] => it isn't working ? –  Tuan Au Sep 16 '12 at 5:56

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.