Get featured image of post once gravity forms selects that post - wordpress

I'm creating a form using Gravity Forms where users will be able to select type of aircrafts, add different flight hours, perks, and other add-ons to their flight. I'm populating the checkbox fields in Gravity Forms with a custom post type I created.
Once a user selects the flight and other perks available, to the left of their selections, I'm populating an HTML Field from GF to display {all_fields}. Since the aircrafts checkboxes are being populated by a custom post type, I want to be able to grab that featured image of that post type and then display it above the location of the HTML Field.
The code listed here is the function I'm using to populate those checkboxes with the custom post type:
add_filter( 'gform_pre_render_6', 'ldm_do_bac_aircraft_large' );
add_filter( 'gform_pre_validation_6', 'ldm_do_bac_aircraft_large' );
add_filter( 'gform_pre_submission_filter_6', 'ldm_do_bac_aircraft_large' );
add_filter( 'gform_admin_pre_render_6', 'ldm_do_bac_aircraft_large' );
function ldm_do_bac_aircraft_large( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( strpos( $field->cssClass, 'bac-aircraft-large' ) === false ) {
continue;
}
$posts = get_posts( array(
'numberposts' => -1,
'post_status' => 'publish',
'post_type' => 'ldm_build_card',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'ldm_build_card_categories',
'field' => 'slug',
'terms' => array( 'large' ),
),
),
) );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
$field->choices = $choices;
}
return $form;
}
So my goal is for when the user selects either a Large, Super-Mid, Mid, or Small aircraft (and those are the categories) the next page of the multi-page GF will not only display that plane the user selected, which is does, but I want it to display that post/Aircrafts featured image above it. So the image will change depending on the aircraft selected.
Does anyone have an idea how I can get the featured image of that post ID depending on what the user selects in GF?
UPDATE 6/24
Ok so to be more clear to what I'm doing. Im using gravity forms Multi-page to create a form to get aircrafts, perks, hours, add-ons, etc. I needed to populate the Aircrafts 'page' of the form with the custom post type. I have these populated but I needed to display the post information such as a Featured Image, Aircraft Name, Range, etc.. above the actual input. I have done that and satisfied but I needed to pull the image of the post so when the form then is submitted and navigates to the next page of the form, it would display that Post Featured Image along with all the other fields I selected.
In this case I selected an aircraft which it displays the title and I needed the Featured Image to go with it. Originally this needed to be an AJAX Gravity Form. The image would not display and would display as undefined when I checked the console log because I'm also moving things around with jQuery.
You may say why am I doing it this way, well it's just what I decided to do because this is my first crack at a multi-page GF where I needed to display a custom post type as selections.
Ok so since the image was not displaying, I decided to turn off AJAX on the Form because I had this idea that the script that is spitting out the filterable categories of the aircrafts was being removed. I was correct so I turned off AJAX and the image now displays. So my Question now is how can I include this script to be included in the AJAX request for this particular Form.
Below I have my jQuery to display the post image in a specific area once that aircraft has been selected.
var checkboxValue = $('#gform_page_6_1 input[type=checkbox]:checked').val();
var aircraftSelect = $('#membershipAircrafts').find('[data-aircraft-title="'+checkboxValue+'"]');
var aircraftImage = $(aircraftSelect).find( '.membership-aircraft-image' );
console.log( aircraftImage );
$('.summary-image').html( aircraftImage );
Now here is the script that I thought was being removed:
function ldm_add_membership_aircrafts() {
?>
<script type="text/template" id="single-membership-listings">
<% _.each( membershipAircrafts, function( membershipAircraft ) { %>
<div id="aircraft<%= membershipAircraft.id %>" class="single-membership-inner clearfix col-sm-4" data-aircraft-title="<%= membershipAircraft.title %>">
<% if( membershipAircraft.imageURL ) { %>
<img class="membership-aircraft-image" src="<%= membershipAircraft.imageURL %>" alt="<%= membershipAircraft.title %>" />
<div class="single-membership-content-container">
<% } else { %>
<div class="single-membership-content-container">
<% } %>
<div class="single-membership-title"><%= membershipAircraft.title %></div>
<div class="single-membership-range">
<p id="membership-label">Range:</p>
<p id="membership-range-content"><%= membershipAircraft.range %></p>
</div>
<div class="single-membership-passengers">
<p id="membership-label">Passengers:</p>
<p id="membership-passengers-content"><%= membershipAircraft.passengers %></p>
</div>
<div class="single-membership-baggage">
<p id="membership-label">Luggage Capacity:</p>
<p id="membership-baggage-content"><%= membershipAircraft.baggage %></p>
</div>
<div class="single-membership-cabinDimensions">
<p id="membership-label">Cabin Dimensions:</p>
<p id="membership-cabinDimensions-content"><%= membershipAircraft.cabinDimensions %></p>
</div>
</div>
</div>
<% }); %>
</script>
<script type="text/template" id="single-categories-listings">
<% var categoryName; %>
<% var count = 0; %>
<div class="membership-categories-inner">
<% _.each( membershipCategories, function( membershipCategory ) { %>
<% if( count !== 0 && categoryName !== membershipCategory.taxonomy ) { %>
</div>
<% } %>
<div data-term-id="<%= membershipCategory.id %>" class="single-membership-category <%= membershipCategory.termName %><% if( count === 0 ){ %> category-active <% } %>"><%= membershipCategory.termName %></div>
<% categoryName = membershipCategory.taxonomy; %>
<% count++; %>
<% }); %>
</div>
</div>
</script>
<?php
}
Thinking about it I'm not sure the script is being removed but AJAX is actually removing the custom fields that I am placing as a partial and using jQuery to add them above the actual inputs
echo '<li id="aircraft-tax" class="gfield">';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo '<div id="membershipCategories" class="membership-categories"></div>';
echo '</div>';
echo '</div>';
echo '</li>';
echo '<li id="aircraft-content" class="gfield">';
echo '<div class="row">';
echo '<div class="col-sm-10 col-sm-offset-1">';
echo '<div id="membershipAircrafts" class="membership-aircrafts"></div>';
echo '</div>';
echo '</div>';
echo '</li>';
Here is the jQuery that i'm using to place above the inputs:
$('#field_6_2').after( $('#aircraft-tax') );
$('#aircraft-tax').after( $('#aircraft-content') );
What Can I do to prevent AJAX from removing this information. If I can include this into the AJAX request then I feel the images will display just like they do if I turn AJAX off on the GF.
**
UPDATE 3
**
I found my issue. Turns out that when I enable ajax on Gravity Forms, it is not adding a template that I originally load on the page. For example I have a partial that I'm including on my page:
get_template_part( 'partials/membership-template', 'aircraft' );
This is being added originally but once I navigate to the next form page (because it is a multi-page form) this template that is being called on the page gets removed.
So how can I keep this template or partial from being removed after the form navigates to the next form page?

Since your form is having users select a category of plane (from you custom taxonomy) you need to get an actual post in order to display a featured image. Add this where you are rendering your form page.
$args = array(
'posts_per_page' => 1,
'post_type' => 'ldm_build_card',
'ldm_build_card_categories' => 'large',//or selected type
'post_status' => 'publish'
);
$show_image = get_posts( $args );
if( $show_image ) {
get_the_post_thumbnail( $show_image[0]->ID, 'image-size');
}
UPDATE: In case I misunderstood your question:
The post ID should also be in your form input so get_the_post_thumbnail( $entry[id-of-selection-field], 'image-size');would also output your image.
UPDATE 2:
To get data from one page to another add an html field to the second page, use a pre_render filter:
add_filter( 'gform_pre_render_{form_id}', 'populate_html' );
Then use GFFormDisplay to get the page. Loop through form fields to get your post id and get the image from there.
function populate_html( $form ) {
//this is a 2-page form with the data from page one being displayed in an html field on page 2
$current_page = GFFormDisplay::get_current_page( $form['id'] );
$html_content = "The information you have submitted is as follows:<br/><ul>";
if ( $current_page == 2 ) {
foreach ( $form['fields'] as &$field ) {
//gather form data to save into html field (id 3 on my form), exclude page break
foreach ( $field->inputs as $input ) {
if ($input['id'] == {selected-plane-input}) {
$input_name = 'input_' . str_replace( '.', '_', $input['id'] );
$value = rgpost( $input_name );
$html_content .= '<p>' . wp_get_attachment_url( get_post_thumbnail_id($value)). '</li>';
}
}
}
}
foreach ( $form['fields'] as &$field ) {
if ( $field->id == {your-html-field-id} ) {
//set the field content to the html
$field->content = $html_content;
}
}
return $form;
}

Related

Wordpress - How to render option page after form submit in wp-admin?

I am trying to create a custom plugin in which form is submitting on first option page added.
I want to fetch posts from database on the basis of submitted data and show on other page in wp-admin to further mark these posts to add some more data with these posts.
Problem: Upon submitting a form to action added, this shows only the values of $_POST on a broken page instead of proper admin page.
Question: how to show fetched data on proper admin page?
function listposts_register_lists_page() {
//brings all posts at first time
add_options_page('Set Message in Selected Posts', 'Alert Messages', 'manage_options', 'listposts_plugin', 'listposts_lists_page'); //listposts_option_page
//Triggers upon Add New Button
add_options_page( 'Set Message in Selected Posts', '', 'manage_options', 'add-new-message', 'add_new_message_page');
}
add_action('admin_menu', 'listposts_register_lists_page');
Form
function add_new_message_page()
{
?>
<div class="wrap">
<h1>Set Alert Message</h1>
<form method='POST' action='<?php echo admin_url( 'admin-post.php' ); ?>'>
<input type='hidden' name='action' value='fetch_posts'/>
<?php wp_nonce_field( 'submitform', 'submitform_nonce' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Message</th>
<td><textarea id="listposts_option_name" name="listposts_option_name" value="<?php echo get_option('listposts_option_name'); ?>" ></textarea></td>
</tr>
<?php
// Excluded CPTs. You can expand the list.
$exclude_cpts = array(
'attachment'
);
// Builtin types needed.
$builtin = array(
'post',
'page',
// 'attachment'
);
// All CPTs.
$cpts = get_post_types( array(
'public' => true,
'_builtin' => false
) );
// remove Excluded CPTs from All CPTs.
foreach($exclude_cpts as $exclude_cpt)
unset($cpts[$exclude_cpt]);
// Merge Builtin types and 'important' CPTs to resulting array to use as argument.
$post_types = array_merge($builtin, $cpts);
//$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
echo ' <tr valign="top">
<td><input type="checkbox" class="selectposttype" name="posttypes" value="'.$post_type.'" '.checked( $post_type, 1 ).' /></td>
<td scope="row"><label for="'.$post_type.'" >'.$post_type.'</label></td>
</tr>';
}
?>
</table>
<input type="submit" value="Send"/>
</form>
</div>
<?php
}
Function to handle form submit
function show_posts(){
//This should be displayed on proper Option page instead of white page
var_dump($_POST);
}
Here how to render this out put on proper wp-admin page in WP_LIST_TABLE ?

search results only from post title, category and tags

If it is default WordPress search or custom search form, If only matched the search text in a post title, category and tags.
The search form should like this.
<form class="search" action="<?php echo home_url( '/' ); ?>">
<input type="search" name="s" placeholder="Search…">
<input type="submit" value="Search">
<input type="hidden" name="post_type" value="kb_article">
</form>
We’ll name our custom search results templates using the WordPress filename conventions of {template}-{post_type}.php. In our case, that would result in search-kb_article.php. We could just check to see if that post_type=kb_article is set in the URL string and use get_template_part to load it if it is, but let’s go a step further. We’ll check to see if post_type is set in the URL string, and if it is, also check to see if custom search template exists for that post type using the {template}-{post_type}.php naming pattern. If it does, we’ll load it. If not, we’ll continue with the default search template.
<?php
// store the post type from the URL string
$post_type = $_GET['post_type'];
// check to see if there was a post type in the
// URL string and if a results template for that
// post type actually exists
if ( isset( $post_type ) && locate_template( 'search-' . $post_type . '.php' ) ) {
// if so, load that template
get_template_part( 'search', $post_type );
// and then exit out
exit;
}
?>
Use below query can get solution,
$args = array(
'post_type' => 'post',
's' => $keyword,
'cat' => $catSearchID,
'tag' => $tag-slug,
'posts_per_page' => -1,
'location' => $post_location_search ,
);
$wp_query = new WP_Query($args);

Product page details tabs to lightbox

in my woocomere product page there is tabs of a product to display different information (description, etc). But i wish to be able to in a specific tab turn in a ligthbox popup that shows text or a image. Is there any plugin available in wordpress for it?
Please try below code in function.php to show tab content in light box. Might be you will have to customize more but basic structure i am sharing with you.
add_action( 'woocommerce_product_write_panel_tabs','outputTabTitle');
add_action( 'woocommerce_product_write_panels','outputTabEditContent');
add_filter( 'woocommerce_product_tabs','productTab');
function outputTabTitle ()
{
?>
<li class="custom_tab">
Additional Information
</li>
<?php
}
function outputTabEditContent ()
{
global $woocommerce, $post;
echo '<div id="custom-tab" class="panel woocommerce_options_panel">';
/************Please put you lightbox text and image here.
echo '</div>';
}
function productTab ( $tabs )
{
$tabs['custom-tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'outputTabEditContent'
);
return $tabs;
}

Advanced Custom Fields (ACF) Checkbox - Promotion of post to 'top story'

I am using the ACF plugin which is awesome but am struggling with a feature of it, namely the checkbox.
I am trying to use the checkbox as a means of promoting a blog post to a 'top story'.
So I have set up a ACF checkbox field called 'top_story' and if it is checked it should promote the post and if it is not checked then it won't promote the post.
Now this does work but I keep getting the following error message whenever a blog post does not have that checkbox ticked.
Warning: in_array() [function.in-array]: Wrong datatype for second argument
I have simplified the code so it looks like this:
<?php
if( in_array( 'topstory', get_field('top_story') ) )
{
echo '<h1>This is a top story</h1>';
}
else
{
echo '<h1>This isn't a top story</h1>';
}
?>
So I guess what I want to know is what is going wrong here and how to rectify it? It looks like as there is no value in the array for the posts which aren't 'top stories' then there is no argument passed into the 'get-field' function and it falls over?
I was just going to hide the errors as essentially it still works but that doesn't sit comfortable with me and I'm sure I will need to do this again in the future.
Thanks for all your time and help in advance.
Maybe like this:
<?php
// args to check if "Top Story" os TRUE:
$args = array(
'cat' => '5', // Enter Category for "Topstories"
'posts_per_page' => 3, // How many posts to show if multiple selected "Backend"
'orderby' => 'date', // How to sort posts - date, rand etc...
'order' => 'asc', // How to order posts - ASC, desc etc...
'meta_key' => 'topstory', // Name of ACF field to filter through
'meta_value' => 'yes' // Yes = Show, No = Don't show
);
// The results:
$the_query = new WP_Query( $args );
// The Loop:
<?php if( $the_query->have_posts() ) :?>
<h1>This is a top story</h1>
<?php
while ( $the_query->have_posts() ) : $the_query- >the_post(); ?>
....
// Properties to show you post //
....
endwhile;
endif;
wp_reset_query(); // Reset/kill query
?>
Sounds like there are two things you could be running into here:
If a field isn't set, or doesn't exist, get_field() will return false.
If none of the options in a checkbox field are checked, get_field() will return an empty string.
In either case, you won't have an array to search with in_array, and will get a warning if you try.
I'd try this, following ACF's documentation. You should also consider using ACF's True/False field, which is designed for this sort of thing; the Checkbox field is more for multiple checkboxes, of which more than one can be true.
<?php
$topStory= get_field('top_story');
if($topStory) // Check whether this meta field exists at all
{
if(is_array($topStory) && in_array( 'topstory',$topStory ) {
echo "<h1>This is a top story</h1>";
}
else {
echo "<h1>This isn't a top story</h1>";
}
}
?>
If you had a True/False field, you would make it a bit simpler:
<?php
if(get_field('top_story')) {
echo "<h1>This is a top story</h1>";
} else {
echo "<h1>This isn't a top story</h1>";
}
?>

Can I use Wordpress' editor and media uploader outside of a Wordpress site?

I'm currently creating a simple CMS and I would really, really like to use the editor and mainly, image uploader from WordPress on my site. Is this possible?
I am aware that WordPress uses TinyMCE and that they do offer an image manager as a commercial add-on, which I will probably use if this is not possible.
You can use the wp function the_editor:
http://codex.wordpress.org/Function_Reference/the_editor
If you look up that function on google you'll find a bunch of pages describing how to insert the wordpress editor into plug-ins. They provide lots of different ways of doing what you're hopefully looking for.
I used something like:
<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
<div><h2>Title</h2>
<input type="text" id="title" value="" tabindex="1" name="title" AUTOCOMPLETE=OFF/>
<div>
<h2>Description</h2>
<?php the_editor('', 'description', 'title', true); ?>
</div>
<input type="hidden" name="action" value="post" />
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
<?php wp_nonce_field( 'new-post' ); ?>
</form>
Which you then have to save to the wordpress db using something like:
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
/* 'post_category' => array('cat' => '3'), */ // Usable for custom taxonomies too
'post_status' => 'pending', // Choose: publish, pending, draft, auto-draft, future, etc.
'post_type' => 'post' // Use a custom post type if you want to
);
$newID = wp_insert_post($post); // Pass the value of $post to WordPress the insert function
// http://codex.wordpress.org/Function_Reference/wp_insert_post
// wp_redirect( home_url() );
} // end IF
// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post');
That's the basics of it... Good luck!
Without knowing the details of your project, I can offer this as a starting point:
With respect to the image uploader, WordPress utilizes some version of the free upload handler Plupload in the current version of WP (3.3), which will handle image uploads.
http://www.plupload.com
If you are looking for features such as crop, resize, thumbnail, etc., then you are right - they do have a paid file and image managers (MCImageManager)
http://www.tinymce.com/enterprise/mcimagemanager.php

Resources