• Resolved Tunn

    (@iuriem)


    Hi,

    I need to filter my forms with hf_form_html filter hook, differently for different forms, so I need to get the form slug for this. I found a way how to get the form slug, but maybe exists a proper plugin solution for this. Or not?

    This is my way:

    add_filter( 'hf_form_html', function( $html ) {
        $doc = new DOMDocument( '1.0', 'UTF-8' );
        $doc->loadHTML( $html );
        $attr = array();
        $form = $doc->getElementsByTagName( 'form' )->item(0);
        foreach( $form->attributes as $attribName => $attribNodeVal ) {
            $attr[$attribName] = utf8_decode( $form->getAttribute( $attribName ) );
        }
    
        if( $attr['data-slug'] == 'my-form-slug' ) {
            ...
        }
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Tunn

    (@iuriem)

    Ok, my code for getting the current form slug can be simplified, but the question remains: is there a plugin specific or simpler way to do this?

    add_filter( 'hf_form_html', function( $html ) {
        $dom = new DOMDocument();
        $dom->loadHTML( $html );
        $slug = $dom->getElementsByTagName( 'form' )->
            item(0)->getAttribute( 'data-slug' );
    
        if( $slug == 'my-form-slug' ) {
            ...
        }
    }

    Hi,

    The form should be in the second argument, so I think it should be:

    add_filter( ‘hf_form_html’, function( $html, $form ) {
    if($form->slug == ‘…’)
    {

    }
    }

    I’m sorry I did not have time to test it so let me know if it doesn’t work!

    Hope that helps. If you have any questions, please let me know!

    Thread Starter Tunn

    (@iuriem)

    @lapzor,

    No succes! I suppose that in the case of hf_form_html filter hook this doesn’t work, it accept only one argument – $html, that contains the form itself.

    Yeah you’re right….

    I found that the slug is actually in the html, so you could use str_pos:

    add_filter( ‘hf_form_html’, function( $markup ) {
    if(strpos($markup, ‘data-slug=”my-slug”‘)) {
    $markup .= ‘My Slug is found’;
    } else $markup .= ‘My slug wasn’t found’;
    return $markup;
    });

    Just tested this on my own setup and it worked for me.

    Hope that helps. If you have any questions, please let me know!

    Thread Starter Tunn

    (@iuriem)

    @lapzor,

    Very good! Thank you very much!

    add_filter( ‘hf_form_html’, function( $html ) {
    $doc = new DOMDocument( ‘1.0’, ‘UTF-8’ );
    $doc->loadHTML( $html );
    $attr = array();
    $form = $doc->getElementsByTagName( ‘form’ )->item(0);
    foreach( $form->attributes as $attribName => $attribNodeVal ) {
    $attr[$attribName] = utf8_decode( $form->getAttribute( $attribName ) );
    }

    if( $attr[‘data-slug’] == ‘my-form-slug’ ) {

    }
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get the form slug in the hf_form_html filter hook’ is closed to new replies.