DEV Community

angela300
angela300

Posted on

A wordpress plugin to add daily tasks to your website

With Chatgpt

On chatgpt, type this request:

Create the code for a wordpress plugin that adds a 'Daily task' window to the wordpress dashboard. The Daily Task window should be writable and it should be able to check all done tasks when a user is finished with a specific task

Create a new directory for your plugin. Let's name it daily-task-widget.

Inside the daily-task-widget directory, create a main plugin file named daily-task-widget.php with the PHP code that is generated:

<?php
/*
Plugin Name: Daily Task Widget
Description: Adds a daily task widget to the WordPress dashboard.
Version: 1.1
Author: Your Name
*/

// Enqueue jQuery
function enqueue_jquery() {
    wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'enqueue_jquery');

// Add the widget to the dashboard
function daily_task_widget() {
    wp_add_dashboard_widget(
        'daily_task_widget',
        'Daily Task',
        'display_daily_task_widget'
    );
}

// Display the widget content
function display_daily_task_widget() {
    $tasks = get_user_meta(get_current_user_id(), 'daily_tasks',
 true);
    ?>
    <textarea id="daily_tasks" name="daily_tasks" 
    style="width: 100%;" rows="5"><?php echo esc_textarea($tasks); ?>
</textarea>
    <br>
    <label for="task_done">
    Mark task as done:
    <input type="checkbox" id="task_done" name="task_done"></label>
    <button id="save_tasks">Save Tasks</button>

    <script>
        jQuery(document).ready(function($) {
            $('#save_tasks').on('click', function() {
                var tasks = $('#daily_tasks').val();
                var taskDone = 
                $('#task_done').is(':checked') ? 1 : 0;

                $.post(ajaxurl, {
                    action: 'save_daily_tasks',
                    tasks: tasks,
                    task_done: taskDone,
                    security: 
           '<?php echo wp_create_nonce("save_daily_tasks_nonce"); ?>'
                }, function(response) {
                    alert('Tasks saved successfully!');
                });
            });
        });
    </script>
    <?php
}

// Save the tasks when the user updates the dashboard
function save_daily_tasks() {
    check_ajax_referer('save_daily_tasks_nonce', 'security');

    if (isset($_POST['tasks'])) {
        $tasks = sanitize_textarea_field($_POST['tasks']);
        update_user_meta(get_current_user_id(), 
        'daily_tasks', $tasks);
    }

    if (isset($_POST['task_done'])) {
        // Handle marking task as done
        // You can implement your logic here
    }

    wp_die();
}

// Hook functions to WordPress actions
add_action('wp_dashboard_setup', 'daily_task_widget');
add_action('wp_ajax_save_daily_tasks', 'save_daily_tasks');
?>
Enter fullscreen mode Exit fullscreen mode

This simple plugin adds a textarea for daily tasks and a checkbox to mark a task as done. The tasks are saved in the user's meta data using the update_user_meta function.

Zip the daily-task-widget directory.

Upload the zipped file through the WordPress admin interface or manually upload it to the wp-content/plugins directory on your server.

Activate the plugin through the WordPress admin.

Now, when users log in, they will see a "Daily Task" widget on their dashboard where they can write and save their daily tasks. Note that this is a basic example, and you may want to enhance it based on your specific requirements.

Thanks for reading through! To support my blog you can make a donation through https://www.buymeacoffee.com/angela300

Top comments (0)