Pages

Tuesday, June 10, 2014

Wordpress add user meta fields - [resolved]


function wp_vendor_products_upload_limit( $user ) {
if ( is_super_admin() ) {
?>
<h3><?php _e('Vendor Product Upload Limit'); ?></h3>

<table class="form-table">
<tr>
<th>
<label for="no_of_vendor_products"><?php _e('No of Products'); ?>
</label></th>
<td>
<input type="text" name="no_of_vendor_products" id="no_of_vendor_products" value="<?php echo esc_attr( get_the_author_meta( 'no_of_vendor_products', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Please enter no. of products.'); ?></span>
</td>
</tr>
</table>
<?php
}
}

function wp_save_vendor_products_upload_limit( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;

update_usermeta( $user_id, 'no_of_vendor_products', $_POST['no_of_vendor_products'] );
}

add_action( 'show_user_profile', 'wp_vendor_products_upload_limit' );
add_action( 'edit_user_profile', 'wp_vendor_products_upload_limit' );

add_action( 'personal_options_update', 'wp_save_vendor_products_upload_limit' );
add_action( 'edit_user_profile_update', 'wp_save_vendor_products_upload_limit' );


add_action('manage_users_columns','remove_user_posts_column');
function remove_user_posts_column($column_headers) {
    unset($column_headers['posts']);
    return $column_headers;
}

How to display wordpress posts view count? - [Solved]

1. Please write below code in functions.php

<?php
// function to display number of views for post
function wp_get_post_views($postId){
    $count_key = 'count_post_views';
    $count = get_post_meta($postId, $count_key, true);
    if($count == ''){
        delete_post_meta($postId, $count_key);
        add_post_meta($postId, $count_key, '0');
        return '0 View';
    }
    return $count.' Views';
}

// function to set post views
function wp_set_post_views($postId) {
    $count_key = 'count_post_views';
    $count = get_post_meta($postId, $count_key, true);
    if($count == ''){
        $count = 0;
        delete_post_meta($postId, $count_key);
        add_post_meta($postId, $count_key, '0');
    }
else
{
        $count++;
        update_post_meta($postId, $count_key, $count);
    }
}

// add views column to wp_admin
add_filter('manage_posts_columns', 'wp_posts_views_column');
function wp_posts_views_column($defaults){
    $defaults['post_views'] = __('Views');
    return $defaults;
}

add_action('manage_posts_custom_column', 'wp_posts_views_custom_column',10,2);
function wp_posts_views_custom_column($column_name, $id){
    if($column_name === 'post_views'){
        echo wp_get_post_views(get_the_ID());
    }
}
?>

2. Now, open singe.php and add below code inside the loop.


<?php
wp_set_post_views(get_the_ID());
?>

3. Please write below code for display views of post.

<?php
wp_get_post_views(get_the_ID());
?>


4. If you want to sort post by view than add below code:

<?php
$args = array(  'numberposts'  => 10,
                'orderby'      => 'meta_value',
                'meta_key'     => 'count_post_views',
                'order'        => 'DESC',
                'post_type'    => 'post',
                'post_status'  => 'publish'
            );
$mostViewedPosts = get_posts( $args );
foreach( $mostViewedPosts as $mostViewedPost ) {
/* Write your code here */
}
?>

Monday, June 9, 2014

Woocommerce create role for vendor and add products capabilities for him

Please write below code in functions.php



add_role('shop_vendor', 'Shop Vendor', array(
'read' => true,
'read_product' => true,
'edit_product' => true,
'edit_products' => true,
'edit_private_products' => true,
'delete_product' => true,
'delete_products' => true,
'delete_published_products' => true,
'publish_product' => true,
'publish_products' => true,
'edit_published_products' => true,
'assign_product_terms' => true,
));

Monday, June 2, 2014

WP_List_Table




$testData = new Test_List_Table();
$testData->prepare_items();
?>



$testData->search_box( 'search', 'search_id' );
$testData->display(); 
?>
 

if( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

class Test_List_Table extends WP_List_Table
{
/**
     * Prepare the items for the table to process
     *
     * @return Void
     */
    public function prepare_items()
    {
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();

        $data = $this->table_data();
        usort( $data, array( &$this, 'sort_data' ) );

        $perPage = 10;
        $currentPage = $this->get_pagenum();
        $totalItems = count($data);

        $this->set_pagination_args( array(
            'total_items' => $totalItems,
            'per_page'    => $perPage
        ) );

        $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);

        $this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
        $this->items = $data;
    }

    /**
     * Override the parent columns method. Defines the columns to use in your listing table
     *
     * @return Array
     */
    public function get_columns()
    {
$columns = array(
'cb'        => '',
'title' => 'Title',
'sort_desc' => 'Short Description',
'edit' => 'Edit',
'view' => 'View',
'delete' => 'Delete'
);

        return $columns;
    }

    /**
     * Define which columns are hidden
     *
     * @return Array
     */
    public function get_hidden_columns()
    {
        return array();
    }

    /**
     * Define the sortable columns
     *
     * @return Array
     */
    public function get_sortable_columns()
    {
        return array('title' => array('title', false));
    }

function get_bulk_actions() {
$actions = array(
'delete_multi'    => 'Delete'
);
return $actions;
}

function column_cb($item) {
        return sprintf(
            '', $item['id']
        );    
    }

    /**
     * Get the table data
     *
     * @return Array
     */
    private function table_data()
    {
        $data = array();
global $wpdb;

$whereCnd = '';
if(isset($_REQUEST['s'])){
$whereCnd = ' Search query ' ;
}

$listsData = $wpdb->get_results( "Main Query");

if(!empty($listsData)) {

foreach($listsData as $key => $val){
$delNonce = wp_create_nonce( 'wp-list-'.$val->ID.'-delete-nonce' );
$data[] = array(
'id'        => $val->ID,
'title' => $val->title,
'sort_desc' => $val->sort_description,
'edit' => 'edit',
'view' => 'View',
'delete' => 'Delete'
);
}

        return $data;
    }

    /**
     * Define what data to show on each column of the table
     *
     * @param  Array $item        Data
     * @param  String $column_name - Current column name
     *
     * @return Mixed
     */
    public function column_default( $item, $column_name )
    {
        switch( $column_name ) {
            case 'id':
            case 'title':
            case 'sort_desc':
            case 'edit':
            case 'view':
            case 'delete':
return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
        }
    }

    /**
     * Allows you to sort the data by the variables set in the $_GET
     *
     * @return Mixed
     */
    private function sort_data( $a, $b )
    {
        // Set defaults
        $orderby = 'title';
        $order = 'asc';

        // If orderby is set, use this as the sort column
        if(!empty($_GET['orderby']))
        {
            $orderby = $_GET['orderby'];
        }

        // If order is set use this as the order
        if(!empty($_GET['order']))
        {
            $order = $_GET['order'];
        }


        $result = strcmp( $a[$orderby], $b[$orderby] );

        if($order === 'asc')
        {
            return $result;
        }

        return -$result;
    }

function process_bulk_action() {        
global $wpdb;
        if( 'delete_multi'===$this->current_action() ) {
global $wpdb;

if(is_array($_REQUEST['id'])) 
{
echo "Your actions";
}
die();
        }        
    }

}