Ad 468 X 60

Thursday, 18 June 2015

Change Post Page count ( All, Published, Mine, Draft, Pending) in wordpress

This script is very useful when you creating directory type website and you have to show post and page count by author in admin and user profile interface where he can manage his post and page add and edit.

I also faced this problem and i google to find this type of scrpit many more time and finaly i fond       this script from .You can find more detail from this link.
you have to copy and paste this code in functions.php

foreach( array( 'edit-post', 'edit-page', 'edit-movie', 'upload' ) as $hook )
    add_filter( "views_$hook" , 'wpse_30331_custom_view_count', 10, 1);

function wpse_30331_custom_view_count( $views ) 
    global $current_screen;
    switch( $current_screen->id ) 
        case 'edit-post':
            $views = wpse_30331_manipulate_views( 'post', $views );
        case 'edit-page':
            $views = wpse_30331_manipulate_views( 'page', $views );
        /*case 'edit-movie':
            $views = wpse_30331_manipulate_views( 'movie', $views );
        case 'upload':
            $views = wpse_30331_manipulate_views( 'attachment', $views );
            break; */
    return $views;

function wpse_30331_manipulate_views( $what, $views )
    global $user_ID, $wpdb;

     /*if ( !current_user_can('artist') ) 
        return $views; */
     * This needs refining, and maybe a better method
     * e.g. Attachments have completely different counts 

    $total = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending') AND (post_author = '$user_ID'  AND post_type = '$what' ) ");
    $publish = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = '$user_ID' AND post_type = '$what' ");
    $draft = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = '$user_ID' AND post_type = '$what' ");
    $pending = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'pending' AND post_author = '$user_ID' AND post_type = '$what' ");

     * Only tested with Posts/Pages
     * - there are moments where Draft and Pending shouldn't return any value
  $views['all'] = preg_replace( '/\(.+\)/U', '('.$total.')', $views['all'] ); 
    $views['publish'] = preg_replace( '/\(.+\)/U', '('.$publish.')', $views['publish'] ); 
 /*$views['draft'] = preg_replace( '/\(.+\)/U', '('.$draft.')',    $views['draft'] ); 
 $views['pending'] = preg_replace( '/\(.+\)/U', '('.$pending.')', $views['pending'] ); */
    return $views;

Read More »

Wednesday, 28 May 2014

Add or edit custom link in wp users list in wordpress admin

To enhance the knowledge in WordPress , i am going to write new post for add new custom link in WordPress " User list " or  we can modify exist link and list name  also ..See below the screenshot.

Now question is what should i do to do it??
Yeah its vary to implement  this functionality  here i share a piece of code .to tetsing please copy and paste it into your "Theme Function.php" and save and then run it. this code will edit the existing link.

function my_custom_action_link($actions, $user_object) {
global $wp_roles;
$userRole = new WP_User( $user_object->ID );
if($userRole->roles[0]=="administrator" || $userRole->roles[0]=="editor" || $userRole->roles[0]=="university_admin")
$profileurl= admin_url("user-edit.php?user_id=$user_object->ID&wp_http_referer=%2Fwordpress%2Fwp-admin%2Fusers.php"); 
$profileurl= get_permalink( get_page_by_path( 'Subscriber Profile Page' ) )."?user=$user_object->ID"; 

$actions['edit'] = "<a class='cgc_ub_edit_badges' href='" .$profileurl. "'>" . __( 'Edit', 'cgc_ub' ) . "</a>";
return $actions;

// this filter change the link of users edit row in wp admin
add_filter('user_row_actions', 'my_custom_action_link', 10, 2);
// this filter change the link of users edit row in wp admin 

Now if you want Add new link in user row action please copy and paste in your function.php \

function add_new_link($actions, $user_object) {
    $actions['custom_link'] = "<a class='your_class' href='" . admin_url( "users.php?user=$user_object->ID") . "'>" . __( 'Custom Link', 'cgc_ub' ) . "</a>";
    return $actions;
// admin_url( "users.php?user=$user_object->ID") where this will generate admin users view url you can put site_url() for example

add_filter('user_row_actions', 'add_new_link', 10, 2); 

If you have any query for this post then please do comment here

Read More »

Monday, 26 May 2014

Add pagination in my own plugin in wordpress?

Dear Users,
In this post i will describe how to add pagination in my own plugin with step by step.
as we know wordpress provide us a very huge library of code for every thing that you need in a website. it is very simple task to add pagination in custom plugins. now here i just explain some step of code that will easily add pagination upper and lower section of table .
Here are some  easy step that will help to show pagination in your plugin .
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM
{$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );


$entries = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}table_name
LIMIT $offset, $limit" );

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
echo '<div class="tablenav">
<div class="tablenav-pages" style="margin: 1em 0">
'.$page_links.'</div></div>'; }

Read More »

Sunday, 25 May 2014

Send email to users on post publish with acf plugin custom fields

Dear users,
Today we are talking about sending email when a post is published.As i search on Google for this snippet ,I found many script to done this job but mostly did not work properly  and when i worked  on it ,its successfully sent the email to users..
So there are many  Hooks available for do this job  and these are called  'Actions', Each action takes the form of '{$old_status}_to_{$new_status}'.
These actions are
1) publish_post
2) save_post,
3) draft_to_publish
4) transition_post_status
for more action please visit  WordPress Codex Source

function my_function($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);

$message = "
Dear ".$author->display_name.",
Your post, ".$post->post_title." has just been published at&nbsp
".get_permalink( $post_id ).". Well done!
wp_mail($author->user_email, "Your article is online", $message);
add_action('publish_post', 'my_function');

// or for default use

add_action('acf_save_post', 'my_function');
// if use acf (Advance Custom Field ) plugin and wordpress version should be 3.x
// or wordpress version is 4.x then use it

add_action('acf/save_post', 'my_function');

"publish_post" will send email first time
"save_post" will send email every click on update the post

But if you want send email with Acf plugin fields then you have to use Acf action "acf_save_post"
because of acf plugin work just after publish the post and you can not get the field value of acf in publish_post you  have to use "acf_save_post" for version(3.x) and version(4.x) you have to use "acf/save_post"   for more acf action details please visit the   Acf action

Read More »

Monday, 19 May 2014

Select usermeta table from multiple sets of meta keys and their values in wordpress

Dear WordPress developers,
This is our second post of this blog ,and this post is about WordPress complex MySQL query.Actually i am talking about some scenarios in WordPress when we want to get result from user-meta  table and some time our requirement is out scope of WordPress MySQL function. like we want to get result from user-meta table with more than one meta keys and meta values at that time WordPress MySQL function will not produce exact results so we have to customize the MySQL query to get  the result..

Now Just use WordPress Query Function for Wp User Meta Table

// define global variable 

global $wpdb;  
$sql= $wpdb->get_results("
select * from wp_usermeta a1 inner join wp_usersmeta
a2 on a1.user_id=a2.user_id 
where (a1.meta_key="something" and a1.meta_value="something")
and(a2.meta_key="something" and a2.meta_value="something")
//Now  Output will  

This query will return a stdClass object .

Read More »

Tuesday, 1 April 2014

Turn off Magic Quotes while saveing or inserting data in mysql using Tiny MCE Editor or CK, FCK Editor

How To Turn off Magic Quotes while saveing or inserting data in mysql using Tiny MCE Editor or CK, FCK Editor   Hi Friends,This is my first post on Dark PHP Blog.Now come to the point. it happens sometime when we use text editor like CK Editor,Tiny MCE etc , these editor sometime add some Magic quotes. For this reason we are not get desire result from text editor. The reason behind on this "Magic Quote".To stop this irritating problem. we have to turn off Magic Quote. Now just Copy and Paste this code on top the page between PHP Code Tag "<?php ?>"
 if (get_magic_quotes_gpc()) {
    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
    while (list($key, $val) = each($process)) {
        foreach ($val as $k => $v) {
            if (is_array($v)) {
                $process[$key][stripslashes($k)] = $v;
                $process[] = &$process[$key][stripslashes($k)];
            } else {
                $process[$key][stripslashes($k)] = stripslashes($v);

Read More »