Skip to content

Instantly share code, notes, and snippets.

@imvision
Created January 21, 2014 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imvision/8540090 to your computer and use it in GitHub Desktop.
Save imvision/8540090 to your computer and use it in GitHub Desktop.
Wordpress Plugin - User Certifications - Adds user certifications fields using template tags anywhere. User certifications then can be rendered anywhere using template tags.
<?php
/*
Plugin Name: User Certifications
Plugin URI:
Description: Adds User Certifications fields in user profile. User certifications can be rendered anywhere using template tags.
Version: 1.0
Author: Ali Roshan
Author Email: aliroshan@live.com
License:
Copyright 2014 Ali Roshan (aliroshan@live.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class UserCertifications {
/*--------------------------------------------*
* Constants
*--------------------------------------------*/
const name = 'User Certifications';
const slug = 'user_certifications';
/**
* Constructor
*/
function __construct() {
//Hook up to the init action
add_action( 'init', array( &$this, 'init_user_certifications' ) );
}
/**
* Runs when the plugin is initialized
*/
function init_user_certifications() {
// Load JavaScript and stylesheets
$this->register_scripts_and_styles();
// Register the shortcode [user_certifications]
add_shortcode( 'user_certifications', array( &$this, 'load_cert_list' ) );
if ( is_admin() ) {
//this will run when in the WordPress admin
} else {
//this will run when on the frontend
}
add_action( 'wp_ajax_savecert', array( &$this, 'save_certification' ) );
add_action( 'wp_ajax_loadcert', array( &$this, 'load_certification' ) );
add_action( 'wp_ajax_delcert', array( &$this, 'del_certification' ) );
add_action( 'user-certifications-render', array( &$this, 'render_cert_form' ) );
add_action( 'bp_after_profile_loop_content', array( &$this, 'load_cert_list' ) );
add_filter( 'your_filter_here', array( &$this, 'filter_callback_method_name' ) );
}
function render_cert_form() {
?>
<div id="cert-all"><?php $this->load_certification();?></div>
<div class="fieldset cert-fs">
<label>Certificate</label>
<input type="text" name="cert-title" id="cert-title">
</div>
<div class="clear"></div>
<div class="cert-box">
<div class="fieldset cert-fs">
<label>From</label>
<input type="text" name="cert-from" id="cert-from">
</div>
<div class="clear"></div>
<div class="fieldset cert-fs">
<label>Year</label>
<input type="text" name="cert-year" id="cert-year">
</div>
<div class="clear"></div>
<div class="fieldset cert-fs">
<label>&nbsp;</label><button id="save_cert">Add</button>
</div>
</div>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$("#save_cert").click(function() {
if($('#cert-title').val()=="") {
return false;
}
var cert_data = {
"from" : $("#cert-from").val(),
"title" : $("#cert-title").val(),
"year" : $("#cert-year").val()
};
$.ajax({
type: "post",url: ajaxurl,data: { action: 'savecert', certs: cert_data},
beforeSend: function() {
$("#cert-from").val(''); $("#cert-title").val(''); $("#cert-year").val('');
},
success: function(html){ //so, if data is retrieved, store it in html
$("#cert-all").html(html); //fadeIn the html inside helloworld div
// $("#helloworld").fadeIn("fast"); //animation
}
}); //close $.ajax(
return false;
});
$("#cert-all").on("click", "a", function() {
var meta_id = $(this).data("umeta");
delete_cert(meta_id);
$(this).parent().parent().remove();
});
// load_cert();
});
})(jQuery);
function load_cert() {
jQuery.ajax({
type: "post",url: ajaxurl,data: { action: 'loadcert'},
beforeSend: function() {}, //fadeIn loading just when link is clicked
success: function(html){ //so, if data is retrieved, store it in html
jQuery("#cert-all").html(html); //fadeIn the html inside helloworld div
// jQuery("#helloworld").fadeIn("fast"); //animation
}
});
}
function delete_cert(meta_id) {
jQuery.ajax({
type: "post",url: ajaxurl,data: { action: 'delcert', umeta_id: meta_id},
beforeSend: function() {}, //fadeIn loading just when link is clicked
success: function(html){}
});
}
</script>
<?php
}
function save_certification() {
global $wpdb;
$user_ID = get_current_user_id();
$certs = $_POST['certs'];
$certs['verified'] = 0;
add_user_meta( $user_ID, "certifications", $certs, false );
$this->load_certification();
die();
}
function load_cert_list($user_ID) {
$certs = $this->dbLoadCert($user_ID);
?>
<table class="profile-fields cert-profile-view">
<tr><td><h4>Certifications</h4></td></tr>
<?php $counter = 0; foreach ($certs as $cert): $c = unserialize($cert[1]); ?>
<tr class="">
<td class="datas">
<b><?php echo $c['title'];?></b>
<?php echo ($c['from']=="") ? "":"from <b>{$c['from']}</b>" ;?>
<?php echo ($c['year']=="") ? "":"({$c['year']})" ;?>
</td>
</tr>
<?php endforeach;?>
</table>
<?php
}
function load_certification() {
$user_ID = get_current_user_id();
$certs = $this->dbLoadCert($user_ID);
?>
<table class="cert-text">
<?php $counter = 0; foreach ($certs as $cert): $c = unserialize($cert[1]); ?>
<tr class="<?php echo ($counter%2==1) ? 'grey-row':'' ;?>">
<td class="left-text"><?php echo ++$counter;?>.</td>
<td class="left-text">
<b><?php echo $c['title'];?></b>
<?php echo ($c['from']=="") ? "":"from <b>{$c['from']}</b>" ;?>
<?php echo ($c['year']=="") ? "":"({$c['year']})" ;?>
</td>
<td class="left-text"><?php echo ($c['verified']==1) ? "Verified":"Unverfied" ;?></td>
<td><a data-umeta="<?php echo $cert[0] ;?>" href="javascript: void(0);">x</a></td>
</tr>
<?php endforeach;?>
</table>
<?php
}
function dbLoadCert($user_ID) {
global $wpdb;
$certs = $wpdb->get_results( "SELECT umeta_id, meta_value FROM $wpdb->usermeta WHERE user_id='$user_ID' AND meta_key='certifications' ORDER by umeta_id ASC", ARRAY_N );
return $certs;
}
function del_certification() {
global $wpdb;
$user_ID = get_current_user_id();
$umeta_id = esc_sql($_POST['umeta_id']);
$wpdb->delete( $wpdb->usermeta, array( 'umeta_id' => $umeta_id, 'user_id' => $user_ID ) );
}
function filter_callback_method_name() {
// TODO define your filter method here
}
function render_shortcode($atts) {
// Extract the attributes
extract(shortcode_atts(array(
'attr1' => 'foo', //foo is a default value
'attr2' => 'bar'
), $atts));
// you can now access the attribute values using $attr1 and $attr2
}
/**
* Registers and enqueues stylesheets for the administration panel and the
* public facing site.
*/
private function register_scripts_and_styles() {
if ( is_admin() ) {
// $this->load_file( self::slug . '-admin-script', '/js/admin.js', true );
// $this->load_file( self::slug . '-admin-style', '/css/admin.css' );
} else {
// $this->load_file( self::slug . '-script', '/js/widget.js', true );
$this->load_file( self::slug . '-style', '/css/default.css' );
} // end if/else
} // end register_scripts_and_styles
/**
* Helper function for registering and enqueueing scripts and styles.
*
* @name The ID to register with WordPress
* @file_path The path to the actual file
* @is_script Optional argument for if the incoming file_path is a JavaScript source file.
*/
private function load_file( $name, $file_path, $is_script = false ) {
$url = plugins_url($file_path, __FILE__);
$file = plugin_dir_path(__FILE__) . $file_path;
if( file_exists( $file ) ) {
if( $is_script ) {
wp_register_script( $name, $url, array('jquery') ); //depends on jquery
wp_enqueue_script( $name );
} else {
wp_register_style( $name, $url );
wp_enqueue_style( $name );
} // end if
} // end if
} // end load_file
} // end class
new UserCertifications();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment