1. Code
  2. PHP

Consejo rápido: uso de wp_editor

¡En este consejo descubriremos para qué sirve wp_editor!
Scroll to top
2 min read

Spanish (Español) translation by Santiago Diaz (you can also view the original English article)

¡En este consejo descubriremos para qué sirve wp_editor!


¿Qué es wp_editor?

Text editor / notepad icon

Es una función de WordPress que crea un editor visual (WYSIWYG) como el que aparece en el administrador de WordPress al crear publicaciones o páginas. Esta pequeña y práctica función ha estado disponible desde WordPress v3.3. Hay una página detallada del Codex sobre wp_editor, si necesitas más información. WordPress utiliza una versión personalizada del editor TinyMCE, que puedes encontrar aquí. Para ver los archivos, consulta wp-includes/js/tinymce en el directorio de instalación de WordPress.

The editor window in WordPress admin interfaceThe editor window in WordPress admin interfaceThe editor window in WordPress admin interfaceEl editor habitual en el administrador.

¿Por qué necesitamos esto?

¡Porque también podemos usar esta función en temas y complementos! El contenido enriquecido es útil en varias ocasiones, no solo en las publicaciones. Podemos usar varios editores en una sola subpágina, solo usa el contenido y las variables de ID de manera apropiada.


Ejemplos

Code icon

Esta parte asume que conoces al menos algo de programación PHP básica. Las variables $content y $editor_id son obligatorias, deben estar configuradas en todo momento. La variable $settings es una matriz en la que se pueden activar / desactivar las funciones del editor único.

Por favor ten en cuenta que la mayoría de las explicaciones están en los comentarios, ¡léelas también!

Los siguientes códigos (1, 2, 3 y 4) muestran cómo utilizar la función.

1
/**

2
 * Mandatory variables

3
 */
4
wp_editor( $content, $editor_id );
5
6
/**

7
 * Basic syntax

8
 */
9
wp_editor( $content, $editor_id, $settings = array() );
10
11
/**

12
 * 1.

13
 * The first variable will set the content to show in the box,

14
 * the second one holds the HTML id attribute of the editor

15
 * (must be lowercase letters and no underscores or hyphens).

16
 */
17
wp_editor( 'Hello World! This is our first test! Enjoy!', 'ourmaineditor' );
18
19
/**

20
 * 2.

21
 * This code renders an editor box and a submit button.

22
 * The box will have 15 rows, the quicktags won't load

23
 * and the PressThis configuration is used.

24
 */
25
$args = array(
26
	'textarea_rows' => 15,
27
	'teeny' => true,
28
	'quicktags' => false
29
);
30
31
wp_editor( 'This is the default text!', 'editor', $args );
32
submit_button( 'Save content' );
33
34
/**

35
 * 3.

36
 * We can recreate the post editor with the get_post function,

37
 * which retrieves an existing post (in this case number 117)

38
 * from the database.

39
 */
40
$post = get_post( 117, 'OBJECT' );
41
wp_editor( $post, 'editor' );
42
43
/**

44
 * 4.

45
 * Custom buttons for the editor.

46
 * This is a list separated with a comma after each feature

47
 * eg. link, unlink, bold, ...

48
 */
49
$settings = array(
50
	'textarea_name' => 'content',
51
	'media_buttons' => false,
52
	'tinymce' => array(
53
		'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
54
			'bullist,blockquote,|,justifyleft,justifycenter' .
55
			',justifyright,justifyfull,|,link,unlink,|' .
56
			',spellchecker,wp_fullscreen,wp_adv'
57
	)
58
);
59
wp_editor( '', 'content', $settings );

Personalización del editor

Cog icon

Podemos personalizar las funciones del editor con la ayuda de esta descripción en el Codex. Para profundizar más, también puedes consultar class-wp-editor.php en wp-includes en tu instalación de WordPress.