Created
October 5, 2018 17:07
-
-
Save lamberger/621c7129300a90edbc78cd8276764339 to your computer and use it in GitHub Desktop.
Repeat content in editor Post Page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Add facillity | |
| Plugin URI: https://github.com/ | |
| Description: Test | |
| Version: 0.0.1 | |
| Author: PL | |
| Author URI: https://github.com/ | |
| */ | |
| add_action( 'add_meta_boxes', 'dynamic_add_facillity_box' ); | |
| /* Do something with the data entered */ | |
| add_action( 'save_post', 'dynamic_save_facillity_postdata' ); | |
| /* Adds a box to the main column on the Post and Page edit screens */ | |
| function dynamic_add_facillity_box() { | |
| add_meta_box( | |
| 'dynamic_sectionfacillity', | |
| __( 'Lägg till utrustning', 'facillity_textdomain' ), | |
| 'dynamic_inner_facillity_box', | |
| 'post'); | |
| } | |
| /* Prints the content */ | |
| function dynamic_inner_facillity_box() { | |
| global $post; | |
| // Use nonce for verification | |
| wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicFacillity_noncename' ); | |
| ?> | |
| <div id="meta_inner"> | |
| <?php | |
| //get the saved meta as an arry | |
| $facillitys = get_post_meta($post->ID,'facillitys',true); | |
| $f = 0; | |
| if (is_array($facillitys) || is_object($facillitys)) | |
| { | |
| foreach( $facillitys as $facillitydescription ) { | |
| if ( isset( $facillitydescription['facillitytype'] ) || isset( $facillitydescription['facillitydescription'] ) ) { | |
| printf( '<p><em>Typ av produkt :</em> <input type="text" class="widefat" name="facillitys[%1$s][facillitytype]" value="%2$s"/><em class="small-em">Utbytesdatum :</em> <input type="text" class="widefat" name="facillitys[%1$s][facillitydescription]" value="%3$s"/><span class="remove">%4$s</span></p>', $f, $facillitydescription['facillitytype'], $facillitydescription['facillitydescription'], __( '<a href="#" class="">Ta bort</a><hr />' ) ); | |
| $f = $f +1; | |
| } | |
| } | |
| } | |
| ?> | |
| <span id="here2"></span> | |
| <span class="add2"><a href="#" class="btn-pjl-add"><?php _e('Skapa en ny produkt'); ?></a></span> | |
| <script> | |
| var $ =jQuery.noConflict(); | |
| $(document).ready(function() { | |
| var count = <?php echo $f; ?>; | |
| $(".add2").click(function() { | |
| count = count + 1; | |
| $('#here2').append('<p><em>Typ av produkt:</em><br /><input placeholder="Ex. spis, armatur..." class="widefat" type="text" name="facillitys['+count+'][facillitytype]" value="" /><br /><em>Utbytt :</em><input class="widefat" type="date" name="facillitys['+count+'][facillitydescription]" value"" /><span class="remove"><a href="#">Ta bort utrymme</a></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_facillity_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['dynamicFacillity_noncename'] ) ) | |
| return; | |
| if ( !wp_verify_nonce( $_POST['dynamicFacillity_noncename'], plugin_basename( __FILE__ ) ) ) | |
| return; | |
| // Authenticated: we need to find and save the data | |
| $facillitys = $_POST['facillitys']; | |
| update_post_meta($post_id,'facillitys',$facillitys); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment