Skip to content Skip to sidebar Skip to footer

Save User Generated Options In Wp-plugin

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 w

Solution 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);
}

Post a Comment for "Save User Generated Options In Wp-plugin"