File: /www/wwwroot/biographybirthday.com/wp-content/themes/bio/functions.php
<?php
function blog_scripts() {
wp_enqueue_style( 'blog_scripts-style', get_stylesheet_uri(), array(), '1.0.4' );
wp_enqueue_style( 'blog_scripts-customstyle', get_theme_file_uri('/css/custom.css'), array(), '1.1.3' );
// wp_enqueue_script( 'blog-main-js', get_theme_file_uri( '/js/main.js' ), array(), '1.1', true );
wp_enqueue_script( 'blog-typeahead-js', get_theme_file_uri( '/js/bootstrap-typeahead.min.js' ), array(), '1.0', true );
wp_enqueue_style( 'blog-typeahead-css', get_theme_file_uri('/css/jquery.typeahead.min.css'), array(), '1.0' );
// wp_enqueue_script( 'jquery' );
wp_enqueue_script('theia-sticky', get_template_directory_uri() . '/js/theia-sticky-sidebar.min.js',array(),false,true);
}
add_action( 'wp_enqueue_scripts', 'blog_scripts' );
function replace_core_jquery_version() {
wp_deregister_script( 'jquery' );
// Change the URL if you want to load a local copy of jQuery from your own server.
wp_register_script( 'jquery', "https://code.jquery.com/jquery-1.12.4.min.js", array(), '1.12.4' );
}
add_action( 'wp_enqueue_scripts', 'replace_core_jquery_version' );
function update_post_meta_fields( $post_ID, $post_after, $post_before ) {
$dob = get_post_meta( $post_ID, 'bio_dob', true);
if($dob != ''){
$zodiacval = zodiac($dob);
// var_dump($zodiac);
update_post_meta( $post_ID,'bio_horoscope', $zodiacval);
// $birth_month = get_post_meta( $post_ID, 'bio_birth_month', true);
// $birth_day = get_post_meta( $post_ID, 'bio_birth_day',true);
// update_post_meta( $post_ID,'bio_birth_month_day', $birth_month.'-'.$birth_day);
}
//
// $birth_month = get_post_meta( $post_ID, 'bio_birth_month', true);
// $birth_day = get_post_meta( $post_ID, 'bio_birth_day',true);
// update_post_meta( $post_ID,'bio_birth_month_day', $birth_month.'-'.$birth_day);
// update_post_meta( $post_ID,'bio_dob', $birth_year.'-'.$birth_month.'-'.$birth_day);
//
// $married_month = get_post_meta( $post_ID, 'bio_married_month', true);
// $married_day = get_post_meta( $post_ID, 'bio_married_day',true);
// update_post_meta( $post_ID,'bio_married_month_day', $married_month.'-'.$married_day);
}
add_action( 'post_updated', 'update_post_meta_fields', 10, 3);
function get_fact_popularity_loop($meta_key, $meta_value, $limit = 40){
global $wpdb;
$year_week = date("YW");
$querystr = "
SELECT wp.* ,wv.count
FROM wp_posts wp
LEFT JOIN wp_post_views wv ON wv.id = wp.ID
LEFT JOIN wp_postmeta ON wp.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key = '$meta_key'
AND wp_postmeta.meta_value LIKE '%$meta_value%'
AND wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
ORDER BY wv.count DESC
LIMIT $limit
";
// echo $querystr;exit;
// $l = new WP_Query($querystr);
// print_r($l);exit;
// $posts = $wpdb->get_results($querystr, OBJECT);
// print_r($posts);exit;
// echo $querystr;exit;
// $l = query_posts($querystr);
// print_r($l);
return $wpdb->get_results($querystr, OBJECT);
// return $l;
// echo $l->post_count;exit;
return new WP_Query($querystr);
}
function get_meta_position($views, $meta_key, $meta_value){
global $wpdb;
$year_week = date("YW");
$querystr = "
SELECT COUNT(*) as counter
FROM
wp_posts wp
LEFT JOIN wp_post_views wv ON wv.id = wp.ID
LEFT JOIN wp_postmeta ON wp.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key = '$meta_key'
AND wp_postmeta.meta_value = '$meta_value'
AND wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
AND wv.count > $views
ORDER BY wv.count DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
return (isset($pageposts[0]) ? $pageposts[0]->counter : 0) + 1;
}
// Single.php functions
function blog_get_views(){
global $wpdb;
$year_week = date("YW");
$post_id = get_the_ID();
$querystr = "
SELECT wv.count as counter
FROM wp_posts wp LEFT JOIN wp_post_views wv ON wv.id = wp.ID
WHERE wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
AND wp.ID = $post_id
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
return isset($pageposts[0]) ? $pageposts[0]->counter : 0;
}
function blog_get_popularity_by_age($views, $dob){
global $wpdb;
$year_week = date("YW");
$querystr = "
SELECT COUNT(*) as counter
FROM
wp_posts wp
LEFT JOIN wp_post_views wv ON wv.id = wp.ID
LEFT JOIN wp_postmeta ON wp.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key = 'bio_dob'
AND wp_postmeta.meta_value = '$dob'
AND wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
AND wv.count > $views
ORDER BY wv.count DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
return (isset($pageposts[0]) ? $pageposts[0]->counter : 0) + 1;
}
function blog_get_popularity_by_born_day_month($views, $dob){
global $wpdb;
$month = date('m', strtotime($dob));
$day = date('d', strtotime($dob));
$year_week = date("YW");
$month_day = $month.'-'.$day;
$querystr = "
SELECT COUNT(*) as counter
FROM
wp_posts wp
LEFT JOIN wp_post_views wv ON wv.id = wp.ID
LEFT JOIN wp_postmeta ON wp.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key = 'bio_dob'
AND wp_postmeta.meta_value LIKE '%-$month_day'
AND wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
AND wv.count > $views
ORDER BY wv.count DESC
";
// echo $querystr;exit;
$pageposts = $wpdb->get_results($querystr, OBJECT);
// print_r($pageposts);exit;
return (isset($pageposts[0]) ? $pageposts[0]->counter : 0) + 1;
}
function blog_get_most_popular_position($views){
global $wpdb;
$year_week = date("YW");
$querystr = "
SELECT COUNT(*) as counter
FROM
wp_posts wp
LEFT JOIN wp_post_views wv ON wv.id = wp.ID
WHERE wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
AND wv.count > $views
ORDER BY wv.count DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
return (isset($pageposts[0]) ? $pageposts[0]->counter : 0) + 1;
}
function blog_get_popularity_by_category($views, $category_id){
global $wpdb;
$year_week = date("YW");
$querystr = "
SELECT COUNT(*) as counter
FROM
wp_posts wp
LEFT JOIN wp_post_views wv ON wv.id = wp.ID
LEFT JOIN wp_term_relationships ON (wp.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_term_taxonomy.term_id = '$category_id'
AND wv.type = 1
AND wv.period= '$year_week'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
AND wv.count > $views
ORDER BY wv.count DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
return (isset($pageposts[0]) ? $pageposts[0]->counter : 0) + 1;
}
if (! function_exists('firstcategory_link')) {
function firstcategory_link()
{
$categories = get_the_category();
if (!empty($categories)) {
echo '<a class="cat-info-el" href="' . esc_url(get_category_link($categories[0]->term_id)) . '">' . esc_html($categories[0]->name) . '</a>';
}
}
}
if( ! function_exists('single_category_text')){
function single_category_text($post_id = null){
if($post_id != null){
$category = get_the_category($post_id);
}else{
$category = get_the_category();
}
return isset($category[0]) ? $category[0]->cat_name : '';
}
}
if( ! function_exists('single_category_link')){
function single_category_link($post_id = null){
if($post_id != null){
$category = get_the_category($post_id);
}else{
$category = get_the_category();
}
return isset($category[0]) ? get_category_link($category[0]->term_id) : '';
}
}
if( ! function_exists('single_category_id')){
function single_category_id(){
$category = get_the_category();
return isset($category[0]) ? $category[0]->term_id : '';
}
}
// REGISTER MENUS
if ( ! function_exists( 'bio_theme_setup' ) ) {
function bio_theme_setup(){
add_theme_support( 'title-tag' );
add_theme_support('post-thumbnails', array(
'post',
'page',
'custom-post-type-name',
));
// This theme uses wp_nav_menu() in two locations.
register_nav_menus(
array(
'primary' => __( 'Primary', 'biotheme' ),
'footer' => __( 'Footer Menu', 'biotheme' ),
)
);
}
}
add_action( 'after_setup_theme', 'bio_theme_setup' );
//require get_template_directory() . '/metabox/metabox.php';
//require get_template_directory() . '/cmb2/example-functions.php';
require get_template_directory() . '/metabox/metabox.php';
require get_template_directory() . '/metafield-variable.php';
add_filter('wpseo_title', 'filter_product_wpseo_title');
function filter_product_wpseo_title($title) {
$postTitle = $married = $fianceTitle = $ethnicityTitle = $nationalityTitle = $salaryTitle = $networthTitle = $heightTitle = '';
if( is_singular( 'post') ) {
global $post;
$seo_meta = get_post_meta($post->ID, '_yoast_wpseo_title', true);
if($seo_meta)return $seo_meta;
$postTitle = get_the_title();
$m = marriedStats('bio_maritalstatus');
if(isset($m) && $m != 'Not Known') {
$married = ', ' . $m;
}
$ethnicity = metaField('bio_ethnicity');
if( $ethnicity ) {
$ethnicityTitle = ", Ethnicity";
}
$salary = metaField('bio_fact_salary');
if($salary) {
$salaryTitle = ", Salary";
}
$height = metaField('bio_height');
if($height) {
$heightTitle = ", Height";
}
$nationality = metaField('bio_fact_nationality');
if($nationality) {
$nationalityTitle = ", Nationality";
}
$networth = metaField('bio_fact_net-worth');
if($networth) {
$networthTitle = ", Net Worth";
}
$gender = metaField('bio_gender');
if($gender == 'male' && $m == 'Married') {
$fianceTitle = ", Wife";
} elseif($gender == 'female' && $m == 'Married') {
$fianceTitle = ", Husband";
}
$title = $postTitle . ' -【Biography】Age' .$networthTitle. $salaryTitle. $heightTitle . $married . $fianceTitle . $ethnicityTitle . $nationalityTitle;
return $title;
}
return;
}
add_filter('wpseo_metadesc', 'my_wpseo_metadesc');
function my_wpseo_metadesc($metadesc) {
if( is_singular( 'post') ) {
global $post;
$seo_meta = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
if ($metadesc) {
// $metadesc = filter_product_wpseo_title() . ' | ' . $seo_meta;
$metadesc = $seo_meta;
} else {
// filter_product_wpseo_title($post->ID) . ' | ' .
$metadesc = strip_tags( wp_trim_words( $post->post_content, 45, '' ) );
}
}
return $metadesc;
}
if ( ! function_exists( 'newspaperss_breadcrumb' ) ) {
function newspaperss_breadcrumb( $showhome = true, $separatorclass = false ) {
// Settings
$separator = '>';
$id = 'breadcrumbs';
$class = 'breadcrumbs';
$home_title = esc_attr__('Biography Birthday','newspaperss');
// Get the query & post information
global $post,$wp_query;
$category = get_the_category();
// Build the breadcrums
echo '<ul id="' .esc_attr($id). '" class="' . esc_attr($class) . '">';
// Do not display on the homepage
if ( ! is_front_page() ) {
// Home page
echo '<li class="item-home"><a class="bread-link bread-home" href="' .esc_url( get_home_url() ). '" title="' . esc_html($home_title) . '">' . esc_html($home_title) . '</a></li>';
if ( $separatorclass ) {
echo '<li class="separator separator-home"> ' . esc_html($separator) . ' </li>';
}
if ( is_single() && ! is_attachment() ) {
// Single post (Only display the first category)
echo '<li class="item-cat item-cat-' . esc_attr($category[0]->term_id) . ' item-cat-' . esc_attr($category[0]->category_nicename) . '"><a class="bread-cat bread-cat-' . esc_attr($category[0]->term_id) . ' bread-cat-' . esc_attr($category[0]->category_nicename) . '" href="' .esc_url( get_category_link($category[0]->term_id) ). '" title="' . esc_html($category[0]->cat_name) . '">' . esc_html($category[0]->cat_name) . '</a></li>';
if ( $separatorclass ) {
echo '<li class="separator separator-' . esc_attr($category[0]->term_id) . '"> ' . esc_html($separator) . ' </li>';
}
echo '<li class="item-current item-' . esc_attr($post->ID) . '"><strong class="bread-current bread-' . esc_attr($post->ID) . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
} elseif ( is_category() ) {
// Category page
// Get the current category
$current_category = $wp_query->queried_object;
echo '<li class="item-current item-cat-' . $current_category->term_id . ' item-cat-' . $current_category->category_nicename . '"><strong class="bread-current bread-cat-' . $current_category->term_id . ' bread-cat-' . $current_category->category_nicename . '">' . $current_category->cat_name . '</strong></li>';
} elseif ( is_tax() ) {
// Tax archive page
$queried_object = get_queried_object();
$name = $queried_object->name;
$slug = $queried_object->slug;
$tax = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$parent = $queried_object->parent;
if( $parent ) {
// Loop through all term ancestors
while ( $parent ) {
$parent_term = get_term($parent, $tax);
// The output will be reversed, so separator goes first
if ( $separatorclass ) {
$parents[] = '<li class="separator separator-' . $parent . '"> ' . esc_html($separator) . ' </li>';
}
$parents[] = '<li class="item-parent item-parent-' . $parent . '"><a class="bread-parent bread-parent-' . $parent . '" href="' .esc_url( get_term_link($parent) ). '" title="' . $parent_term->name . '">' . $parent_term->name . '</a></li>';
$parent = $parent_term->parent;
}
echo implode( array_reverse( $parents ) );
}
echo '<li class="item-current item-tax-' . $term_id . ' item-tax-' . $slug . '">' .esc_html( $name ). '</li>';
} elseif ( is_page() ) {
// Standard page
if ( $post->post_parent ) {
// If child page, get parents
$anc = get_post_ancestors( $post->ID );
// Get parents in the right order
$anc = array_reverse( $anc );
// Parent page loop
$parents = '';
foreach ( $anc as $ancestor ) {
$parents .= '<li class="item-parent item-parent-' . esc_attr($ancestor) . '"><a class="bread-parent bread-parent-' . $ancestor . '" href="' .esc_url( get_permalink($ancestor) ). '" title="' . get_the_title($ancestor) . '">' . get_the_title($ancestor) . '</a></li>';
if ( $separatorclass ) {
$parents .= '<li class="separator separator-' . esc_attr($ancestor) . '"> ' . esc_html($separator) . ' </li>';
}
}
// Display parent pages
echo $parents;
// Current page
echo '<li class="current item-' . $post->ID . '">' . esc_html(get_the_title()) . '</li>';
} else {
// Just display current page if not parents
echo '<li class="current item-' . $post->ID . '"> ' . esc_html(get_the_title()) . '</li>';
}
} elseif ( is_tag() ) {
// Tag page
// Get tag information
$term_id = get_query_var('tag_id');
$taxonomy = 'post_tag';
$args = 'include=' . $term_id;
$terms = get_terms($taxonomy, $args);
// Display the tag name
echo '<li class="current item-tag-' . $terms[0]->term_id . ' item-tag-' . $terms[0]->slug . '">' . $terms[0]->name . '</li>';
} elseif ( is_day() ) {
// Day archive
// Year link
echo '<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="' .esc_url( get_year_link(get_the_time('Y'))) . '" title="' . get_the_time('Y') . '">' . esc_html( get_the_time( __( 'Y', 'newspaperss' ) ) ) .'</a></li>';
if ( $separatorclass ) {
echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . esc_html($separator) . ' </li>';
}
// Month link
echo '<li class="item-month item-month-' . get_the_time('m') . '"><a class="bread-month bread-month-' . get_the_time('m') . '" href="' . esc_url(get_month_link(get_the_time('Y'), get_the_time('m'))) . '" title="' . get_the_time('M') . '">' . esc_html( get_the_time( __( 'M', 'newspaperss' ) ) ) . '</a></li>';
if ( $separatorclass ) {
echo '<li class="separator separator-' . get_the_time('m') . '"> ' . esc_html($separator) . ' </li>';
}
// Day display
echo '<li class="current item-' . get_the_time('j') . '">' . get_the_time('jS') . ' ' . get_the_time('M') . '</li>';
} elseif ( is_month() ) {
// Month Archive
// Year link
echo '<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" href="' . esc_url(get_year_link(get_the_time('Y')) ). '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . '</a></li>';
if ( $separatorclass ) {
echo '<li class="separator separator-' . get_the_time('Y') . '"> ' . esc_html($separator) . ' </li>';
}
// Month display
echo '<li class="item-month item-month-' . get_the_time('m') . '">' . get_the_time('M') . ' </li>';
} elseif ( is_year() ) {
// Display year archive
echo '<li class="current item-current-' . get_the_time('Y') . '">' . get_the_time('Y') . '</li>';
} elseif ( is_author() ) {
// Auhor archive
// Get the author information
global $author;
$userdata = get_userdata($author);
// Display author name
echo '<li class="current item-current-' . $userdata->user_nicename . '">'. __('Author :', 'newspaperss' ) . $userdata->display_name . '</li>';
} elseif ( get_query_var('paged') ) {
// Paginated archives
echo '<li class="current item-current-' . get_query_var('paged') . '">' . __('Page', 'newspaperss' ) . ' ' . get_query_var('paged') . '</li>';
} elseif ( is_search() ) {
// Search results page
echo '<li class="current item-current-search">'. __('Search results for: ', 'newspaperss' ) . get_search_query() . '</li>';
} elseif ( is_post_type_archive() ) {
// Custom Post Type Archive Page
echo '<li class="current">' . post_type_archive_title( '', false ) . '</li>';
} elseif ( is_404() ) {
// 404 page
echo '<li>'. __('Error 404','newspaperss').'</li>';
} // End if().
} else {
if ( $showhome ) {
echo '<li class="item-home current">' . esc_html($home_title) . '</li>';
}
} // End if().
echo '</ul>';
}
} // End if().
function bio_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">Posted on %2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = 'Updated On %4$s';
// $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s"><span class="posted-on"> / Last Modified %4$s</span></time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
esc_html_x( '%s', 'post date', 'mbiography' ), $time_string );
$byline = sprintf(
esc_html_x( '%s ', 'post author', 'mbiography' ),
'<a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '"><span class="author vcard">'.get_the_author_meta( 'display_name' ).'</span></a>'
);
echo '<span class="posted-on">' . $posted_on . '</span>'; // WPCS: XSS OK.
echo '<span class="byline posted-on"> | <i class="fa fa-user"></i> ' . $byline . '</span>';
// echo '<span class="posted-on">' . $posted_on . '</span>';
}
// define the function to be fired for logged in users
function bio_random() {
$loop = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'limit' => 1,
'orderby' => 'rand'
) );
$post = array();
while ( $loop->have_posts() ) : $loop->the_post();
$post = array(
'name' => get_the_title(),
'category' => single_category_text(),
'href' => get_the_permalink()
// 'image' => get_the_post_thumbnail_url()
);
endwhile;
wp_reset_query();
echo json_encode($post); exit;
}
// define the function to be fired for logged in users
function bio_autocomplete() {
$loop = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
's' => $_GET['q'],
'limit' => 5
) );
$posts = array();
while ( $loop->have_posts() ) : $loop->the_post();
$posts[] = array(
'name' => get_the_title(),
'category' => single_category_text(),
'href' => get_the_permalink()
// 'image' => get_the_post_thumbnail_url()
);
endwhile;
wp_reset_query();
echo json_encode($posts); exit;
}
// define the function to be fired for logged in users
function bio_boost() {
$like_count = get_post_meta($_REQUEST["post_id"], "boost", true);
// echo $like_count;exit;
$like_count = ($like_count != '') ? $like_count : 0 ;
$new_like_count = $like_count + 1;
$like = update_post_meta($_REQUEST["post_id"], "boost", $new_like_count);
$result['like_count'] = $new_like_count;
// Check if action was fired via Ajax call. If yes, JS code will be triggered, else the user is redirected to the post page
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = json_encode($result);
echo $result;
}
else {
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
// don't forget to end your scripts with a die() function - very important
die();
}
function add_ajax_actions() {
add_action("wp_ajax_bio_random", "bio_random");
add_action("wp_ajax_nopriv_bio_random", "bio_random");
add_action("wp_ajax_bio_autocomplete", "bio_autocomplete");
add_action("wp_ajax_nopriv_bio_autocomplete", "bio_autocomplete");
add_action("wp_ajax_bio_boost", "bio_boost");
add_action("wp_ajax_nopriv_bio_boost", "bio_boost");
}
add_action( 'admin_init', 'add_ajax_actions' );
function hm_get_template_part( $file, $template_args = array(), $cache_args = array() ) {
$template_args = wp_parse_args( $template_args );
$cache_args = wp_parse_args( $cache_args );
if ( $cache_args ) {
foreach ( $template_args as $key => $value ) {
if ( is_scalar( $value ) || is_array( $value ) ) {
$cache_args[$key] = $value;
} else if ( is_object( $value ) && method_exists( $value, 'get_id' ) ) {
$cache_args[$key] = call_user_method( 'get_id', $value );
}
}
if ( ( $cache = wp_cache_get( $file, serialize( $cache_args ) ) ) !== false ) {
if ( ! empty( $template_args['return'] ) )
return $cache;
echo $cache;
return;
}
}
$file_handle = $file;
do_action( 'start_operation', 'hm_template_part::' . $file_handle );
if ( file_exists( get_stylesheet_directory() . '/' . $file . '.php' ) )
$file = get_stylesheet_directory() . '/' . $file . '.php';
elseif ( file_exists( get_template_directory() . '/' . $file . '.php' ) )
$file = get_template_directory() . '/' . $file . '.php';
ob_start();
$return = require( $file );
$data = ob_get_clean();
do_action( 'end_operation', 'hm_template_part::' . $file_handle );
if ( $cache_args ) {
wp_cache_set( $file, $data, serialize( $cache_args ), 3600 );
}
if ( ! empty( $template_args['return'] ) )
if ( $return === false )
return false;
else
return $data;
echo $data;
}
//function custom_rewrite_rule() {
// add_rewrite_rule('properties/([^/]+)/?$','index.php?memberspage=$matches[1]&event_id=$matches[2]','top');
//}
//add_action('init', 'custom_rewrite_rule', 10, 0);
// URL REWRITES
//function custom_rewrite_basic() {
// add_rewrite_rule('^horoscope/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
//}
//add_action('init', 'custom_rewrite_basic');
//add_action('init', function() {
// add_rewrite_rule( '^properties/([0-9]+)/?',
// 'index.php?pagename=properties&property_id=$matches[1]',
// 'top' );
//}, 10, 0);
//
//add_action('init', function() {
// add_rewrite_tag( '%property_id%', '([^&]+)' );
//}, 10, 0);
// FOR TESTING PURPOSE --- REMOVE IN PRODUCTION
//add_filter('init','flushRules'); function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); }
//add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
//function addnew_query_vars($vars)
//{
// $vars[] = 'horoscope'; // c is the name of variable you want to add
// return $vars;
//}
function custom_rewrite_basic()
{
// LOCAL
// add_rewrite_rule('^all/([^/]*)/?','index.php?page_id=56&q=$matches[1]','top');
// add_rewrite_rule('^horoscope/([^/]*)/?','index.php?page_id=56&q=$matches[1]','top');
// add_rewrite_rule('^birthday/([^/]*)/?','index.php?page_id=60&q=$matches[1]','top');
// add_rewrite_rule('^age/([^/]*)/?','index.php?page_id=62&q=$matches[1]','top');
// add_rewrite_rule('^birthplace/([^/]*)/?','index.php?page_id=64&q=$matches[1]','top');
// add_rewrite_rule('^country/([^/]*)/?','index.php?page_id=66&q=$matches[1]','top');
// add_rewrite_rule('^networth/([^/]*)/?','index.php?page_id=68&q=$matches[1]','top');
// add_rewrite_rule('^firstname/([^/]*)/?','index.php?page_id=70&q=$matches[1]','top');
// SERVER
add_rewrite_rule('^all/([^/]*)/?','index.php?page_id=8&q=$matches[1]','top');
add_rewrite_rule('^horoscope/([^/]*)/?','index.php?page_id=40&q=$matches[1]','top');
add_rewrite_rule('^birthday/([^/]*)/?','index.php?page_id=14&q=$matches[1]','top');
add_rewrite_rule('^age/([^/]*)/?','index.php?page_id=10&q=$matches[1]','top');
add_rewrite_rule('^birthplace/([^/]*)/?','index.php?page_id=12&q=$matches[1]','top');
add_rewrite_rule('^city/([^/]*)/?','index.php?page_id=16&q=$matches[1]','top');
add_rewrite_rule('^country/([^/]*)/?','index.php?page_id=18&q=$matches[1]','top');
add_rewrite_rule('^firstname/([^/]*)/?','index.php?page_id=22&q=$matches[1]','top');
add_rewrite_rule('^lastname/([^/]*)/?','index.php?page_id=24&q=$matches[1]','top');
add_rewrite_rule('^networth/([^/]*)/?','index.php?page_id=57&q=$matches[1]','top');
}
add_action('init', 'custom_rewrite_basic');
//function custom_rewrite_tag() {
// add_rewrite_tag('%horoscope%', '([^&]+)');
// add_rewrite_tag('%networth%', '([^&]+)');
//}
//add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_tag() {
// add_rewrite_tag('%food%', '([^&]+)');
// add_rewrite_tag('%variety%', '([^&]+)');
add_rewrite_tag('%q%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
// ADD FEATURED IMAGE AT ADMIN SECTION
add_filter('manage_posts_columns', 'add_img_column');
add_filter('manage_posts_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns['ryu_post_content_length'] = __( 'Length' );
$columns['img'] = 'Featured Image';
$columns['dob'] = 'DOB';
$columns['horoscope'] = 'Horoscope';
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'img' ) {
// echo get_the_post_thumbnail($post_id, 'thumbnail');
the_post_thumbnail( array(100,100));
}
if( $column_name == 'dob' ) {
echo get_post_meta( $post_id, 'bio_dob', true);
}
if( $column_name == 'horoscope' ) {
echo get_post_meta( $post_id, 'bio_horoscope', true);
}
if ( $column_name == 'ryu_post_content_length'){
echo count_words(get_post( $post_id )->post_content);
// echo ' / ';
// echo str_word_count( strip_tags( get_post( $post_id )->post_content ) );
}
return $column_name;
}
if(!function_exists('count_words')){
function count_words($str)
{
$str = preg_replace("/[^A-Za-z0-9 ]/","",strip_tags(
str_replace('<',' <',
str_replace('>','> ',
str_replace(array("\n","\r","\t"),' ',$str)))));
while(substr_count($str,' ')>0)
{
$str = str_replace(' ',' ',$str);
}
return substr_count(trim($str,' '),' ')+1;
}
}
// add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;
if( current_user_can('administrator') ) {
wp_add_dashboard_widget('my_trending_widget_24_hours', 'Today - Trending', 'my_trending_last_24_day_dashboard_widget');
wp_add_dashboard_widget('my_trending_widget_7_days', 'Last 7 Days - Trending', 'my_trending_7days_dashboard_widget');
}
// wp_add_dashboard_widget('my_alltime_widget', 'All Time Top Views', 'my_alltime_dashboard_widget');
}
function my_trending_last_24_day_dashboard_widget() {
global $wpdb;
$year_month = date("Ymd");
// debug($year_month);exit;
$querystr = "
SELECT wp.* ,wv.count
FROM wp_posts wp LEFT JOIN wp_post_views wv ON wv.id = wp.ID
WHERE wv.type = 0
AND wv.period= '$year_month'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
ORDER BY wv.count DESC
LIMIT 10
";
$query = new WP_Query($querystr);
$pageposts = $wpdb->get_results($querystr, OBJECT);
echo my_dashboard_table($pageposts);
}
function my_trending_7days_dashboard_widget() {
global $wpdb;
$year_month = date("YW");
$querystr = "
SELECT wp.* ,wv.count
FROM wp_posts wp LEFT JOIN wp_post_views wv ON wv.id = wp.ID
WHERE wv.type = 1
AND wv.period= '$year_month'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
ORDER BY wv.count DESC
LIMIT 10
";
$query = new WP_Query($querystr);
$pageposts = $wpdb->get_results($querystr, OBJECT);
echo my_dashboard_table($pageposts);
}
function my_alltime_dashboard_widget() {
global $wpdb;
$year_month = date("YW");
$querystr = "
SELECT wp.* ,wv.count
FROM wp_posts wp LEFT JOIN wp_post_views wv ON wv.id = wp.ID
WHERE wv.type = 4
AND wv.period= 'total'
AND wp.post_status = 'publish'
AND wp.post_type = 'post'
AND wp.post_date < NOW()
ORDER BY wv.count DESC
LIMIT 10
";
$query = new WP_Query($querystr);
$pageposts = $wpdb->get_results($querystr, OBJECT);
echo my_dashboard_table($pageposts);
}
function my_dashboard_table($pageposts){
$table = '<table class="wp-list-table widefat striped posts">';
$table .= '<tr><th>Post Title</th><th>Views</th><th>Published Date</th><th>Open in Google</th></tr>';
foreach ( $pageposts as $recent_post ){
$post_title = get_the_title( $recent_post->ID );
$table .= '<tr><td style="display: table-cell;"><a href="'.get_permalink($recent_post->ID).'" target="_blank"> '.$post_title.'</a></td>';
$table .= '<td style="display: table-cell;">'.number_format($recent_post->count).'</td>';
$table .= '<td style="display: table-cell;">'.date('F j, Y',strtotime($recent_post->post_date)).'</td>';
$table .= '<td style="display: table-cell;"><a href="https://google.com/search?q='.urlencode($post_title).'" target="_blank">Open</a></td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
add_action( 'load-edit.php', 'wpse14230_load_edit' );
function wpse14230_load_edit()
{
add_action( 'request', 'wpse14230_request' );
}
function wpse14230_request( $query_vars )
{
if ( ! current_user_can( $GLOBALS['post_type_object']->cap->edit_others_posts ) ) {
$query_vars['author'] = get_current_user_id();
}
return $query_vars;
}
add_filter("views_edit-post", 'custom_editor_counts', 10, 1);
function custom_editor_counts($views) {
if ( ! current_user_can( $GLOBALS['post_type_object']->cap->edit_others_posts ) ) {
$iCurrentUserID = get_current_user_id();
if ($iCurrentUserID !== 0) {
global $wpdb;
foreach ($views as $index => $view) {
if (in_array($index, array('all','mine'))){
continue;
}
// if (in_array($index, array('all', 'publish','mine')))
$viewValue = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type='post' AND post_author=$iCurrentUserID AND post_status='$index'");
$views[$index] = preg_replace('/\(.+\)/U', '(' . $viewValue . ')', $views[$index]);
}
// unset($views['all']);
// unset($views['publish']);
}
}
return $views;
}
//Let Contributor Role to Upload Media
if ( current_user_can('contributor') && !current_user_can('upload_files') ){
add_action('admin_init', 'allow_contributor_uploads');
}
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}