Tuesday, January 15, 2013

Enable widgets for editor role

$role_object = get_role( 'editor' );
//print_r($role_objec);
// add $cap capability to this role object
$role_object->add_cap( 'edit_theme_options' );

function custom_admin_menu() {

$user = new WP_User(get_current_user_id());
if (!empty( $user->roles) && is_array($user->roles)) {
foreach ($user->roles as $role)
$role = $role;
}

if($role == "editor") {
remove_submenu_page( 'themes.php', 'themes.php' );
remove_submenu_page( 'themes.php', 'nav-menus.php' );
remove_submenu_page( 'themes.php', 'theme_options' );
remove_submenu_page( 'themes.php', 'custom-header' );
}
}

add_action('admin_menu', 'custom_admin_menu', 11);


Thursday, January 3, 2013

WordPress pagination without plugins

global $wp_query; 
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo '<div class="page_nav">';
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => 'Prev',
'next_text' => 'Next'
));
echo '</div>';
}

Wednesday, January 2, 2013

Facebook open graph snippet to set default image

function diww_facebook_image() {
                echo '<meta property="fb:admins" content="ADMIN_ID" />';
                echo '<meta property="og:title" content="' . get_the_title() . '" />';
                echo '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />';
        global $post;
        if ( is_singular() ) { // only if a single post or page
                echo '<meta property="og:type" content="article" />';
                echo '<meta property="og:url" content="' . get_permalink() . '" />';
        if (has_post_thumbnail( $post->ID )) { // use featured image if there is one
                $feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
                echo '<meta property="og:image" content="' . esc_attr( $feat_image[0] ) . '" />';
         }else{ // use site logo in case no featured image
                echo '<meta property="og:image" content="http://yourdomain.com/logo.png" />';
         }
        }
        if ( is_home() ) { // for homepage only
                echo '<meta property="og:type" content="website" />';
                echo '<meta property="og:url" content="' . get_bloginfo('url') . '" />';
                echo '<meta property="og:image" content="http://yourdomain.com/logo.png" />';
        }
}
add_action( 'wp_head', 'diww_facebook_image' );

Wednesday, November 28, 2012

use the get_bookmarks() method

<?php $links = get_bookmarks( array(
'orderby' => 'link_id',
'order' => 'ASC',
));
//print_r($links);
// Loop through each bookmark and display the formatted output
foreach ( $links as $link ) {
echo '<li>';
echo '<strong>'.$link->link_name.'</strong>';
echo '<br />';
echo '<a href="'.$link->link_url.'" target="_blank">';
echo $link->link_url.'</a>';
echo '<br />';
echo $link->link_description;
echo '</li>';
} ?>

Wednesday, November 21, 2012

display all Authors in an unordered list

$args = array(
 'blog_id' => $GLOBALS['blog_id'],
 'role' => 'Author',
 'orderby' => 'nicename'
 ); 

  $blogusers = get_users($args);
//  print_r($blogusers);
    foreach ($blogusers as $user) {
    echo '' . $user->user_email . '';
    }
    


Sunday, November 18, 2012

meta_query with comparison

Display posts of type 'my_custom_post_type', ordered by 'training_date', and filtered
  <?php
$todaysDate = date( 'd-m-Y' );
$args = array(
'post_type' => 'training',
'meta_key' => 'training_date',
'orderby' => 'training_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'training_date',
'value' => $todaysDate,
'compare' => '>=',
))

);
$query = new WP_Query($args);

Tuesday, November 13, 2012

How to hide Personal Options in WordPress’ User Profile


Adding this code to the functions.php of your wordpress theme
function hide_personal_options() {
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#your-profile .form-table:first, #your-profile h3:first").remove();
});
</script>
<?php
}
add_action( 'personal_options', 'hide_personal_options');