PHP Developer

Thursday, 31 March 2016

WordPress Post Custom Pagination Without Plugin

WordPress Post Custom Pagination Without Plugin

This code helps you to create template and get post with pagination without using plugin.

<?php

/*

Template Name: Post Pagination without Plugin in WordPress

*/
get_header();
?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
) );
?>
<?php
function wp_pagination() {
global $wp_query;
$big = 12345678;
$page_format = paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages,
    'type'  => 'array'
) );
if( is_array($page_format) ) {
            $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
            echo '<div><ul>';
            echo '<li><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
            foreach ( $page_format as $page ) {
                    echo "<li>$page</li>";
            }
           echo '</ul></div>';
}
}
?>

<article id="core_middle_column" class="col-md-8 col-sm-8"><div id="core_ajax_callback"></div>
<div class="clearfix" id="recentlistings">
<?php 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array( 'posts_per_page' => 2, 'post_type' => 'post', 'post_status' => 'publish', 'post__in' => array(1, 2, 3, 4), 'paged' => $paged ));

if ( have_posts() ) : ?>
<div class="_searchresultsdata">
<div class="panel panel-default">
<div class="list_style  wlt_search_results row">
<?php while ( have_posts() ) : the_post(); ?>
<div class="itemdata col-md-4 item-121 col-sm-6 col-xs-12">
<div class="thumbnail clearfix">
<?php echo get_the_title();?>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<div class="custom_pagination"><?php wp_pagination(); ?></div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php endif; ?>
</div>
</article>
<style>
.custom_pagination ul {
  display: initial;
}

.custom_pagination li:first-child{
 background-color: #fff;
  border: 1px solid #ddd;
  color: #0b5b96;
  list-style: outside none none !important;
  padding: 6px 12px !important;
  text-decoration: none;
  border-radius:15px;
  float:right !important;
}

.custom_pagination li {
  float: left !important;
  list-style: outside none none !important;
}

.custom_pagination .page-numbers {
  background-color: #fff;
  border: 1px solid #ddd;
  color: #0b5b96;
 list-style: outside none none !important;
  padding: 6px 12px !important;
  text-decoration: none;
float: left;
}

.page-numbers.current {
  background-color: #0b5b96 !important;
  color: #fff !important;

}
</style>
<?php
get_footer();
?>

Monday, 28 March 2016

Add and Removes a Capability for a User Role in Wordpress

Add and Removes a Capability for a User Role in Wordpress

Add this code in function.php to Add and Removes a Capability from a User Role

<?php
/**
 * Remove capabilities from editors.
 *
 * Call the function when your plugin/theme is activated.
 */

function wpcodex_set_capabilities() {

    // Get the role object.
    $editor = get_role( 'editor' ); // Get alredy registerd Role "Editor" in wordpress

    // A list of capabilities to remove from editors.
    $caps_remove = array(
        'moderate_comments',
        'manage_categories',  // Allows user to manage post categories
        'manage_links',  // Allows user to edit links
        'edit_others_posts',  // Allows user to edit others posts not just their own
        'edit_others_pages',  // Allows user to edit pages
        'delete_posts',  //  Allows user to delete posts
        'edit_themes', // false denies this capability. User can’t edit your theme
        'install_plugins', // User cant add new plugins
        'update_plugin', // User can’t update any plugins
        'update_core', // user cant perform core updates
    );
   
    // A list of capabilities to Add for editors.
    $caps_add = array(
        'read', // true allows this capability
        'edit_posts', // Allows user to edit their own posts
        'edit_pages', // Allows user to edit pages
        'edit_others_posts', // Allows user to edit others posts not just their own
        'create_posts', // Allows user to create new posts
        'manage_categories', // Allows user to manage post categories
        'publish_posts', // Allows the user to publish, otherwise posts stays in draft mode
    );

    foreach ( $caps_remove as $cap ) {
        // Remove the capability.
        $editor->remove_cap( $cap );
    }
   
    foreach ( $caps_add as $capd ) {
        // Add the capability.
        $editor->add_cap( $capd );
    }
}
add_action( 'init', 'wpcodex_set_capabilities' );

?>

Create New User Role With Capability in Wordpress

Create New User Role With Capability in Wordpress


Add below code in function.php, then new role "Client" is registered with capabilities .

<?php
$result = add_role( 'client', __(

'Client' ),

array(

'read' => true, // true allows this capability
'edit_posts' => true, // Allows user to edit their own posts
'edit_pages' => true, // Allows user to edit pages
'edit_others_posts' => true, // Allows user to edit others posts not just their own
'create_posts' => true, // Allows user to create new posts
'manage_categories' => true, // Allows user to manage post categories
'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
'edit_themes' => false, // false denies this capability. User can’t edit your theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => false, // User can’t update any plugins
'update_core' => false // user cant perform core updates

)

);

?>