NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Nov 16 2021
Estimated reading time : 2 minutes, 48 seconds - 208 words
Share the post "Shortcode WP: Display or send post ID content via Formidable Form"
Shortcode WP: In this tutorial, we will show you how to create a Formidable form which in turn will be used to display a post ID’s content in a user’s email. First, we need to create a form that will allow us to choose the title of our post or the title.
Our newly created form will include two fields: Email and Select. In our example below, our Select field will allow us to display all post existing titles, publish and then convert them into selectable choices in the Select field.
In order to do so, we will use Populate Fields which will insert all post titles into our Select field.
add_filter('frm_setup_new_fields_vars', 'add_a_field_function_formidable', 20, 2); add_filter('frm_setup_edit_fields_vars', 'add_a_field_function_formidable', 20, 2); function add_a_field_function_formidable($values, $field){ // Changer le ID 154 par le ID de votre champ formidable form if($field->id == 154){ $arrayPositions = array(); $pushobj = array( value => "", label => "Sélectionnez votre article" ); array_push($arrayPositions, $pushobj); $positionQuery = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'orderby' => "menu_order", 'order' => "ASC") ); if ( $positionQuery->have_posts() ) { while ( $positionQuery->have_posts() ) { $positionQuery->the_post(); $pushobj = array( value => get_the_title(), label => get_the_title() ); array_push($arrayPositions, $pushobj); } wp_reset_postdata(); } $values['options'] = $arrayPositions; } return $values; }
We will now use a function that wil fetch the ID of the post selected in the form.
function get_post_by_title($page_title, $type = "post", $output = OBJECT) { global $wpdb; $post = $wpdb->get_var ( $wpdb->prepare ( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='$type'", $page_title ) ); if ($post) return get_post ( $post, $output ); return null; }
Now let’s create a shortcode that will generate the content of the post selected by the user so he can receive it by email.
function via_create_shortcode_questions_reponses(){ // Changer votre id par le field de votre champ select Titres des articles $title = do_shortcode( '[frm-field-value field_id=154]' ); $post_id = get_post_by_title($title); $content = get_post_field('post_content', $post_id); return $content; } add_shortcode( 'reponse', 'via_create_shortcode_questions_reponses' );
This feature of quick form can be easily displayed in a sidebar thanks to the Formidable form widget allowing you to select which form to display.
NB: Please make sure to do a back up of your thème, files and database before proceeding with these tutorials.