@alexiusgrey

Как правильно сохранять и редактировать значение поля с TinyMCE?

Не хочется ставить acf или плагин таксономий ради одного поля.

Я уже не один раз делал поля для таксономий руками, но не поле TinyMCE. Вот способ для текста и для textarea - работает благополучно.
add_action('product_cat_add_form_fields', 'wh_taxonomy_add_new_meta_field', 10, 1);
add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);
//Product Cat Create page
function wh_taxonomy_add_new_meta_field() {
    ?>   
    <div class="form-field">
        <label for="wh_meta_desc">Нижнее описание</label>
        <textarea name="wh_meta_desc" id="wh_meta_desc" cols="35" rows="5"></textarea>
        <p class="description">Нижнее описание в категории товара</p>
    </div>
    <?php
}

//Product Cat Edit page
function wh_taxonomy_edit_meta_field($term) {
/*TEXTAREA */
    //getting term ID
    $term_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. 
    $wh_meta_desc = get_term_meta($term_id, 'wh_meta_desc', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="wh_meta_desc">Нижнее описание</label></th>
        <td>
            <textarea name="wh_meta_desc" id="wh_meta_desc" cols="35" rows="5"><?php echo esc_attr($wh_meta_desc) ? esc_attr($wh_meta_desc) : ''; ?></textarea>
            <p class="description">Нижнее описание в категории товара</p>
        </td>
    </tr>
    <?php
}


add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);

// Save extra taxonomy fields callback function.
function wh_save_taxonomy_custom_meta($term_id) {
   
    $wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');   
    update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}
/*ТЕКСТ*/
add_action( 'product_cat_add_form_fields', 'cxc_taxonomy_add_new_meta_field', 10, 2 );

function cxc_taxonomy_add_new_meta_field() {
	// this will add the custom meta field to the add new term page
	?>
	<div class="form-field">
		<label for="custom_term_meta"><?php _e( 'Custom meta field', 'cxc-codexcoach' ); ?></label>
		<input type="text" name="custom_term_meta" id="custom_term_meta" value="">
		<p class="description"><?php _e( 'Enter a value for this field','cxc-codexcoach' ); ?></p>
	</div>
	<?php
}

add_action( 'product_cat_edit_form_fields', 'cxc_taxonomy_edit_meta_field', 10, 2 );

function cxc_taxonomy_edit_meta_field( $term ) {

	// put the term ID into a variable
	$term_id = $term->term_id;

	// retrieve the existing value(s) for this meta field. This returns an array
	$term_meta = get_term_meta( $term_id, 'custom_term_meta', true ); ?>
	<tr class="form-field">
		<th scope="row" valign="top"><label for="custom_term_meta"><?php _e( 'Example meta field', 'cxc-codexcoach' ); ?></label></th>
		<td>
			<input type="text" name="custom_term_meta" id="custom_term_meta" value="<?php echo ( !empty($term_meta) ) ? $term_meta  : ''; ?>">
			<p class="description"><?php _e( 'Enter a value for this field','cxc-codexcoach' ); ?></p>
		</td>
	</tr>

	<?php
}

add_action( 'edited_product_cat', 'cxc_save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_product_cat', 'cxc_save_taxonomy_custom_meta', 10, 2 );

function cxc_save_taxonomy_custom_meta( $term_id ) {
	if ( isset( $_POST['custom_term_meta'] ) && !empty( $_POST['custom_term_meta'] ) ) {
		update_term_meta( $term_id, 'custom_term_meta', $_POST['custom_term_meta'] );
	}
}


Но мне надо в этот раз поле с TinyMCE. Я делаю по аналогии, на первом этапе, где add, оно появляется. и красивое, но на этапе edit в нем нет значения, и оно не обновляется, не добавляется, не меняется. В базу сохраняется - потому что если в первом случае сделать TinyMCE, а для edit оставить текст - то оно вылезает, не вмешается, но все выводится и сохраняется. То есть я наверно не прописываю для TinyMCE что-то нужное.
Пробую так - add
add_action( 'product_cat_add_form_fields', 'cxc_taxonomy_add_new_meta_field', 10, 2 );

function cxc_taxonomy_add_new_meta_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="custom_term_meta"><?php _e( 'Custom meta field', 'cxc-codexcoach' ); ?></label>
        <?php
        wp_editor(get_post_meta( $post->ID, 'custom_term_meta', true ), 'custom_term_meta', array(
            'wpautop'       => 1,
            'media_buttons' => 1,
            'textarea_name' => 'custom_term_meta',
            'textarea_rows' => 5,
            'tabindex'      => null,
            'editor_css'    => '<style>.quicktags-toolbar, .wp-editor-tools, .wp-editor-wrap, .wp-switch-editor {padding: 5px 10px;}</style>',
            'editor_class'  => 'form-field',
            'teeny'         => 0,
            'dfw'           => 0,
            'tinymce'       => 1,
            'quicktags'     => 1,
            'drag_drop_upload' => false
        ) );?>
        <p class="description"><?php _e( 'Enter a value for this field','cxc-codexcoach' ); ?></p>
    </div>
    <?php
}


edit
add_action( 'product_cat_edit_form_fields', 'cxc_taxonomy_edit_meta_field', 10, 2 );

function cxc_taxonomy_edit_meta_field( $term ) {

    // put the term ID into a variable
    $term_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_term_meta( $term_id, 'custom_term_meta', true ); ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="custom_term_meta"><?php _e( 'Example meta field', 'cxc-codexcoach' ); ?></label></th>
        <td>
            <?php
        wp_editor(get_post_meta( $post->ID, 'custom_term_meta', true ), 'custom_term_meta', array(
            'wpautop'       => 1,
            'media_buttons' => 1,
            'textarea_name' => 'custom_term_meta',
            'textarea_rows' => 5,
            'tabindex'      => null,
            'editor_css'    => '<style>.quicktags-toolbar, .wp-editor-tools, .wp-editor-wrap, .wp-switch-editor {padding: 5px 10px;}</style>',
            'editor_class'  => 'form-field',
            'teeny'         => 0,
            'dfw'           => 0,
            'tinymce'       => 1,
            'quicktags'     => 1,           
            'drag_drop_upload' => false
        ) );?>
            <p class="description"><?php _e( 'Enter a value for this field','cxc-codexcoach' ); ?></p>
        </td>
    </tr>
    <?php
}

Чего я не учитываю?
  • Вопрос задан
  • 56 просмотров
Пригласить эксперта
Ответы на вопрос 1
@alexiusgrey Автор вопроса
Накопировал все подряд из разных мест, а $term_id = $term->term_id и $term->ID, которое получилось из $post->ID, это две большие разницы, сейчас сохраняется, оставляю шпаргалку другим, кому надо tinyMCE поле, ну и конечно для себя в будущем, я часто заглядываю в свои же вопросы-ответы
add_action( 'product_cat_add_form_fields', 'cxc_taxonomy_add_new_meta_field', 10, 2 );

function cxc_taxonomy_add_new_meta_field() {
	// this will add the custom meta field to the add new term page
	?>
	<div class="form-field">
		<label for="custom_term_meta"><?php _e( 'Custom meta field', 'cxc-codexcoach' ); ?></label>
		<!--<input type="text" name="custom_term_meta" id="custom_term_meta" value="">-->
		<?php 
	   $term_id = $term->term_id;	
		wp_editor(get_term_meta( $term_id, 'custom_term_meta', true ), 'custom_term_meta', array(
			'wpautop'       => 1,
			'media_buttons' => 1,
			'textarea_name' => 'custom_term_meta',
			'textarea_rows' => 5,
			'tabindex'      => null,
			'editor_css'    => '<style>.quicktags-toolbar, .wp-editor-tools, .wp-editor-wrap, .wp-switch-editor {padding: 5px 10px;}</style>',
			'editor_class'  => 'form-field tinyeditor',
			'teeny'         => 0,
			'dfw'           => 0,
			'tinymce'       => 1,
			'quicktags'     => 1,				
			'drag_drop_upload' => false
		) );?>
		<p class="description"><?php _e( 'Enter a value for this field','cxc-codexcoach' ); ?></p>
	</div>
	<?php
}

add_action( 'product_cat_edit_form_fields', 'cxc_taxonomy_edit_meta_field', 10, 2 );

function cxc_taxonomy_edit_meta_field( $term ) {

	// put the term ID into a variable
	$term_id = $term->term_id;

	// retrieve the existing value(s) for this meta field. This returns an array
	$term_meta = get_term_meta( $term_id, 'custom_term_meta', true ); ?>
	<tr class="form-field">
		<th scope="row" valign="top"><label for="custom_term_meta"><?php _e( 'Example meta field', 'cxc-codexcoach' ); ?></label></th>
		<td>
			<!--<input type="text" name="custom_term_meta" id="custom_term_meta" value="<?php echo ( !empty($term_meta) ) ? $term_meta  : ''; ?>">-->
			<?php 
				wp_editor(get_term_meta( $term_id, 'custom_term_meta', true ), 'custom_term_meta', array(
					'wpautop'       => 1,
					'media_buttons' => 1,
					'textarea_name' => 'custom_term_meta',
					'textarea_rows' => 5,
					'tabindex'      => null,
					'editor_css'    => '<style>.quicktags-toolbar, .wp-editor-tools, .wp-editor-wrap, .wp-switch-editor {padding: 5px 10px;}</style>',
					'editor_class'  => 'form-field tinyeditor',
					'teeny'         => 0,
					'dfw'           => 0,
					'tinymce'       => 1,
					'quicktags'     => 1,	
					'value'       => TRUE,
					'drag_drop_upload' => false
				) );?>
			<p class="description"><?php _e( 'Enter a value for this field','cxc-codexcoach' ); ?></p>
		</td>
	</tr>

	<?php
}

add_action( 'edited_product_cat', 'cxc_save_taxonomy_custom_meta', 10, 2 );  
add_action( 'create_product_cat', 'cxc_save_taxonomy_custom_meta', 10, 2 );

function cxc_save_taxonomy_custom_meta( $term_id ) {
	if ( isset( $_POST['custom_term_meta'] ) && !empty( $_POST['custom_term_meta'] ) ) {
		update_term_meta( $term_id, 'custom_term_meta', $_POST['custom_term_meta'] );
	}
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы