Wordpress
-
how to count the hit of post
Count the post hit and save it to the database
- Create a database table with fields postid, numberofcounts…
logic
onload of the post page , get its id and store it in the datatable with count
On every load the count number will be increased
Example
Let us consider the table name is tb_hit_count with field postid , and numbercount
global $wpdb; $table = $wpdb->prefix.'tb_hit_count'; $post_id = $wpdb->get_results("SELECT ncount,post_id FROM ".$table." WHERE post_id = ". $id); if(!empty($post_id)) { $newcount = $post_id[0]->ncount +1; //echo $newcount; $where = array('post_id'=>$id); $data= array('ncount'=>$newcount); //echo $cat_parent; $wpdb->update( $table, $data, $where); } else{ $datab = array('post_id' => $id, 'parent_category' => 'golf-videos', 'ncount' => 1, ); $wpdb->insert($table,$datab); }
if the post_id is already in the table , we just update the count , else we insert the post_it values in table.
-
Get the parent category of post in wordpress
get current post id
$id = get_the_ID();
echo $id;
get current post parent category slug
$category = get_the_category();
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id != 0 ) {
$category_parent = get_term( $category_parent_id, ‘category’ );
$cat_parent_slug = $category_parent->slug;
} else {
$cat_parent _slug = $category[0]->slug;
} -
Run wordpress in subfolder of domain
To run the website as blog in subfolder of domain i.e www.example.com/blog
you need to change .htaccess .
Normal .htaccess file is as follows
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
you have to change few line here i.e. folder name where the wordpress files lies in eg. blog folder
# BEGIN WordPress RewriteEngine On RewriteBase /blog RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] # END WordPress
-
Set cookies in wordpress
set cookies in wordpress to pop msg if new visitor .
in function.php write the code below :
function bloggolfoy_set_new_user_cookie() {
if ( ! is_admin() && ! isset( $_COOKIE[‘bloggolfoy_new_visitor’] ) ) {
setcookie( ‘bloggolfoy_new_visitor’, 1, time() + 3600 * 24 * 10, COOKIEPATH, COOKIE_DOMAIN, false );
}
}
add_action( ‘init’, ‘bloggolfoy_set_new_user_cookie’);now where you have coded popup : write
<?php if ( isset( $_COOKIE[‘bloggolfoy_new_visitor’] ) ) {
// echo ‘Welcome back!’;
} else {?> script code to pop <?php } ?>
-
WordPress custom plugin development sample
How to write plugin name in custom coding.
Let us consider , we are coding a plugin to show students rank in web page . lets name the plugin rankboard plugin.
- First create a folder name rankboard in wp-content / plugins folder.
2) create a page named rankboard.php. *** you can create with other name too different from folder name .
3. write the name of plugin (declare the plugin name) in rankboard.php . like
<?php
/***Plugin Name: Golflboard
*/- it is the minimum documentation to see plugin name in dashboard, along with it you can write author name , licence and manymore.
- see the resource https://developer.wordpress.org/plugins/plugin-basics/
How to add menu link of plugin to show feature and functionality in dashboard ?
to add menu link on admin / dahboard to show plugin functionality write ..
add_action(‘admin_menu’, ‘studentrank_admin’);
here add_ action (”admin_menu”) creates a menu link on dashboard .