Tagged: CPT, Custom Post Type, pagebuilder, testimonials
-
AuthorPosts
-
January 20, 2015 at 6:57 pm #382591
Hello
I want to have my site testimonials saved once and displayed in various pages/posts, in different display methods (i.e. sometimes as a grid, sometimes in a slider). Using the provided shortcode requires me to enter the testimonial details within the shorcode and I don’t see how I can extract them from a general site storage.
Please advise,
SharonJanuary 21, 2015 at 9:06 am #382900Hi Sharon. You can’t do this without modifying the built in enfold testimonial functionality. By default the testimonials are stored within the metadata of the page they are created on and it’s not practical to reuse them once they have been entered.
I’ve done what you’re asking by making an alternate builder element for testimonials (based on enfold’s) and telling WP/enfold to use mine instead. It pulls from a custom post type using a taxonomy term. I have testimonial posts assigned to categories like “front page” and my testimonial builder element allows the user to choose a category along with all the other normal testimonial options instead of entering individual testimonials. Then when my builder element is displayed it fetches the relevant testimonial posts and uses the built in shortcodes to display them.
Does that make sense?
If you’re comfortable with coding plugins and understand filters/actions you could do this as well. If you’re not it would be quite a challenge to do it. Let me know how experienced/comfortable you are with coding.
January 21, 2015 at 9:23 am #382905Hi Kevin. I had a feeling this would be the response…
I am very comfortable with coding and implemented what you describe in past themes I’ve built from scratch. I was hoping ENFOLD offered a shorter path, just like they do with portfolio items. (but couldn’t find it :-))
I will add a custom post type for the testimonials, however, I am new to ENFOLD. Is there a tutorial or have you any tips for me on how to implement an alternative testimonials builder?
I am working with a child theme anyway.Thank you very much for the help,
SharonJanuary 21, 2015 at 9:44 am #382916Hi Sharon. It’s almost midnight here. If I have time in the morning I’m give you a rundown on how to do it. It’s not rocket science but there are some steps involved. Stay tuned. :-)
January 21, 2015 at 9:04 pm #383346Okay, good morning! First, I’m going to assume that you have created your custom post type via a plugin. If not, you should consider doing so because you will be able to reuse this code on any Enfold-based site. The following assumes that your CPT is called “testimonials” and the taxonomy is called “testcat”. They can be something else; you’d just have to modify the code below accordingly. This also assumes that the CPT has a variety of meta fields to contain the information about the testimonial. Such as company, URL and link text. If you don’t know how to do any of the items above, let me know.
Before you get started, create 2-3 testimonials posts with your new CPT plugin and assign them to a category. Better yet, create 2-3 in two different categories so you can be sure that the correct ones are being displayed when you view the testimonials on the front end of the site later on.
All of the builder elements are stored in a folder inside Enfold. Look in the themes folder and find enfold > config-templatebuilder > avia-shortcodes. DO NOT modify any of these files since that will cause problems with upgrading Enfold later. Instead, uses the items here as a place to start when you want to override a shortcode/builder element.
Inside your plugin folder, create a subfolder called “avia-shortcodes”. It could really be called anything but I like to name it the same as what the theme uses. This is where you’re custom builder element(s) will live. You can put as many builder elements there as you wish and they will all get loaded into the builder at the same time. So, if you want to make one builder element that shows all testimonials from a specific category and another that loads one random testimonial you can do that by putting the correct code in this directory. For this example, we’ll just make the one builder element.
Copy testimonials.php from the enfold directory into the shortcode directory you just created in your plugin.
In your plugin code, add the following. This will tell enfold to load any shortcodes from your avia-shortcodes directory within the plugin folder. Notice that the function name contains the word “testimonials”. As you get more proficient at this you might be loading shortcodes for multiple plugins from multiple locations.
// register builder shortcodes add_filter('avia_load_shortcodes', 'pwm_include_shortcode_template_testimonials', 15, 1); function pwm_include_shortcode_template_testimonials( $paths ){ array_unshift($paths, dirname( __FILE__ ) . '/avia-shortcodes/'); return $paths; }
If you upload your plugin and activate it now, enfold will start using your new testimonials.php immediately. Since the testimonials.php file is wrapped in a “class_exists( ‘avia_sc_testimonial’ )” condition, enfold will load your testimonials.php file and when it goes to load it’s own there will already be an existing class with the same name so it will not load the enfold testimonials.php.
At this point, you’ve effectively hijacked the testimonials builder block and you can do whatever you want with it. Everything below will be done in your NEW testimonials.php file and I won’t refer to the old one again. The issue is that it’s not obvious in the backend that the builder is using your NEW block. So, find the following line of code in your NEW testimonials.php file:
$this->config['name'] = __('Testimonials', 'avia_framework' );
and change it to something like:
$this->config['name'] = __('CPT Testimonials', 'avia_framework' );
If you load the builder in the backend of WordPress, you should see the new name for the builder block in the toolbar. The new testimonials.php still works exactly like the one built into enfold, though, and doesn’t pull from your CPT yet. There are two things to deal with: (1) allow the user to choose a category of testimonials to display while they’re building the page in the builder and (2) display those testimonials on the front end when the page is displayed. So, let’s hand the first issue:
Altering the builder user interface
Find the line in your testimonials.php file that says:
function popup_elements()
This function handles all of the elements in the builder popup windows for this element. Each of the shortcode files has a function like this and is where you would go to modify the info the user has to enter for a given builder element. At the top of this function, we need to get the list of testimonial categories that the user will choose from to display. Add the following right after the function declaration and indent it accordingly
// get a list of available terms in the taxonomy_exists $terms = get_terms("testcat"); $testimonial_categories[ 'Select One' ] = null; if ( !empty( $terms ) && !is_wp_error( $terms ) ): // compile an array for display below foreach ( $terms as $term ) : $testimonial_categories[$term->name] = $term->slug; endforeach; endif;
Now we have an array of the available categories and we can display them in a dropdown SELECT box to the user. But currently the builder has the code that let’s you build testimonials one-by-one in a second popup window and that has to be removed. If you look a little farther down in the popup_elements() you’ll see a bunch of nested arrays that represent all the items that will get displayed to the user. Find the one that starts:
array( "name" => __("Add/Edit Testimonial", 'avia_framework' ),
This entire array including it’s “subelements” array has to be deleted. Stop deleting just before you see the next array:
array( "name" => __("Testimonial Style", 'avia_framework' ),
In place of what you just deleted, put the following code. This will create the dropdown menu of available testimonial categories:
array( "name" => __("Testimonial Category", 'avia_framework' ), "desc" => "The category of testimonials being displayed here", "id" => "testcat", "type" => "select", "std" => "", "subtype" => $testimonial_categories ),
Also, scroll down a little farther and delete the entire function that starts with:
function editor_sub_element($params)
You can delete the preceding comments too if you want to keep your code clean.
Save your testimonials.php file and upload it to the server. Then go into a page and view the page builder (refresh if you were already looking at it). Insert the CPT Testimonials block and you should now see that you have the ability to select a testimonial category rather than entering them one-by-one. If you try to view the page on the front end, though, it will probably break or display a blank area.
Leveraging your custom post type
Now we have to tell testimonials.php to pull from your CPT when displaying on the front end. For simplicity, we’re just going to hijack the way the shortcodes already work. Immediately after the function you just deleted, you should find this one:
function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
We have to make the shortcode aware of the new “testcat” parameter so find the shortcode_atts array and add
'testcat'=> "",
as the first line right above ‘style’=>”grid”. Now, scroll down inside this function until you find the following line:
if($style != "grid") avia_sc_testimonial::$columns = 100000;
Immediately after that line, place the following block of code. Be careful not to replace anything.
// wp_reset_postdata() doesn't want to work for some reason so let's work around it global $post; $temp = $post; // get a list testimonials for this category // The Query $args = array( 'post_type' => 'testimonials', 'orderby' => 'menu_order', 'order' => 'asc', 'tax_query' => array( array( 'taxonomy' => 'testcat', 'field' => 'slug', 'terms' => $testcat, ), ), ); $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // get the post meta data $testimonial_content = get_the_content(); $custom = get_post_custom( $post_id ); $src = get_post_thumbnail_id( get_the_id()); $name = $custom[ 'pwm_testimonial_alt_name' ][ 0 ] ? $custom[ 'pwm_testimonial_alt_name' ][ 0 ] : get_the_title(); $subtitle = $custom[ 'pwm_testimonial_company' ][ 0 ]; $link = $custom[ 'pwm_testimonial_url' ][ 0 ]; $linktext = $custom[ 'pwm_testimonial_link_text' ][ 0 ]; // add a shortcode for the currernt testimonial $content .= "[av_testimonial_single src=\"$src\" name=\"$name\" subtitle=\"$subtitle\" link=\"$link\" linktext=\"$linktext\"]" . $testimonial_content . "[/av_testimonial_single]"; endwhile; else: // no posts found endif; /* Restore original Post Data */ wp_reset_postdata(); /* Restore original Post Data */ $post = $temp;
This code loops through the testimonials in the selected category and sets up the av_testimonial_single shortcode to display your CPT data instead of data entered via the builder. One thing to notice is that on the 31st thru 34th lines of this new block of code, it refers to the metadata fields that contain the company, URL, etc for the testimonial. You should replace these names with whatever you used when you set up your metadata in the plugin.
Hopefully, if you save your work and upload it, the new builder block will now display your testimonials from your Custom Post Type. There’s a small chance I may have left out some important detail and if it doesn’t work, you should feel free to ask me to look over your work.
I hope this helps. Let me know how things turn out either way, okay? Good luck!
- This reply was modified 9 years, 10 months ago by kevinmcgillivray.
January 21, 2015 at 10:35 pm #383388WOW Kevin!
Promise to update once I’m done but it seems I have here all I need and more.
thank you so much for the terrific help. I really appreciate it.January 21, 2015 at 10:59 pm #383399Happy to help. Definitely let me know how it goes. :-)
January 23, 2015 at 2:34 am #384145January 23, 2015 at 3:17 am #384161Sure thing! I love Enfold. :-)
-
AuthorPosts
- The topic ‘How are testimonials saved?’ is closed to new replies.