Save user generated options in wp-plugin

2019-09-17 20:16发布

问题:

I want to create a slogan plugin for wordpress with an easy user interface. it shall randomly display a user-defined slogan along with a name who said it. In the plugin options I want to enable a user to dynamically add or delete those slogans. Here's a quick mock-up of what I want in the end so you can understand it better:

There's the "Add Slogan" button which adds 2 new input fields along with the delete this slogan button. So if there was only Name1 and you press the button, those fields for Name2 would pop up. The JS part is no problem for me but I have problems saving those values. How can I add new values into options and save all dynamically generated options? And even more difficult: If I'd click Delete this slogan for Name2 and click save, it should remove this line from the options as well and update the Name1 and Name 3 fields with the current values.

How can this be done, I'm a bit clueless right now and looking into the Plugin Codex didn't really help

回答1:

You mean something like this ?

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',
        __( 'My Slogans', 'myplugin_textdomain' ),
        'dynamic_inner_custom_box',
        'post');
}

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

    //GEt the array of saved meta
    $slogans = get_post_meta($post->ID,'slogans',true);

    $c = 0;
    //if ( count( $slogans ) > 0 ) {
    if( is_array ( $slogans ) ){
        foreach( $slogans as $slogan ) {
            if ( isset( $slogan['name'] ) || isset( $slogan['slogan'] ) ) {
                printf( '<p>Slogan Name <input type="text" name="slogans[%1$s][name]" value="%2$s" /> -- Slogan Content : <input type="text" name="slogans[%1$s][slogan]" value="%3$s" /><input class="button tagadd remove" type="button" value="%4$s"></p>', $c, $slogan['name'], $slogan['slogan'], __( 'Remove Slogan' ) );
                $c = $c +1;
            }
        }
    }

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

            $('#here').append('<p> Slogan Name <input type="text" name="slogans['+count+'][name]" value="" /> -- Slogan Content : <input type="text" name="slogans['+count+'][slogan]" value="" /><input class="button tagadd remove" type="button" value="<?php _e('Remove Slogan'); ?>">' );
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}

/*  saves our custom data when the post is saved */
function dynamic_save_postdata( $post_id ) {
    // verify if this is an auto save routine. 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // verify Nonce and that the request is valid,
    if ( !isset( $_POST['dynamicMeta_noncename'] ) )
        return;

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

    // GOOD; we are set, find a save data

    $slogans = $_POST['slogans'];

    update_post_meta($post_id,'slogans',$slogans);
}