• Resolved kemalkautsar

    (@kemalkautsar)


    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 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @kemalkautsar

    I hope you are doing well today.

    I pinged our SLS Team to review your query and see what will be possible in this case. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again

    There isn’t a way in Forminator’s API to determine the form’s ID by form title. But we have created a simple snippet to show the form by form title. Please replace your code with the following one:

    add_shortcode( 'wd-couple-form', 'show_couple_form' );
    function show_couple_form( $atts ) {
    	$name = ! empty( $atts['name'] ) ? $atts['name'] : '';
    	if ( ! $name ) {
    		return esc_html__( 'Form name attribute is required!', 'forminator' );
    	}
    
    	$form_title = str_replace( ' ', '-', strtolower( $name ) );
    
    	$form_id = wpmudev_form_id_by_name( $form_title );
    
    	if ( empty( $form_id ) ) {
    		return esc_html__( 'Form does not exists.', 'forminator' );
    	}
    
    	$html = do_shortcode( '[forminator_form id="' . $form_id . '"]' );
    
    	return $html;
    
    }
    
    function wpmudev_form_id_by_name( $page_title, $output = OBJECT ) {
    	global $wpdb;
    	$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='forminator_forms'", $page_title ) );
    	if ( $post ) {
    		return $post;
    	}
    
    	return null;
    }

    Instead of using the simple shortcode [wd-couple-form] you need to pass the form name also in that shortcode. For example: [wd-couple-form name=”test?form”]

    Kind Regards,
    Kris

    Thread Starter kemalkautsar

    (@kemalkautsar)

    Hi Kris & the SLS team,

    Yeah I was playing around and teach myself how to make proper wp shortcode while waiting on your answer (it was weekend so its understandable). and what i end up with is here:

    add_shortcode('wd-formshow','show_couple_form');
    
    function show_couple_form($atts){
    
    ? ? $a = shortcode_atts( array(
    
    ? ? ? ? 'title' => 'Header Color Selector'
    
    ? ? ), $atts );
    
    ? ? $formi_post = get_page_by_title( $a['title'], '', 'forminator_forms' );
    
    ? ? if ($formi_post){
    
    ? ? ? ? $formi_post = '[forminator_form id="'.$formi_post->ID.'"]';
    
    ? ? ? ? $execute_sc = do_shortcode($formi_post);
    
    ? ? ? ? return $execute_sc;
    
    ? ? }
    
    }

    as you can see I use get_page_by_title() that is deprecated, but i need it in a pinch just to show the function for my customer.

    I will try out your function and report back to you!

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @kemalkautsar

    We didn’t hear back from you for a while so I’m assuming that shared code (or your own solution) worked good enough and I’m marking it as resolved.

    However, if you still have questions or need help with this, just let us know and we’ll be happy to continue.

    Best regards,
    Adam

    Thread Starter kemalkautsar

    (@kemalkautsar)

    Adam Hi!

    yes this is good enough! also thank you for your support on the Pro version that we are doing as well!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘how to get form ID using form title’ is closed to new replies.