comments – Cómo reorganizar los campos en comment_form ()

Pregunta:

Estoy usando un filtro personalizado para cambiar los campos, pero no puedo entender cómo cambiar el orden de los campos en el formulario de comentarios.

Orden deseada:

  • campo de comentario (primero / arriba)
  • nombre
  • Email
  • sitio web

Este es el código que estoy usando actualmente:

function alter_comment_form_fields($fields){
    $fields['comments'] = 'Test';
    $fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Your name, please' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
                    '<input id="author" name="author" type="text" placeholder="John Smith" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>';
    $fields['email'] = 'next';  //removes email field
    //$fields['url'] = '';  //removes website field

    return $fields;
}

add_filter('comment_form_default_fields','alter_comment_form_fields');

Respuesta:

Eso es bastante simple. Solo tiene que quitar el área de textarea de los campos predeterminados – filtro 'comment_form_defaults' – e imprimirlo en la acción 'comment_form_top' :

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Comment Textarea On Top
 * Description: Makes the textarea the first field of the comment form.
 * Version:     2012.04.30
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

// We use just one function for both jobs.
add_filter( 'comment_form_defaults', 't5_move_textarea' );
add_action( 'comment_form_top', 't5_move_textarea' );

/**
 * Take the textarea code out of the default fields and print it on top.
 *
 * @param  array $input Default fields if called as filter
 * @return string|void
 */
function t5_move_textarea( $input = array () )
{
    static $textarea = '';

    if ( 'comment_form_defaults' === current_filter() )
    {
        // Copy the field to our internal variable …
        $textarea = $input['comment_field'];
        // … and remove it from the defaults array.
        $input['comment_field'] = '';
        return $input;
    }

    print apply_filters( 'comment_form_field_comment', $textarea );
}

Leave a Comment

Your email address will not be published.

Scroll to Top

istanbul avukat

-

web tasarım