简体   繁体   English

wordpress如何在数组中添加wp_editor

[英]wordpress how to add wp_editor in an array

I had a small problem in my wordpress code I need to show a wordpress wp_editor in my page where it has array of values.the values are defined like the following 我的wordpress代码中有一个小问题我需要在我的页面中显示wordpress wp_editor,其中包含值数组。值定义如下

    $fields[] = array(
        'name' => __('Class', 'my-theme'),
        'desc' => __('', 'my-theme'),
        'id' => 'class'.$n,
        'std' => ( ( isset($class_text[$n]['class']) ) ? $class_text[$n]['class'] : '' ),
        'type' => 'text');

When I define my wp_editor like the above array it doesn't display where I want. 当我像上面的数组一样定义我的wp_editor时,它不会显示我想要的位置。 Instead all the editors displayed at the top before any content in all pages. 相反,所有编辑器都显示在所有页面中的任何内容之前的顶部。

I tried like the following set of array for the editor: 我为编辑器尝试了以下一组数组:

    $fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => wp_editor( '', 'sectioncontent'.$n ));

Attached the image of my problem: 附上我的问题的形象:

在此输入图像描述

Cause 原因

By default wp_editor prints the textarea that's why you cannot assign it to any variable or array. 默认情况下, wp_editor会打印textarea,这就是您无法将其分配给任何变量或数组的原因。

Solution

you can use output buffering of php to get printed data in variable like this: 你可以使用php的输出缓冲来获取变量中的打印数据,如下所示:

ob_start(); // Start output buffer

// Print the editor
wp_editor( '', 'sectioncontent'.$n );

// Store the printed data in $editor variable
$editor = ob_get_clean();

// And then you can assign that wp_editor to your array.

$fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => $editor); // <-- HERE

Looks to me like you're using Redux Framework to set up your theme/plugin option page - If you are looking to add the default Wordpress WYSIWYG (What You See Is What You Get - the same editor from the edit post page in the backend) editor in there you'll need to use the type: 'editor'. 在我看来,你正在使用Redux Framework来设置你的主题/插件选项页面 - 如果你想要添加默认的Wordpress WYSIWYG(所见即所得 - 来自后端编辑帖子页面的相同编辑器)编辑器在那里你需要使用类型:'编辑器'。

It can be confusing - the wp_editor() function you are using is the right place to start if you were setting up this options page from scratch, but you'd need to do quite a bit of work to have it display where and how you want it. 这可能会令人困惑 - 如果你从头开始设置这个选项页面,你正在使用的wp_editor()函数是正确的起点,但你需要做很多工作才能让它显示你在哪里以及如何显示想要它。 Redux et al make this quite a bit easier for you by generating the editor for you, so instead of you using the wp_editor function at all you just tell Redux that you want an editor field called 'My Content' to be one of the fields on the page. Redux等通过为您生成编辑器使您更容易实现这一点,因此您不必使用wp_editor函数,而只是告诉Redux您希望名为“我的内容”的编辑器字段是其中一个字段。这页纸。

The documentation for the editor field is here: https://docs.reduxframework.com/core/fields/editor/ 编辑器字段的文档位于: https//docs.reduxframework.com/core/fields/editor/

If I am correct that you are using redux, the correct code to replace what you have is: 如果我说你使用redux是正确的,那么替换你所拥有的代码的正确代码是:

 $fields[] = array(
        'name' => __('My Content', 'my-theme'),
        'id' => 'sectioncontent'.$n,
        'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
        'type' => 'editor');

To explain the other parts of this field array: 要解释此字段数组的其他部分:

  • 'Name' will be what shows up in the label for this field. “名称”将显示在此字段的标签中。 In this case you are using the localization functions in wordpress ( __() ) to get a phrase from the local dictionary in the 'my-theme' domain. 在这种情况下,您使用wordpress( __() )中的本地化函数从“my-theme”域中的本地字典中获取短语。
  • 'id' is what you will use to retrieve what has been entered into this field. 'id'是您将用于检索已输入此字段的内容的内容。 It will also affect the ID attribute assigned to the HTML elements in the options page. 它还会影响分配给选项页面中HTML元素的ID属性。
  • 'std' is the default for the field, which will be the value of the field when the options page is first displayed, before the user has set any options 'std'是字段的默认值,在用户设置任何选项之前,它将是首次显示选项页面时字段的值

On the editor documentation page linked above, you'll see the details of various other options you can define, like whether to show Media Upload buttons, and whether to run the input through wpautop to replace line breaks in the editor with <p> tags (by default both of these are true). 在上面链接的编辑器文档页面上,您将看到可以定义的各种其他选项的详细信息,例如是否显示“媒体上载”按钮,以及是否通过wpautop运行输入以使用<p>标签替换编辑器中的换行符(默认情况下,这两个都是真的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM