Video Reviews for Woocommerce & Site Comments, 2 Easy Solutions.

Have you ever wanted to include video reviews to your Woocommerce store? Or allow users to post videos with comments, which works same as reviews. Well now we will see how to achieve this with two ways.

First Solution is to have users to post comment with necessary html code for embed the video.
To make this work our default user role need to have necessary (unfiltered_html) permission. Dont be scared its easy to do with 2 lines of code. Make sure you selected correct user role, by default its ‘subscriber’, but may change with woocommerce as it creates role ‘customer’.

$role = get_role( 'subscriber' );
$role->add_cap( 'unfiltered_html' );

Now this allowed users to post unfiltered html to our website. Okay as you might already guessed it can result in some html code be posted. If you manually review each comment that should not be big problem.
And user needs to know how to acquire html for video embed to be posted in comments.
This would look something like this: <iframe width=”560″ height=”315″ src=”https://www.youtube.com/embed/7V-fIGMDsmE” frameborder=”0″ allowfullscreen></iframe>.

However that’s not it, I have promised 2 solution and here is the second and better solution.

We create filter that hooks in to the comment and checks for embeds.
Everything is done with build-in wordpress functionality, without giving permissions or html. All we need is link to the video, or other supported build in WordPress embed service.

$wp_embed = new WP_Embed;
add_filter( 'comment_text', array( $wp_embed, 'autoembed' ), 8 );

Modification of plugin’s hooks and actions regardless of load order.

To have access to all other plugins hooks, we need run our script when wordpress done loading plugin.
Use following workaround.

Place your hooks in to the function, and call it when plugins_loaded.

add_action('plugins_loaded','modify_actions_custom');

function modify_actions_custom(){

}

Reference:
WordPress Codex with loading structure.

WordPress – Creating Site specific plugin

Why create wordpress modification as a plugin? That allow us to keep theme files (function.php) clean of setup or plugin specific modifications. With that said lets begin.

Create a folder in the plugin directory of your wordpress website.
Create index.php file with following content as template for plugin:

<?php
/*
Plugin Name: 
Description: 
*/

/* Below this line content of the plugin */

?>

 

As you can see its pretty simple. To get started, as it always with wordpress. However there are some difference from how plugin operates compared with theme.

  • Include paths
    • To include any of your file in to the plugin or a theme we use function that determines its location. That includes JS, CSS or images. In theme location is passed with get_template_directory_uri(),

      In theme they are passed with get_template_directory_uri(),
      In child theme its get_stylesheet_directory_uri(),
      while for plugins its: plugin_dir_url( __FILE__ ).
      Followed by the file name with extension.

  • Execution order
    • Plugins are loaded before themes, therefore some of the hooks may not be available as plugins themselves are executed in wordpress determined order. So your site specific plugin maybe executed before required hooks or action becomes available. Little snippet on how to solve it.

Keep this things in mind when moving the code from function.php to your new site specific plugin. If you have encountered any other problems with migration to plugin from theme, feel free to comment.

WordPress – Sensei plugin auto grading of ALL question types

Sensei is a Learning Platform Plugin, with build-in Woocommerce integration. It is also developed by Woothemes, company which was recently purchased by Automatic (Owner of WordPress) to simplify building of e-commerce websites.
Sensei is premium plugin, means you need to buy it, but its also GPL, and has its development open on https://github.com/woothemes/sensei.

By The Default Sensei Supports only multiple choice, fill in the blank, and true and false, auto-grading. However in the recent project I have worked on I have encountered need for auto-grading the questions of the single line, multi-line and file upload types. So in order to do it we need to make couple of things.

I recommend create Site specific plugin for this modification.

First allow in administrator menu for this question types to be gradable automatically. That achieved with simple jquery.

Create the JS file, with this content:
and save it as sensei/sensei-admin-scripts.js

jQuery(document).ready(function($){
	$( 'input#quiz_grade_type' ).removeAttr( 'disabled' );
	$( 'input#quiz_grade_type' ).closest('p.form-field').removeClass( 'disabled' );
	$( 'input#quiz_grade_type_disabled' ).val( 'enabled' );
});

Then we create an index.php file with our plugin content


<?php
add_action( 'admin_enqueue_scripts', 'load_custom_sensei_admin_script');

add_filter( 'sensei_autogradable_question_types', function($supported){return array_merge( $supported, array('multi-line','single-line','file-upload'));});

add_filter( 'sensei_grade_question_auto', 'sensei_autograde_question_score', 10, 4 );

function load_custom_sensei_admin_script() {
	wp_enqueue_script( 'custom-sensei-admin-script', plugin_dir_url( __FILE__ ).'sensei/sensei-admin-scripts.js');
}

?>

Update: 8/19/15
In future 2.0 Release it is planned to have ability to set questions without grading, enhancement which I have purposed back in july (https://github.com/woothemes/sensei/issues/962).

So this logic will be changed. But for now this solution works perfectly.