0

I have just started learning Wordpress so please go easy on me here. I have managed to setup a custom metabox with some text fields. If I enter some sample text in these input boxes, the code gets formatted differently in the custom fields.

enter image description here

This is the code I am using to implement custom fields:

/**
 * function to return a custom field value.
 */
function alep_get_custom_field( $value ) {
    global $post;

    $custom_field = get_post_meta( $post->ID, $value, true );
    if ( !empty( $custom_field ) )
        return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );

    return false;
}

/**
 * Register the Meta box
 */
function alep_add_custom_meta_box() {
    add_meta_box( 'alep-meta-box', __( 'Metabox Example', 'alep' ), 'alep_meta_box_output', 'post', 'normal', 'high' );
    add_meta_box( 'alep-meta-box', __( 'Metabox Example', 'alep' ), 'alep_meta_box_output', 'page', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'alep_add_custom_meta_box' );


/**
 * Output the Meta box
 */
function alep_meta_box_output( $post ) {
    // create a nonce field
    wp_nonce_field( 'my_alep_meta_box_nonce', 'alep_meta_box_nonce' ); ?>

    <p>
        <label for="alep_textfield1"><?php _e( 'Textfield 1', 'alep' ); ?>:</label>
        <input type="text" name="alep_textfield1" id="alep_textfield1" value="<?php echo alep_get_custom_field( 'alep_textfield1' ); ?>" size="70" />
    </p>

    <p>
        <label for="alep_textfield2"><?php _e( 'Textfield 2', 'alep' ); ?>:</label>
        <input type="text" name="alep_textfield2" id="alep_textfield2" value="<?php echo alep_get_custom_field( 'alep_textfield2' ); ?>" size="70" />
    </p>

    <p>
        <label for="alep_textfield3"><?php _e( 'Textfield 3', 'alep' ); ?>:</label>
        <input type="text" name="alep_textfield3" id="alep_textfield3" value="<?php echo alep_get_custom_field( 'alep_textfield3' ); ?>" size="70" />
    </p>

    <p>
        <label for="alep_textfield4"><?php _e( 'Textfield 4', 'alep' ); ?>:</label>
        <input type="text" name="alep_textfield4" id="alep_textfield4" value="<?php echo alep_get_custom_field( 'alep_textfield4' ); ?>" size="70" />
    </p>

    <p>
        <label for="alep_textfield5"><?php _e( 'Textfield 5', 'alep' ); ?>:</label>
        <input type="text" name="alep_textfield5" id="alep_textfield5" value="<?php echo alep_get_custom_field( 'alep_textfield5' ); ?>" size="70" />
    </p>

    <?php
}


/**
 * Save the Meta box values
 */
function alep_meta_box_save( $post_id ) {
    // Stop the script when doing autosave
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // Verify the nonce. If insn't there, stop the script
    if( !isset( $_POST['alep_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['alep_meta_box_nonce'], 'my_alep_meta_box_nonce' ) ) return;

    // Stop the script if the user does not have edit permissions
    if( !current_user_can( 'edit_post', get_the_id() ) ) return;

    // Save the textfield
    if( isset( $_POST['alep_textfield1'] ) )
        update_post_meta( $post_id, 'alep_textfield1', esc_attr( $_POST['alep_textfield1'] ) );

    // Save the textfield
    if( isset( $_POST['alep_textfield2'] ) )
        update_post_meta( $post_id, 'alep_textfield2', esc_attr( $_POST['alep_textfield2'] ) );

    // Save the textfield
    if( isset( $_POST['alep_textfield3'] ) )
        update_post_meta( $post_id, 'alep_textfield3', esc_attr( $_POST['alep_textfield3'] ) );

    // Save the textfield
    if( isset( $_POST['alep_textfield4'] ) )
        update_post_meta( $post_id, 'alep_textfield4', esc_attr( $_POST['alep_textfield4'] ) );

    // Save the textfield
    if( isset( $_POST['alep_textfield5'] ) )
        update_post_meta( $post_id, 'alep_textfield5', esc_attr( $_POST['alep_textfield5'] ) );
}
add_action( 'save_post', 'alep_meta_box_save' );

How can I get the custom field values to display exactly the same as what is entered in the meta box input boxes?

The reason I need this is because I am trying to display shortcodes in my sidebar (outside the main post content).


Edit: removing esc_attrworks visually, but then I have no code validation...

1 Answer 1

0

The whole point of esc_attr is to encode special characters to avoid confusion later on. So if you have double quotes, yes they will be changed into &quot;. If you don't want that, don't use esc_attr.

If you insist on using esc_attr but don't like what it does, you'll have to undo it after you have used it. That would go like this:

$input = esc_attr ($_POST['alep_textfield1']);
$input = str_replace ('&quot;', '"', $input);
update_post_meta ($post_id, 'alep_textfield1', $input);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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