Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* TEXT UPLOADER FOR TEXT PAGES */
- add_action( 'add_meta_boxes', 'add_custom_text_box' );
- /* Do something with the data entered */
- add_action( 'save_post', 'save_text_postdata' );
- /* Adds a box to the main column on the Post and Page edit screens */
- function add_custom_text_box() {
- add_meta_box(
- 'dynamic_sectionid',
- __( 'Text Contents', 'myplugin_textdomain' ),
- 'inner_text_custom_box',
- 'sc_text');
- }
- /* Prints the box content */
- function inner_text_custom_box() {
- global $post;
- // Use nonce for verification
- wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
- ?>
- <div id="meta_inner">
- <ul>
- <?php
- //get the saved meta as an array
- $images = get_post_meta($post->ID,'images',true);
- $c = 0;
- if(is_array($images)){
- foreach($images as $image ){
- if (isset($image['url'])){
- echo '<li>
- <span class="description">Link Text </span>
- <input class="title" type="text" name="images['.$c.'][link_text]" value="'.$image['link_text'].'" /><br/>
- <span class="description">Link URL </span>
- <input class="year" type="text" name="images['.$c.'][link_url]" value="'.$image['link_url'].'" /><br/>
- <span class="description">Subtext </span>
- <input class="materials" type="text" name="images['.$c.'][subtext]" value="'.$image['subtext'].'" /><br/>
- <span class="description">Image </span>
- <input class="upload" type="text" name="images['.$c.'][url]" value="'.$image['url'].'" />
- <input type="button" name="uploadbutton'.$c.'" id="uploadbutton'.$c.'" class="uploadbutton" value="insert image" />
- <img src="'.$image['url'].'" width="70" height="auto" />
- <span class="remove"><input type="button" name="remove" id="remove" value="remove" /></span>
- </li>';
- $c = $c +1;
- }
- if ( $c == 1 )
- break;
- }
- }
- ?>
- <span id="here"></span>
- <span class="add"><?php echo __('<input type="button" name="addimage" id="addimage" value="add image" />'); ?></span>
- <script>
- var $ =jQuery.noConflict();
- $(document).ready(function() {
- var count = <?php echo $c; ?>;
- if (count == 1){
- $(".add").hide();
- }
- $(".add").click(function() {
- $(".add").hide();
- count = count + 1;
- $('#here').append('<li><span class="description">Link Text </span><input class="title" type="text" name="images['+count+'][link_text]" value="" /><br/><span class="description">Link URL </span><input class="year" type="text" name="images['+count+'][link_url]" value="" /><br/><span class="description">Subtext </span><input class="materials" type="text" name="images['+count+'][subtext]" value="" /><br/><span class="description">Image </span><input class="upload" type="text" name="images['+count+'][url]" value="" /><input type="button" name="uploadbutton'+count+'" id="uploadbutton'+count+'" class="uploadbutton" value="insert image" /><img src="http://dummyimage.com/100x30/ddd/ddd" width="100" height="auto" /><span class="remove"><input type="button" name="remove" id="remove" value="remove" /></span></li>' );
- return false;
- });
- $(".remove").live('click', function() {
- $(this).parent().remove();
- });
- var uploadID = '';
- var thumbsrc = '';
- jQuery('.uploadbutton').live('click', function() {
- uploadID = jQuery(this).prev('input');
- thumbsrc = jQuery(this).next('img');
- formfield = jQuery('.upload').attr('name');
- tempsendtoeditor = window.send_to_editor; // store it for after
- window.send_to_editor = function(html) {
- imgurl = jQuery('img',html).attr('src');
- uploadID.val(imgurl);
- thumbsrc.attr('src', imgurl );
- tb_remove();
- window.send_to_editor = tempsendtoeditor; // restore it to original
- };
- tb_show('', 'media-upload.php?type=image&TB_iframe=true');
- return false;
- });
- });
- </script>
- <script type="text/javascript">
- jQuery(document).ready(function() {
- jQuery("#meta_inner").sortable({
- 'tolerance':'intersect',
- 'cursor':'pointer',
- 'items':'li',
- 'placeholder':'placeholder',
- 'nested': 'ul'
- });
- });
- </script>
- </ul>
- </div>
- <!-- /* STYLES */ -->
- <style type="text/css">
- #meta_inner ul { margin-left:0px; list-style: none; }
- #meta_inner li { padding: 2px 0px 2px 5px; margin: 4px 0px; border: 1px solid #DDDDDD; cursor: move; -moz-border-radius:6px; clear: both; height: 100px; position: relative;}
- #meta_inner li span.description { float: left; padding: 5px 0 0 0; width: 60px;}
- #meta_inner input {margin-right: 5px;}
- #meta_inner input.upload, #meta_inner input.title, #meta_inner input.year, #meta_inner input.materials {width: 400px;}
- #meta_inner { list-style-type: none; margin: 10px 0 0 0; padding: 0; width: 100%; }
- #meta_inner li img {position: absolute; top: 5px; right: 5px;}
- #meta_inner li.placeholder{border: dashed 2px #ccc;background-color:#FFF;height:20px;}
- #meta_inner #remove {float: right;}
- </style>
- <?php
- }
- /* When the post is saved, saves our custom data */
- function save_text_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;
- // 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'])){
- if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
- return;
- }else{return;}
- // OK, we're authenticated: we need to find and save the data
- $images = $_POST['images'];
- update_post_meta($post_id,'images',$images);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement