Adding WordPress Posts to Elementor Pro Form Select field - php

I have a client who wants me to create a field in the form where the customer can select additional products to add to their quote. The products are set up as posts. I am using a WordPress installation, with Elementor Pro and Dynamic ooo plugins, and I was able to create a call to collect all the post titles using:
Using the Elementor Pro using Dynamic ooo Token: [query:post] (as seen in the image)
Here are the options in Elementor Pro Forms Module
This is what the form looks like when you try to select an option
The issue I am having is that it is bulking the post titles into one giant select item rather than single selection items separated by the post title. What I want to happen is that I can call all the post titles, and they will populate into separate the items into a multi-select where any of the post titles can be selected and added to their quote request. Does anyone have an idea of how to do this?
Here is the link to see what I am doing:
http://www.closeout.gbwcompanies.com/product-closeouts/stone-veneer-closeouts/blue-ridge-matterhorn/
Click on the blue button to Request a Quote and the form popup should show you what I am talking about. You will see the select list on the first step of the form with all the post titles in one long item.
Here is a link to the Dynamic ooo docs that I used to get me this far:
Dynamic ooo Docs for Tokens
Edited:
Here is a code I am currently trying but is not working:
function get_posts_title_filter($data) {
global $post;
$args = array( 'cat' => 4 );
$myposts = get_posts( $args );
$data = '<select select name="lstdate" id="prods" onchange="document.getElementById(\'prods\').value=this.value;"><option></option>';
foreach( $data as $title ) {
$output.=$title.'
';
}
foreach ( $myposts as $post ) : setup_postdata($post);
$title = get_the_title();
$output .= '<option value="'. $title .'">'. $title .' </option>';
endforeach;
$data .= "</select>";
return var_dump($data);
}

I just figured out the answer to my question. I am so sorry for not seeing this sooner. I found this article Tokens Collection Link
If you are looking to do this, use the following shortcode:
[query:posts|options]

i don't have the plugin. So i can test myself and give you final working solution. But from what i have after read the Dynamic ooo docs, this might be work:
open your theme's functions.php file.
insert this codes at very bottom (above php close tag "?>" if have) and save it:
function get_posts_title_filter($data) {
$output = '';
foreach ($data as $title) {
$output .= $title.'
';
}
return $output;
}
in your token setting, using this token:
[query:post|get_posts_title]
if you get any error, send back to me for debug and correction.

Related

Gravity Forms Dynamically Populate Post/Page Category

I would like to be able to use the same form on several pages, and know which page a submitted form came from. For a long list of reasons, I need to do that based on category.
I found code to add categories to pages, works great.
But I can't figure out how to get Gravity Forms to dynamically populate a field with the category.
I've selected "Allow field to be populated dynamically" on the form, I've set the parameter name to "pagecategory"
Here's what I've got - it does nothing:
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$value = get_the_category( $post->ID,'metakeyname',true);
return $value;
}
get_the_category() actually returns an array of all category that is assigned to that post. The code below works for me to fetch the name of first category only.
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$categories = get_the_category( $post->ID,'metakeyname',true);
$value = $categories[0]->cat_name;
return $value;
}
I'm a bit surprised this type of need/question isn't more prevalent. I had to do this same thing, but for a custom taxonomy. In case anyone lands here in need of how to do Mash's answer, but with a custom taxonomy, the following is working for me. My custom taxonomy is "services," which you can see replaces 'metakeyname' in Mash's answer.
//Get Page Custom Taxonomy - For Gravity Forms
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$terms = get_the_terms( $post->ID,'services',true);
$value = $terms[0]->name;
return $value;
}

Disable custom post type links depending on the taxonomy option

I have a list (shown as a grid by this plugin) of Custom Post Types, but I need the ones under one taxonomy value to have a link to the inner page.
Example: I have the taxonomy "example" with 3 options (OptionA
- OptionB
- OptionC), but I just want the ones under "OptionB" to have an inner link:
I know there is a solution via css (hiding the links styles), but I would like to keep the whole site clean of css tricks.
Is there any way to achieve this functionality using PHP?
Here is the part of the PHP code that adds the link to the titles:
$output .= '<div class="pl-detailcnt">
<h4 class="pl-title left-txt">';
if (isset($this->pw_hide_date) && ($this->pw_hide_date=='off')){
$output .= '<span class="pl-date">'. get_the_date($this->pw_date_format).'</span>';
}
$output .= ''. get_the_title().'</h4>
</div>';
Since I can't add the whole code (max characters exceeded) as #Dontfeedthecode suggested, here it goes: http://ideone.com/HeVfny
You can query for taxonomies for the post then check to see if your custom post type is in the array it returns:
$post_types = get_object_taxonomies( $post );
if( in_array( 'your taxonomy name', $post_types )) {
// Show link
}
SOLUTION (using a bit of JQuery).
First step:
Add the term-slug as a CSS class, so I can distinguish between classes for customize it later with JQuery.
<div class="add_your_random_class_here '.$term->slug.'">
Second Step:
Disable the links that have the term-slug class created.
<script>
jQuery(function() {
jQuery('.here-goes-your-new-based-slug-class').click(function(e){e.preventDefault();});
});
</script>
Any improvement will be welcome

Wordpress Dropdown Categories All Links to Single

I'm having an issue where, even though the page renders originally with "All" selected in the dropdown, if you go to a category (renders fine) and then back to "All" in the dropdown, you see a single post. This is instead of the originally displayed page with all of the categories. I need the page to basically render the same page for "All" regardless. Any thoughts?
wp_dropdown_categories('show_option_all=All&hide_empty=0&show_count=0&orderby=name&echo=0');
I did a similar post on WPSE last week and it seems that the two might be related. For convenience, here is the post
Here is a variation of the code that you use. I'm using get_categories() here to achieve the same goal. I had to adjust my code slightly to make it acceptable for your need.
There are a however other modifications you have to make for this to work. When you select the All Categories option, you will be taken to a page that will display what ever you need to display. This page you will have to manually create
There is no index archive pages in Wordpress as you might know. (Check out this post I have done on the same subject). What this means is, domain.com/category/ returns a 404.
So, to make this all work, you'll have to make a copy of page.php, rename it to something like page-category.php (see the codex on how to create custom page templates), open it up, create your custom query to display what you would like to display when this page is visited
You now need to create your page in the back end. I would suggest that you use the slug category so that when you visit domain.com/category/, this page would be displayed. (Just remember, you cannot create child pages for this page, it will break the hierarchy). I have also made the code to go to domain.com/category/ when All Categories is selected
Apart from that, the code should work fine. You just need to check the URL structures maybe, and also set the parameters in get_categories() to suite your needs. Here is the drop down code.
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$option = '<option value="' . get_option('home') . '/category/">All Categories</option>'; // change category to your custom page slug
$categories = get_categories();
foreach ($categories as $category) {
$option .= '<option value="'.get_option('home').'/category/'.$category->slug.'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
}
echo $option;
?>
</select>
EDIT
I actually had an idea here that will come in handy. I've recently done an answer on displaying all categories in a list with all post titles under the specific category. This same idea can be used in your page-category.php template.
When a user selects the All Categories option, they will be taken to this page which will list all categories and post title.
Here is the complete code: (for an explanation of the code, see my post here)
In your functions.php
add_action( 'transition_post_status', 'publish_new_post', 10, 3 );
function publish_new_post() {
delete_transient( 'category_list' );
}
In your template where you need to display your list
<?php
if ( false === ( $q = get_transient( 'category_list' ) ) ) {
$args = array(
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '' . get_the_title() .'';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '' . $category->name . '';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
set_transient( 'category_list', $q, 12 * HOUR_IN_SECONDS );
}
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
?>

Adding a Taxonomy Filter to a Custom Post Type

There is an amazing conversation from about two years ago on the Wordpress Answer site where a number of people came up with good solutions for adding a taxonomy filter to the admin screen for your custom post types (see URL for screen I'm referring to):
http://[yoursite.com]/wp-admin/edit.php?s&post_status=all&post_type=[post-type]
Anyway, I loved Michael's awesome contribution but in the end used Somatic's implementation with the hierarchy option from Manny. I wrapped it in a class - cuz that's how I like to do things -- and it ALMOST works. The dropdown appears but the values in the dropdown are all looking in the $_GET property for the taxonomies slug-name that you are filtering by. For some reason, I don't get anything. I looked at the HTML of the dropdown and it appears ok to me. Here's a quick screenshot for some context:
You can tell from this that my post-type is called "exercise" and that the Taxonomy I'm trying to use as a filter is "actions". Here then is the HTML surrounding the dropdown list:
<select name="actions" id="actions" class="postform">
<option value="">Show all Actions</option>
<option value="ate-dinner">Ate dinner(1)</option>
<option value="went-running">Went running(1)</option>
</select>
I have also confirmed that all of the form elements are within the part of the DOM. And yet if I choose "Went running" and click on the filter button the URL query string comes back without ANY reference to what I've picked.
More explicitly, the page first loads with the following URL:
/wp-admin/edit.php?post_type=exercise
and after pressing the filter button while having picked "Went Running" as an option from the actions filter:
/wp-admin/edit.php?s&post_status=all&post_type=exercise&action=-1&m=0&actions&paged=1&mode=list&action2=-1
actually, you can see a reference to an "actions" variable but it's set to nothing and as I now look in detail it appears that the moment I hit "filter" on the page it resets the filter drop down to the default "Show All Actions". Can anyone help me with this?
BTW, I've attached the PHP code here (although I'm now starting to lean toward it being a js issue): gist.
I was having the same issue and added a small fix, basically checking if the taxonomy $_GET parameter is set. I hope it helps:
add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {
global $typenow;
$taxonomy = 'mytaxonomy'; // Change this
if( $typenow != "page" && $typenow != "post" ){
$filters = array($taxonomy);
foreach ($filters as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>Show All $tax_name</option>";
foreach ($terms as $term) {
$label = (isset($_GET[$tax_slug])) ? $_GET[$tax_slug] : ''; // Fix
echo '<option value='. $term->slug, $label == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";
}
}
}
Try this class, it works wonderful

Wordpress: include content of one page in another

How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.

Categories