Author Archives: kaixersoft
How to deal with Facebook OG tags on Single posts and Category posts
How to make an AJAX caller handler for your AJAX stuff inside a post / page
I usually create a file that handles all AJAX request, call it ajaxcall.php, place it on your theme folder
//Pull WordPress DB account
require_once( "../../../wp-config.php" );
require_once( "../../../wp-load.php" );
define('DEBUG_MODE',true); //show_errors(); } else { $wpdb->hide_errors(); }
//See functions.php for all the functions used in this file
$ac = $_REQUEST['ac'];
$ret = json_encode(array('status'=>'empty'));
switch ($ac) {
case 'pt': //Post a tip
$fbid = $_REQUEST['uid'];
$fbname = $_REQUEST['fn'];
$postdesc = $_REQUEST['pd'];
$postcat = $_REQUEST['pc'];
$ret = htmlspecialchars(json_encode( post_a_tip($fbid,$fbname,$postdesc,$postcat) ), ENT_NOQUOTES);
break;
case 'ru': //Register a user
$uid = $_REQUEST['uid'];
$fname = $_REQUEST['fn'];
$lname = $_REQUEST['ln'];
$email = $_REQUEST['em'];
$nric = $_REQUEST['ic'];
$contact = $_REQUEST['mo'];
$dob = $_REQUEST['db'];
$contestMonth = $_REQUEST['cm'];
$ret = htmlspecialchars(json_encode( register_user( $fname,$lname, $nric, $dob, $email, $contact, $contestMonth , $uid ) ), ENT_NOQUOTES);
break;
}
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");
echo $ret;
And then, here’s how you call it, inside your template
var _url = "/ajaxcall.php";
var _args = {
ac: 'ru',
uid: "",
fn : fname,
ln : lname,
db: dob,
em: email,
ic : nric,
mo : contact,
cm: contestMonth
}
$.getJSON(_url,_args,function(data) {
participationFeed();
var _html = '
';
$('div.contest_right_box').html(_html);
});
How create custom function for your custom tables
Here’s a couple of code that I use to pull data from a custom table, this one utilizes wordpress own DAL. ($wpdb)
You can just add these function on function.php, then use it anywhere.
/*
Post a user tip to specific category
*/
function post_a_tip($uid,$uname,$postdsc,$postcat) {
global $wpdb;
$sql =$wpdb->prepare("INSERT INTO tips(fbid,fbname,post_desc,post_cat,post_date) VALUES(%s , %s , %s , %d , %s)",
$uid,$uname,$postdsc,$postcat,date('Y-m-d H:i:s'));
$wpdb->query($sql);
}
function get_user_tips_by_category($catId) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT * FROM tips WHERE post_cat = %s ORDER BY post_date DESC", $catId);
$rs = $wpdb->get_results( $sql , ARRAY_A );
if (count($rs)) {
return $rs;
} else {
return array();
}
}
How to Pull First and Second Images of a post
function pull_first_image_from_post() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "";//"/images/default.jpg";
}
return $first_img;
}
function pull_second_image_from_post() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [1];
// no image found display default image instead
if(empty($first_img)){
$first_img = "";//"/images/default.jpg";
}
return $first_img;
}
Pull Date Archives by Category
Here’s a very nice wordpress plugin, to help you pull date archives of specific category.
See URL : http://kwebble.com/blog/2007_08_15/archives_for_a_category
From a Page template, pull monthly archive :
<?php //————- Monthly Archives ———————– ?>
<ul>
<?php wp_get_archives(‘type=monthly&limit=12&cat=4′); ?>
</ul>
How to pull posts of a specific category inside the loop
if ( in_category( array(‘throughthelens-video’) ) ) {
$catId = get_category_id(‘throughthelens-video’);
query_posts(‘cat=’.$catId);
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part(‘content’,'carousel_video’);
endwhile; endif;
wp_reset_query();
} //end if ?>
Here’s what inside the template part :
<?php the_title(); ?><br />
<?php
$postId = get_the_ID();
$vid = get_post_custom_values(‘videoid’, $postId); ?>
<a href=”<?php echo get_permalink($postId);?>”>
<img src=”http://img.youtube.com/vi/<?php echo $vid[0];?>/2.jpg” />
</a>
Here’s what’s inside the function.php
function get_category_id($slug) {
$category = get_category_by_slug($slug);
return $category->term_id;
}
Display Category and Sub Category posts
Remove First Image of the post
Methods :
1. echo preg_replace("/\< *[img][^\>]*[.]*\>/i","",get_the_content(),1);
2. put the following add_filter before the_content in any file you want to remove image
add_filter('the_content', 'remove_image_content');
the_content();
3. put filter on functions.php to remove first image from all the_content
function remove_image_content($content) {
return preg_replace("/\< *[img][^\>]*[.]*\>/i","",$content,1);}add_filter('the_content', 'remove_image_content');
How to create password protected folder in linux
htpasswd -c /var/www/folder_name/.htpasswd <username>
Should be prompted with new password

