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.

The upload button is not working when put inside javascript, but it works when I put it outside the javascript.

/* -----------------------upload button------------------ */
function multiple_media_upload_js() {
    $js_path = get_bloginfo('template_directory');
    wp_enqueue_script('thickbox');
    wp_register_script('my-upload', $js_path .'/assets/js/admin.js', array('jquery','media-upload','thickbox'));
    wp_enqueue_script('my-upload');
}

function multiple_media_upload_css() {wp_enqueue_style('thickbox');}

add_action('admin_print_scripts', 'multiple_media_upload_js');
add_action('admin_print_styles', 'multiple_media_upload_css');



/*  -----------------------MeTA Box-------------------------- */
add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
    add_meta_box(
        'dynamic_sectionid',
        __( 'Client Information', 'myplugin_textdomain' ),
        'dynamic_inner_custom_box',
        'page');
}

/* Prints the box content */
function dynamic_inner_custom_box() {
    global $post;
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $ourwork = get_post_meta($post->ID,'ourwork',true);

    $c = 0;
    if ( is_array( $ourwork ) ) {
        foreach( $ourwork as  $track ) {
            if ( isset( $track['thumb'] ) || isset( $track['client-img1'] ) || isset( $track['client-img2'] ) || isset( $track['client-img3'] ) || isset( $track['client-img4'] ) || isset( $track['client-desc'] ) ) {
                printf( '<p><strong>Thumb Image</strong> &nbsp;:&nbsp;<input type="text" name="ourwork[%1$s][thumb]" value="%2$s" size="50" />&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/><strong>Client Image 1</strong> : <input type="text" name="ourwork[%1$s][client-img1]" value="%3$s" size="50" />&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/>
                <strong>Client Image 2</strong> : <input type="text" name="ourwork[%1$s][client-img2]" value="%4$s" size="50"/>&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/>
                <strong>Client Image 3</strong> : <input type="text" name="ourwork[%1$s][client-img3]" value="%5$s" size="50" />&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/>
                <strong>Client Image 4</strong> : <input type="text" name="ourwork[%1$s][client-img4]" value="%6$s" size="50" />&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/>
                <strong>client Description</strong> :<br/><textarea name="ourwork[%1$s][client-desc]" cols="75" rows="6" >%7$s</textarea><br/>
                <span class="remove">%8$s</span></p>', $c, $track['thumb'], $track['client-img1'], $track['client-img2'] , $track['client-img3'] , $track['client-img4'], $track['client-desc'],  __( '<span class="button">Remove Section</span>' ) );
                $c = $c +1;
            }
        }
    }

    ?>
<span id="here"></span>
<span class="add"><?php _e('<span class="button">Add Section</span>'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".add").click(function() {
            count = count + 1;

            $('#here').append('<p><strong>Thumb Image</strong> &nbsp;:&nbsp;<input type="text" id="ourwork['+count+'][thumb]" name="ourwork['+count+'][thumb]" value="" size="50"/>&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/><strong>Client Image 1</strong> : <input type="text" name="ourwork['+count+'][client-img1]" value="" size="50"/>&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/><strong>Client Image 2</strong> : <input type="text" name="ourwork['+count+'][client-img2]" value="" size="50"/>&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/><strong>Client Image 3</strong> : <input type="text" name="ourwork['+count+'][client-img3]" value="" size="50"/>&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/><strong>Client Image 4</strong> : <input type="text" name="ourwork['+count+'][client-img4]" value="" size="50"/>&nbsp;<input class="upload_image_button" id="_btn" type="button" value="Upload" /><br/><br/><strong>Client Description</strong> :<br/><textarea  name="ourwork['+count+'][client-desc]" cols="75" rows="6"></textarea><br/><span class="remove"><span class="button">Remove Section</span></span></p>' );
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}


/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
    // verify if this is an auto save routine.
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !isset( $_POST['dynamicMeta_noncename'] ) )
        return;

    if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
        return;

    // OK, we're authenticated: we need to find and save the data

    $ourwork = $_POST['ourwork'];

    update_post_meta($post_id,'ourwork',$ourwork);
}
share|improve this question
    
ok i have changed the question and checked the {}. It works fine when i add the button outside javascript. but does not work when it is inside the script –  Azad Rana Aug 10 '13 at 14:59
1  
What do you mean by "not working"? What are you expecting and what are you getting or not getting? –  Charles Clarkson Aug 10 '13 at 15:01
    
there is this button <input class="upload_image_button" id="_btn" type="button" value="Upload" /> when clicked opens a media library but it is not working when used inside jquery –  Azad Rana Aug 10 '13 at 15:14
    
is there any solution to this –  Azad Rana Aug 10 '13 at 15:58
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.