• Resolved spinhead

    (@spinhead)


    shortcode in functions.php

    /*       [this_post_num] shortcode      */
    add_shortcode( 'this_post_num', 'this_return_post_id' );
    
    function this_return_post_id() {
        return get_the_ID();
    }

    code in the post

    <input id="handle[this_post_num]" type="checkbox" name="collapse" />
      <h2 class="handle">
        <label for="handle[this_post_num]">html</label>
      </h2>

    view source in the page

    <input id="handle[this_post_num]" type="checkbox" name="collapse" /></p>
    <h2 class="handle">
        <label for="handle957">html</label><br />
      </h2>

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’ll need to try <input id="<?php do_shortcode( handle[this_post_num]); ?>"

    Thread Starter spinhead

    (@spinhead)

    Code in view source is now

    <input id="<?php do_shortcode( handle[this_post_num]); ?>” type=”checkbox” name=”collapse” /></p>

    In other words, same result, it’s just showing the new code instead of the old.

    Will php really run in a form field in a post? Seems unlikely to me.

    The reason is the protection of the HTML elements via kses. input is not included there. You have to add this element first for this to work. So:

    function custom_wp_kses_allowed_html( $htmlTags ) {
        if( empty($htmlTags["input"]) ) {
    	    $htmlTags["input"] = [
    		    'type' => true,
    		    'class' => true,
    		    'name' => true,
    		    'value' => true,
    		    'id' => true
    	    ];
        }
        return $htmlTags;
    }
    add_filter( 'wp_kses_allowed_html', 'custom_wp_kses_allowed_html', 10, 1);

    The example of @sterndata will not work when inserted in Gutenberg or most other editors because the PHP code is not parsed in that case. Can be seen in your page at this very moment, which is why I assume you’ve already tried this now ??

    I have successfully tested the above locally. You don’t have to change anything in your HTML code (so just use the one you initially tried).

    Thread Starter spinhead

    (@spinhead)

    Ta daa. Thanks muchly. Will research kses.

    Note: because this seems to be a day where everyone skips over little assumptions:

    You have to add this element first for this to work.

    By “this element” you mean “the input element”
    By “add” you mean “add to what kses allows”
    And the whole batch goes in functions.php
    Though it’s hard to tell if by “add this . . . first” if it needs to be added to functions.php above/before the shortcode, or simply “do this, then it’ll work.”

    Not trying to be a jerk. Just having a difficult time getting folks to write clear coherent responses to what I believe are clear coherent queries. Your answer is correct, but someone with a tiny bit less information and experience would be back here asking for clarification, perhaps multiple rounds of it.

    Again, thanks for the perfect solution, and a research project that’ll deepen my understanding of this stuff.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘shortcode fails in input field id but works elsewhere’ is closed to new replies.