Quantcast
Channel: WordPress.org Forums » [Forminator Forms – Contact Form, Payment Form & Custom Form Builder] Support
Viewing all articles
Browse latest Browse all 1454

how to get form ID using form title

$
0
0

Replies: 0

Hello Forminator friends,

long story short, I’m making a website where I need to get an ID of a form using form title. So I have this function on my custom plugin where it can automatically create a pre-defined form (I learned how to make that on another tutorial).

To show the form on a page you need to call a shortcode, for example “[forminator_form id=”62″]”. The problem is: on a different site, the ID of the form is going to be different right?

What i can guarantee is the title of the form on my plugin is 100% going to be the same (example: “Kemal’s Registration Form” or in this case “test form”)

now, I have made a function to get an id of a post by entering the form title and a shortcode to echo the form’s id. However it’s showing nothing so far. here’s the code:

//this function is to get a page id by title

function getPageByTitle( $page_title, $output = OBJECT, $post_type = 'page' ){

    global $wpdb;

    if ( is_array( $post_type ) ) {

        $post_type           = esc_sql( $post_type );

        $post_type_in_string = "'" . implode( "','", $post_type ) . "'";

        $sql                 = $wpdb->prepare(

            "

            SELECT ID

            FROM $wpdb->posts

            WHERE post_title = %s

            AND post_type IN ($post_type_in_string)

        ",

            $page_title

        );

    } else {

        $sql = $wpdb->prepare(

            "

            SELECT ID

            FROM $wpdb->posts

            WHERE post_title = %s

            AND post_type = %s

        ",

            $page_title,

            $post_type

        );

    }

    $page = $wpdb->get_var( $sql );

    if ( $page ) {

        return get_post( $page, $output );

    }

    return null;

}

//this code is to echo the id if the function success

add_shortcode('wd-couple-form','show_couple_form');

function show_couple_form($atts){

    $pagename = getPageByTitle('test form');

    echo $pagename;

}

I think I am a literal stupid, know that get_page_by_title function has been deprecated but can’t get WP_query to work properly, and i stole that function from Stackoverflow, and can’t get it to work.

now, what I’m thinking is this :
1. Is there a way in Forminator’s API to determine the Form’s ID? that way I don’t need the hassle of making an abomination of a function like above
or
2. Where did I do wrong on the function that I tragically copy-paste. because the add_shortcode part is sure as hell too simple to fail.

p/s: you might want to explain to me as if I’m 5 years old or as if I’m a cat.

Thanks for your help!


Viewing all articles
Browse latest Browse all 1454

Trending Articles