0

I created a custom post type and metabox for uploading another image. the problem is when i add the image its shown correctly in its place in backend and when i update its also the same i had the image displayed.

But when i get back to the the post to modify it i can't find the picture and if i make an update to the post so there is no picture to be displayed.

<?php   
function hotels_meta_boxes() {
add_meta_box( 'hotels_form', 'Hotels - Détails', 'hotels_form', 'hotels', 'normal', 'high' );} function hotels_form() {
$post_id = get_the_ID();
$hotel_data = get_post_meta( $post_id, '_hotel', true );
$carte = ( empty( $hotel_data['carte'] ) ) ? '' : $hotel_data['carte'];
wp_nonce_field( 'hotels', 'hotels' );
?>  
    <h2 class="metaboxtitle">Coordonnées</h2>
<?php 
    global $post;

    $image_src = '';

    $image_id = get_post_meta( $post->ID, '_hotel', true );
    $image_src = wp_get_attachment_url( $image_id );

    ?>
    <img id="book_image" src="<?php echo $image_src ?>" style="max-width:100%;" />
    <input type="hidden" name="hotel[carte]" id="upload_image_id" value="<?php echo $image_id; ?>" />

        <a title="<?php esc_attr_e( 'Set book image' ) ?>" href="#" id="set-book-image"><?php _e( 'Set book image' ) ?></a>
        <a title="<?php esc_attr_e( 'Remove book image' ) ?>" href="#" id="remove-book-image" style="<?php echo ( ! $image_id ? 'display:none;' : '' ); ?>"><?php _e( 'Remove book image' ) ?></a>
    </p>

    <script type="text/javascript">
    jQuery(document).ready(function($) {

        // save the send_to_editor handler function
        window.send_to_editor_default = window.send_to_editor;

        $('#set-book-image').click(function(){

            // replace the default send_to_editor handler function with our own
            window.send_to_editor = window.attach_image;
            tb_show('', 'media-upload.php?post_id=<?php echo $post->ID ?>&amp;type=image&amp;TB_iframe=true');

            return false;
        });

        $('#remove-book-image').click(function() {

            $('#upload_image_id').val('');
            $('img').attr('src', '');
            $(this).hide();

            return false;
        });

        // handler function which is invoked after the user selects an image from the gallery popup.
        // this function displays the image and sets the id so it can be persisted to the post meta
        window.attach_image = function(html) {

            // turn the returned image html into a hidden image element so we can easily pull the relevant attributes we need
            $('body').append('<div id="temp_image">' + html + '</div>');

            var img = $('#temp_image').find('img');

            imgurl   = img.attr('src');
            imgclass = img.attr('class');
            imgid    = parseInt(imgclass.replace(/\D/g, ''), 10);

            $('#upload_image_id').val(imgid);
            $('#remove-book-image').show();

            $('img#book_image').attr('src', imgurl);
            try{tb_remove();}catch(e){};
            $('#temp_image').remove();

            // restore the send_to_editor handler function
            window.send_to_editor = window.send_to_editor_default;

        }

    });
    </script>

</p>
<?php } add_action( 'save_post', 'hotels_save_post' );/*** Data validation and saving** This functions is attached to the 'save_post' action hook.*/ function hotels_save_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;

if ( ! empty( $_POST['hotels'] ) && ! wp_verify_nonce( $_POST['hotels'], 'hotels' ) )
    return;

if ( ! empty( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
    if ( ! current_user_can( 'edit_page', $post_id ) )
        return;
} else {
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;
}

if ( ! wp_is_post_revision( $post_id ) && 'hotels' == get_post_type( $post_id ) ) {
    remove_action( 'save_post', 'hotels_save_post' );

    wp_update_post( array(
        'ID' => $post_id,
    ) );

    add_action( 'save_post', 'hotels_save_post' );
}

if ( ! empty( $_POST['hotel'] ) ) {
    $hotel_data['carte'] = ( empty( $_POST['hotel']['carte'] ) ) ? '' : sanitize_text_field( $_POST['hotel']['carte'] );
    update_post_meta( $post_id, '_hotel', $hotel_data );
} else {
    delete_post_meta( $post_id, '_hotel' );
}} add_filter( 'manage_edit-hotels_columns', 'hotels_edit_columns' );/******************** and to show it i use *************************/if ( $query->have_posts() ) {
    while ( $query->have_posts() ) : $query->the_post();
        $post_id = get_the_ID();
        $hotel_data = get_post_meta( $post_id, '_hotel', true );
        $carte = ( empty( $hotel_data['carte'] ) ) ? '' : $hotel_data['carte'];
        $hotels .= '<div class="hotel_map">';
                $carte = ( empty( $hotel_data['carte'] ) ) ? 'Map introuvable' : $hotel_data['carte'];
            $hotels .= '<img src="' . wp_get_attachment_url( $carte ) . '" style="margin-top: 10px;"/>' ;
            $hotels .= '</div>';//fin map
            endwhile;
    wp_reset_postdata();
}
else {
    echo 'Pas d\'hotels pour l\'instant';
}
return $hotels;}

1 Answer 1

0

I have no time to write at your code, but with your help works for me

function wms_add_headerimage_cb( $post ){
//campuri
$values = get_post_custom( $post->ID );

$headerpageimage        = isset( $values['headerpageimage'][0] ) ? esc_attr( $values['headerpageimage'][0] ) : '';
$img_src                = wp_get_attachment_image_src( $headerpageimage, 'medium' );

wp_nonce_field( 'allpages_meta_box', 'allpages_meta_box_nonce' );
?>
    <p>
        <img id="headerpage_image" src="<?php echo $img_src[0] ?>" style="max-width:100%;" />
        <input type="hidden" name="headerpageimage" id="headerpageimage" value="<?php echo $headerpageimage; ?>" />

    <a title="<?php esc_attr_e( 'Set image' ) ?>" href="#" id="set-book-image"><?php _e( 'Set image' ) ?></a>
    <a title="<?php esc_attr_e( 'Remove image' ) ?>" href="#" id="remove-headerpageimage" style="<?php echo ( ! $headerpageimage ? 'display:none;' : '' ); ?>"><?php _e( 'Remove image' ) ?></a>
</p>
<script type="text/javascript">
jQuery(document).ready(function($) {

    // save the send_to_editor handler function
    window.send_to_editor_default = window.send_to_editor;

    $('#set-book-image').click(function(){

        // replace the default send_to_editor handler function with our own
        window.send_to_editor = window.attach_image;
        tb_show('', 'media-upload.php?post_id=<?php echo $post->ID ?>&amp;type=image&amp;TB_iframe=true');

        return false;
    });

    $('#remove-headerpageimage').click(function() {

        $('#headerpageimage').val('');
        $('img').attr('src', '');
        $(this).hide();

        return false;
    });

    // handler function which is invoked after the user selects an image from the gallery popup.
    // this function displays the image and sets the id so it can be persisted to the post meta
    window.attach_image = function(html) {

        // turn the returned image html into a hidden image element so we can easily pull the relevant attributes we need
        $('body').append('<div id="temp_image">' + html + '</div>');

        var img = $('#temp_image').find('img');

        imgurl   = img.attr('src');
        imgclass = img.attr('class');
        imgid    = parseInt(imgclass.replace(/\D/g, ''), 10);

        $('#headerpageimage').val(imgid);
        $('#remove-headerpageimage').show();

        $('img#headerpage_image').attr('src', imgurl);
        try{tb_remove();}catch(e){};
        $('#temp_image').remove();

        // restore the send_to_editor handler function
        window.send_to_editor = window.send_to_editor_default;

    }

});
</script>
<?php

}

add_action( 'save_post', 'headerimage_save' ); function headerimage_save( $post_id ){

if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
if( !isset( $_POST['allpages_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['allpages_meta_box_nonce'], 'allpages_meta_box' ) ) return $post_id;    
if( !current_user_can( 'edit_post' ) ) return $post_id;  
$allowed = array(   
    'a' => array( // on allow a tags  
        'href' => array() // and those anchors can only have href attribute  
    )  
);

// quantity Icon
if( isset( $_POST['headerpageimage'] ) )  update_post_meta( $post_id, 'headerpageimage', esc_attr( $_POST['headerpageimage']) );

}

1
  • Thanks for your answer, but i didn't get exactly what you wanna say (why did you create a function). I left the first part as it was and i added this lines to my code . $allowed = array( 'a' => array( // on allow a tags 'href' => array() // and those anchors can only have href attribute ) ); // quantity Icon if( isset( $_POST['upload_image_id'] ) ) update_post_meta( $post_id, 'upload_image_id', esc_attr( $_POST['upload_image_id']) ); I used the id of hidden input on it, is that true?? but nothing changed after updating, i can't find the image on his place again. Apr 1, 2015 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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