1

I have this following code to give a textarea to a front end user to use it as notes on a page to write things to remember. But this code saves only one sticky note that can be edited whenever the user wants. My questions is if there is a way to save the content somewhere and have a new one every time submit is pressed. or a add new sticky button.

       require_once( plugin_dir_path( __FILE__ ) . 'wp-ajax.php' );
        class notepad_stikey extends WP_Ajax {
            var $user;
            var $username;
            function __construct() {
                parent::__construct();
                add_action('init', array( &$this, 'setup') );
            }
            function setup() {
                $this->user = get_current_user_id();
                $this->username = get_userdata( $this->user )->user_login;
                $this->notes = get_user_meta( $this->user, 'notepad_stikey', true );
                $this->ph = ' '; //placeholder
                if (empty( $this->notes )) {
                    $this->notes = $this->ph;
                }
                add_action('wp_enqueue_scripts', array( &$this, 'scripts') );
            }
            function scripts() {
                wp_enqueue_script( 'notepad_stikey', plugins_url( 'notepad_stikey.js', __FILE__ ), array( 'jquery' ) );
                wp_localize_script( 'notepad_stikey', 'notepad_stikey', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
            }
            function an_change_notepad_stikey() {
                $notes = trim( $_POST['notes'] );
                //if notes is empty, delete
                if ( empty($notes) ) {
                    if ( delete_user_meta( $this->user, 'notepad_stikey' ) )
                        die( 'notes deleted' );
                }
                //notes are the same, notes = placeholder, or resaving empty notes
                if ( $notes == $this->notes || $notes == $this->ph || ( empty($notes) && $this->notes == $this->ph) )
                    die();
                //update
                if ( update_user_meta( $this->user, 'notepad_stikey', $notes ) )
                    die( 'updated' );
                //hopefully, we don't get this far. if we do, something is wrong
                die( 'uh oh. notes could not be saved' );
            }
        }
        global $notepad_stikey;
        $notepad_stikey = new notepad_stikey();
        add_action( 'widgets_init', 'notepad_stikey_load' );
        function notepad_stikey_load() {
            register_widget( 'notepad_stikey_Widget' );
        }
        class notepad_stikey_Widget extends WP_Widget {
            function notepad_stikey_Widget() {
                $widget_ops = array('classname' => 'notepad_stikey', 'description' => __( 'notepad_stikey. Only one instance please. Else this will break.', 'notepad_stikey' ) );
                $control_ops = array( 'id_base' => 'notepad_stikey' );
                parent::WP_Widget( 'notepad_stikey', __( 'notepad_stikey', 'notepad_stikey' ), $widget_ops, $control_ops );
            }
            function widget( $args, $instance ) {
                extract( $args, EXTR_SKIP );
                echo $before_widget;
                global $notepad_stikey;
                $username = $notepad_stikey->username;
                $notes = $notepad_stikey->notes;
                //overwrite title
                $instance['title'] = 'Notepad for '. $username;
                echo $instance['hide_title'] ? '' : $before_title . $instance['title'] . $after_title;
                echo "<div id='notepad_stikey' class='$username' style='border: 1px solid #eee; padding: 10px 15px;min-height: 100px;'>";
                echo $notes;
                echo '</div><span style="float:left;color:#008;" id="notepad_stikey_response"></span><small style="float:right;">click box above to edit</small>';
                echo $after_widget;
            } //end widget()
            function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance['title'] = esc_attr( $new_instance['title'] );
                $instance['hide_title'] = (bool) $new_instance['hide_title'] ? 1 : 0;
                return $instance;
            } //end update()
            function form( $instance ) {
                $instance = wp_parse_args( (array) $instance, array( 'hide_title' => 0 ) );
                extract( $instance );
                ?>
                <p>
                    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hide_title'); ?>" name="<?php echo $this->get_field_name('hide_title'); ?>"<?php checked( $hide_title ); ?> />
                    <label for="<?php echo $this->get_field_id('hide_title'); ?>"><?php _e('Hide Title?', 'notepad_stikey' );?></label>
                </p>
                <?php
            } //end form()
        }



       if (!class_exists('WP_Ajax')) {
   class WP_Ajax {
function __construct( $ajax_prefix = 'a', $nopriv_prefix = 'n' ) {
    $regex = "/^($ajax_prefix)?($nopriv_prefix)?_|^($nopriv_prefix)?  ($ajax_prefix)?_/";
    $methods = get_class_methods( $this );
    foreach ( $methods as $method ) {
        if ( preg_match( $regex, $method, $matches ) ) {
            if ( count( $matches ) > 1 ) {
                $action = preg_replace( $regex, '', $method );
                if ( count( $matches ) == 3 ) {
                    add_action( "wp_ajax_$action", array( $this, $method )    );
                    add_action( "wp_ajax_nopriv_$action", array( $this, $method ) );
                } else {
                    if ( $matches[1] == $ajax_prefix ) {
                        add_action( "wp_ajax_$action", array( $this, $method ) );
                    } else {
                        add_action( "wp_ajax_nopriv_$action", array( $this,    $method ) );
                    }
                }
            }
        }
    }
}
   }

   }



     jQuery(document).ready(function($) {
     $('#notepad_stikey').click(function(e) {
       tag = e.target.tagName;
        if ( tag != 'TEXTAREA' && tag != 'INPUT' ) {
        contents = $(this).html();
        $(this).html( '<textarea rows="5" cols="50" style="display:block;width:98%;height:100px;">' + contents + '</textarea><input type="submit" class="save" style="position:relative;z-index:99" />' );
    }
   });
  $('#notepad_stikey input.save').live( 'click', function() {
    new_contents = $(this).siblings('textarea').val();
    $('#notepad_stikey').html( new_contents );
    change_notepad_stikey( new_contents );
    return false;
   });
    function change_notepad_stikey( notes ) {
    $('#notepad_stikey_response').text( '...' );
    $.post(notepad_stikey.ajaxurl,
        {
            'action' : 'change_notepad_stikey',
            'notes' : notes
        }, function(response) {
            //if (response != '') {
                //alert( response );
                $('#notepad_stikey_response').text( response );
            //}
        }, 'text' );
    }
    });
1

You need to create separate tables for store sticky notes and save user sticky notes to that tables , in this way you can manage multiple sticky notes of user. i think this idea will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.