plugins – Saving wp.media URL to database

Question

I am having trouble saving the selected wp.media attachment URL to the database.

The value of the hidden input seems to get the value, but the value is not being stored in the database.

<?php

global $wpdb;
$table_name = $wpdb->prefix . 'wpclc_logos';

// Add variables
$message="";
$notice="";

// Set defaults
$default = array(
    'id' => 0,
    'status' => '',
    'img' => '',
    'label' => null,
    'start' => null,
    'end' => null
);

// Verify nonce
if( wp_verify_nonce( $_REQUEST['nonce'], basename(__FILE__) ) ) {

    // Combine default item with request params
    $item = shortcode_atts( $default, $_REQUEST );

    // validate data, and if all ok save item to database
    // if id is zero insert otherwise update
    $item_valid = wpclc_validate_items($item);

    // Validate form
    if( $item_valid === true ) {

        if( $item['id'] == 0 ) {

            $result = $wpdb->insert( $table_name, $item );
            $item['id'] = $wpdb->insert_id;

            if( $result ) {

                $message = __('Logo was successfully added', 'wpclc_text_domain');

            } else {

                $notice = __('There was an error while saving logo', 'wpclc_text_domain');

            }

        } else {

            $result = $wpdb->update( $table_name, $item, array( 'id' => $item['id'] ) );

            if( $result ) {

                $message = __('Logo was successfully updated', 'wpclc_text_domain');

            } else {

                $notice = __('There was an error while updating logo', 'wpclc_text_domain');

            }

        }

    } else {

        $notice = $item_valid;

    }

} else {

    $item = $default;

    if( isset( $_REQUEST['id'] ) ) {

        $item = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $_REQUEST['id']), ARRAY_A);

        if( ! $item ) {

            $item = $default;
            $notice = __('Logo not found', 'wpclc_text_domain');

        }

    }

}

/**
 * Error alert messages for validation
 */
function wpclc_validate_items($item)
{
    $messages = array();

    //if( empty( $item['img_url'] ) ) $messages[] = __('Image not selected', 'wpclc_text_domain');
    if( empty( $item['label'] ) ) $messages[] = __('Label is not set', 'wpclc_text_domain');
    if( empty( $item['status'] ) ) $messages[] = __('Status is not set', 'wpclc_text_domain');
    if( empty( $item['start'] ) ) $messages[] = __('Start date is not set', 'wpclc_text_domain');
    if( empty( $item['end'] ) ) $messages[] = __('End date is not set', 'wpclc_text_domain');

    if( empty($messages) ) {

        return true;
    
    }

    return implode( '<br />', $messages );
}

?>

<div class="wrap">

    <div class="icon32 icon32-posts-post" id="icon-edit"><br></div>

    <h1>
    
        <?php _e('Add new logo', 'wpclc_text_domain')?>

        <a class="add-new-h2" href="<?php echo get_admin_url(get_current_blog_id(), 'admin.php?page=wpclc_settings');?>">
            <?php _e('Back', 'wpclc_text_domain')?>
        </a>

    </h1>

    <?php if (!empty($notice)): ?>

        <div id="notice" class="error">
            <p>
                <?php echo $notice ?>
            </p>
        </div>

    <?php endif;?>

    <?php if (!empty($message)): ?>

        <div id="message" class="updated">
            <p>
                <?php echo $message ?>
            </p>
        </div>

    <?php endif;?>

    <form id="wpclc_form_add" method="POST">

        <input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>

        <input type="hidden" name="id" value="<?php echo $item['id'] ?>"/>

        <table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table">

            <tbody>

                <tr class="form-field">
                    <th valign="top" scope="row">
                        <label for="img"><?php _e('Image', 'wpclc_text_domain')?></label>
                    </th>
                    <td>
                        <input id="img_url" type="hidden" name="img_url" value="<?php echo esc_attr($item['img_url'])?>">
                        <button id="img" name="img" type="button" class="button">Choose or upload image</button>
                        <span id="img_text" style="margin-left:1rem; line-height:30px;"></span>
                    </td>
                </tr>
                <tr class="form-field">
                    <th valign="top" scope="row">
                        <label for="label"><?php _e('Label', 'wpclc_text_domain')?></label>
                    </th>
                    <td>
                        <input id="label" name="label" type="text" style="width: 95%" value="<?php echo esc_attr($item['label'])?>"
                            size="50" class="" placeholder="<?php _e('Christmas', 'wpclc_text_domain')?>" required>
                    </td>
                </tr>
                <tr class="form-field">
                    <th valign="top" scope="row">
                        <label for="status"><?php _e('Status', 'wpclc_text_domain')?></label>
                    </th>
                    <td>
                        <select id="status" name="status" style="width: 95%" value="" class="" required>
                            <option value="active">Active</option>
                            <option value="inactive">Inactive</option>
                        </select>
                    </td>
                </tr>
                <tr class="form-field">
                    <th valign="top" scope="row">
                        <label for="start"><?php _e('Start Date', 'wpclc_text_domain')?></label>
                    </th>
                    <td>
                        <input id="start" name="start" type="date" style="width: 95%" value="<?php echo esc_attr( date( "d/m/Y", strtotime( $item['start'] ) ) ); ?>" class="" required>
                    </td>
                </tr>
                <tr class="form-field">
                    <th valign="top" scope="row">
                        <label for="end"><?php _e('End Date', 'wpclc_text_domain')?></label>
                    </th>
                    <td>
                        <input id="end" name="end" type="date" style="width: 95%" value="<?php echo esc_attr( date( "d/m/Y", strtotime( $item['end'] ) ) ); ?>" class="" required>
                    </td>
                </tr>

            </tbody>

        </table>

        <input type="submit" value="<?php _e('Save', 'wpclc_text_domain')?>" id="submit" class="button-primary" name="submit">
        
    </form>

</div>

The images below show the results I recieve when I add a new entry to the database. As you will see, the img section contains no content at all. It is setup in the database as a VARCHAR(255).

enter image description here
enter image description here

Please ignore the errors in the console, they simply relate to the date input not being of YYYY-mm-dd format.

0
jesse 1 month 2021-04-03T05:20:54-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse