1

I created an additional description field for categories, but what I write in html is saved in MSQL without html, how can I add HTML support to this field?

For example, I write a statement like this:

<div class="class-name">It's very hot these days</div>

I am looking at phpmyadmin and the only saved is "It's very hot today". Since only the text part is saved, the table, image, title.. etc none appear when I add it. I can't even make it thick.

Codes;

/* Custom Field for Categories */
function dm_category_fields($term) {
    if (current_filter() == 'category_edit_form_fields') {
        $bottom_description = get_term_meta($term->term_id, 'bottom_description', true);
        ?>
        <tr class="form-field">
            <th valign="top" scope="row"><label for="term_fields[bottom_description]"><?php _e('Sayfa Sonu Açıklaması'); ?></label></th>
            <td>
                <textarea class="large-text" cols="50" rows="10" id="term_fields[bottom_description]" name="term_fields[bottom_description]"><?php echo esc_textarea($bottom_description); ?></textarea><br/>
                <span class="description"><?php _e('Kategori içeriğinin sonuna bir şeyler eklemek için. Shortcode destekler.'); ?></span>
            </td>
        </tr>
    <?php } elseif (current_filter() == 'category_add_form_fields') {
        ?>
        <div class="form-field">
            <label for="term_fields[bottom_description]"><?php _e('Sayfa Sonu Açıklaması'); ?></label>
            <textarea cols="40" rows="10" id="term_fields[bottom_description]" name="term_fields[bottom_description]"></textarea>
            <p class="description"><?php _e('Kategori içeriğinin sonuna bir şeyler eklemek için. Shortcode destekler.'); ?></p>
        </div>
    <?php
    }
}
add_action('category_add_form_fields', 'dm_category_fields', 10, 2);
add_action('category_edit_form_fields', 'dm_category_fields', 10, 2);

function wcr_save_category_fields($term_id) {
    if (!isset($_POST['term_fields'])) {
        return;
    }
    foreach ($_POST['term_fields'] as $key => $value) {
        update_term_meta($term_id, $key, sanitize_text_field($value));
    }
}
add_action('edited_category', 'wcr_save_category_fields', 10, 2);
add_action('create_category', 'wcr_save_category_fields', 10, 2);

2 Answers 2

2

sanitize_text_field() is intended to sanitize a value for use as plain-text. HTML tags are stripped in the process.

wp_kses() may be a more appropriate sanitization helper for your use-case and can be passed a custom list of permitted HTML tags. Alternately, you can pass a context name to use a pre-defined set. To use the list which is used in the process of sanitizing post content, for instance:

update_term_meta( $term_id, $key, wp_kses( $value, 'post' ) );

See the Theme Security section of the Theme Handbook for more information on Data Sanitization/Escaping in WordPress.

8
  • Thanks, this solved the problem. I have one more question. How do I switch to the editor? drive.google.com/file/d/1h35S6LbbWKNrNDOthK_p9tqkcnxoqUP1 I want it to be like the one above. The classic editor of Wordpress.
    – Murat Dinc
    Commented Aug 23, 2022 at 16:30
  • It sort of depends on the use-case, and is a bit of murky space at the moment. To globally revert to the classic editor for all post-types, you can use the Classic Editor plugin from the core WordPress contributors - but they may drop support for it at the end of the year. CPTs registered with the show_in_rest option disabled will also presently revert to the classic editor. Another solution is to use the Classic Editor block within the Block Editor, and potentially set it up in a locked template such that it's the only block.
    – bosco
    Commented Aug 23, 2022 at 16:43
  • 1
    I use and LOVE the Classic Editor plugin (you may also need the Classic Widgets plugin). @bosco I'd love to know where you heard the rumor of support being dropped for it - there are millions of users for that plugin and it's translated into almost 70 languages, I'd heard previously that there was no plan to sunset it anytime soon, let alone this year....can you please shed more light?
    – Trisha
    Commented Aug 23, 2022 at 18:32
  • No, I didn't mean that. I'm not asking the Classic Editor for content. The code in the topic adds an annotation field to the categories, but adds it as a "textarea". I want this place to appear as an editor, as in the current description area. With an edit to the code, this editor of Wordpress can be activated in that area, but I don't know how.
    – Murat Dinc
    Commented Aug 23, 2022 at 18:53
  • @Trisha No rumor - from the very first release it was always intended as a temporary transitional measure. It's briefly mentioned in the second paragraph of the plugin's description. But I suspect they'll be supporting it past that estimate - they've extended the support window before - and if and when the core contribs drop support, I reckon the larger community will be all too happy to pick up the mantle. A means to use the Classic Editor will likely be available for the foreseeable future :)
    – bosco
    Commented Aug 23, 2022 at 19:03
0

I solved the problem, the codes that work are as follows;

/* Custom Field for Categories */
function dm_category_fields($term) {
    if (current_filter() == 'category_edit_form_fields') {
        $bottom_description = get_term_meta($term->term_id, 'bottom_description', true);
        $description_field = 'bottom-description';
        $description_args = array(
            'wpautop' => false,
            'media_buttons' => true,
            'textarea_rows' => 10,
            'textarea_name' => 'term_fields[bottom_description]'
        );
        ?>
        <tr class="bottom-form-field">
            <th valign="top" scope="row"><label><?php _e('Page Break Description'); ?></label></th>
            <td>
                <?php wp_editor( $bottom_description, $description_field, $description_args ); ?>
                <span class="description"><?php _e('To add something to the end of category content. Supports shortcode.'); ?></span>
            </td>
        </tr>
    <?php
    } elseif ( current_filter() == 'category_add_form_fields' ) {
    ?>
        <div class="bottom-form-field">
            <label><?php _e('Page Break Description'); ?></label>
            <textarea rows="5" cols="72" id="term_fields[bottom_description]" name="term_fields[bottom_description]"></textarea>
            <p class="description"><?php _e('To add something to the end of category content. Supports shortcode.'); ?></p>
        </div>
    <?php
    }
}
add_action('category_edit_form_fields', 'dm_category_fields', 10, 2);
add_action('category_add_form_fields', 'dm_category_fields', 10, 2);
function dm_save_category_fields($term_id) {
    if (!isset($_POST['term_fields'])) {
        return;
    }
    foreach ($_POST['term_fields'] as $key => $value) {
        update_term_meta($term_id, $key, $value);
    }
}
add_action('edited_category', 'dm_save_category_fields', 10, 2);
add_action('create_category', 'dm_save_category_fields', 10, 2);

Your Answer

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

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