Facebook <br> <div style="width:300px; border:solid 1px;"> <div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.4"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> <div class="fb-share-button" data-href="<?php the_permalink();?>" data-layout="button_count"></div> Twitter <br> <a href="https://twitter.com/share" class="twitter-share-button" >Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> </div> Pinterest <br> <a href="http://pinterest.com/pin/create/button/?url={URI-encoded URL of the page to pin}&media={URI-encoded URL of the image to pin}&description={optional URI-encoded description}" class="pin-it-button" count-layout="horizontal"> <img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /> </a>
Author: b40xq
Register custom post type
add_action(
'init'
,
'codex_book_init'
);
/**
* Register a book post type.
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function
codex_book_init() {
$labels
=
array
(
'name'
=> _x(
'Books'
,
'post type general name'
,
'your-plugin-textdomain'
),
'singular_name'
=> _x(
'Book'
,
'post type singular name'
,
'your-plugin-textdomain'
),
'menu_name'
=> _x(
'Books'
,
'admin menu'
,
'your-plugin-textdomain'
),
'name_admin_bar'
=> _x(
'Book'
,
'add new on admin bar'
,
'your-plugin-textdomain'
),
'add_new'
=> _x(
'Add New'
,
'book'
,
'your-plugin-textdomain'
),
'add_new_item'
=> __(
'Add New Book'
,
'your-plugin-textdomain'
),
'new_item'
=> __(
'New Book'
,
'your-plugin-textdomain'
),
'edit_item'
=> __(
'Edit Book'
,
'your-plugin-textdomain'
),
'view_item'
=> __(
'View Book'
,
'your-plugin-textdomain'
),
'all_items'
=> __(
'All Books'
,
'your-plugin-textdomain'
),
'search_items'
=> __(
'Search Books'
,
'your-plugin-textdomain'
),
'parent_item_colon'
=> __(
'Parent Books:'
,
'your-plugin-textdomain'
),
'not_found'
=> __(
'No books found.'
,
'your-plugin-textdomain'
),
'not_found_in_trash'
=> __(
'No books found in Trash.'
,
'your-plugin-textdomain'
)
);
$args
=
array
(
'labels'
=>
$labels
,
'description'
=> __(
'Description.'
,
'your-plugin-textdomain'
),
'public'
=> true,
'publicly_queryable'
=> true,
'show_ui'
=> true,
'show_in_menu'
=> true,
'query_var'
=> true,
'rewrite'
=>
array
(
'slug'
=>
'book'
),
'capability_type'
=>
'post'
,
'has_archive'
=> true,
'hierarchical'
=> false,
'menu_position'
=> null,
'supports'
=>
array
(
'title'
,
'editor'
,
'author'
,
'thumbnail'
,
'excerpt'
,
'comments'
)
);
register_post_type(
'book'
,
$args
);
}
Remove Description tab if empty in woocommerce – codeforall
Below for line code can do magic if you want to remove the description tab from woocommerce single product page
just add this code to your tabs.php
themes/your-theme-name/woocommerce/single-product/tabs/tabs.php
<?php
$product_detail
= get_post(get_the_ID())->post_content;
if
(
strlen
(
$product_detail
)==0)
{
$description
= 0;
unset(
$tabs
[
"description"
]);
}
?>
copy and paste the above code in tabs.php file just above where it starts
How to add Custom column for custom post type on backend
add_filter(
'manage_edit-cptx_business_columns'
,
array
(
$this
,
'customColumns'
) ) ;
add_action(
'manage_cptx_business_posts_custom_column'
,
array
(
$this
,
'myCustomColumns'
), 10, 3 );
public
function
myCustomColumns(
$column
,
$post_id
) {
global
$post
;
$user_ID
= get_current_user_id();
$count_user_posts_by_type
= create_function(
'$userid,$meta_key, $meta_value, $post_type'
,'
global
$wpdb
;
$where
=
" WHERE post_type = \'cptx_rental\' AND post_author = 4 AND postmeta.meta_value=\'$meta_value\' AND postmeta.meta_key = \'$meta_key\' "
;
$count
=
$wpdb
->get_var(
"SELECT COUNT(*) FROM $wpdb->posts posts left join $wpdb->postmeta postmeta on(postmeta.post_id =posts.id) $where "
);
return
apply_filters( \'get_usernumposts\',
$count
,
$userid
);
');
$total_rentals
=
$count_user_posts_by_type
(
$user_ID
,
"_cptx_rental_client_id"
,
$post
->ID,
'cptx_rental'
);
$total_business
=
$count_user_posts_by_type
(
$user_ID
,
"_cptx_rental_client_id"
,
$post
->ID,
'cptx_business'
);
switch
(
$column
) {
case
'business_type'
:
$terms
= wp_get_post_terms(
$post_id
,
'cptx_business_listing_type'
);
echo
$terms
[0]->name;
break
;
case
'num_businesses'
:
echo
$total_business
;
break
;
case
'users'
:
$client_id
= get_post_meta(
$post_id
,
'_cptx_rental_client_id'
, true);
$client_meta
= get_post_meta(
$client_id
,
'_cl_first_name'
,true);
echo
$client_meta
;
break
;
/* Just break out of the switch statement for everything else. */
default
:
break
;
}
}
public
function
customColumns() {
$columns
=
array
(
'cb'
=>
'<input type="checkbox" />'
,
'title'
=> __(
'Title'
),
'business_type'
=> __(
'Business Type'
),
'thumb'
=> __(
'Thumb'
),
'users'
=> __(
'Users'
),
'date'
=> __(
'Date'
),
);
return
$columns
;
}
get_post_types not working properly in admin
add_action(
'admin_init'
,
function
(){
$post_types
= get_post_types(
array
(
'public'
=> true ),
'names'
);
var_dump(
$post_types
);
});
htaccess redirect 301 from one page to another – working code
page name: store
Redirect to http://www.yourdomain.com/
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / Redirect 301 /store http://www.youtdomain.com/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Leverage Browser Caching with WordPress
Add the below code into your .htaccess file and then check your page speed
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
Gzip compression of Js , Css in wordpress
Add the below code into your .htacess file and then check your website page speed.
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP
Hide add to cart button WooCommerce plugin
hide add to cart button WooCommerce plugin
it will make all products purchasable to FALSE, hence add to cart button will be removed automatically.
1 | add_filter( 'woocommerce_is_purchasable' , false ); |
Load More posts feature in WordPress
Copy below code into your functions.php
function misha_paginator( $first_page_url ){
// the function works only with $wp_query that's why we must use query_posts() instead of WP_Query()
global $wp_query;
// remove the trailing slash if necessary
$first_page_url = untrailingslashit( $first_page_url );
// it is time to separate our URL from search query
$first_page_url_exploded = array(); // set it to empty array
$first_page_url_exploded = explode("/?", $first_page_url);
// by default a search query is empty
$search_query = '';
// if the second array element exists
if( isset( $first_page_url_exploded[1] ) ) {
$search_query = "/?" . $first_page_url_exploded[1];
$first_page_url = $first_page_url_exploded[0];
}
// get parameters from $wp_query object
// how much posts to display per page (DO NOT SET CUSTOM VALUE HERE!!!)
$posts_per_page = (int) $wp_query->query_vars['posts_per_page'];
// current page
$current_page = (int) $wp_query->query_vars['paged'];
// the overall amount of pages
$max_page = $wp_query->max_num_pages;
// we don't have to display pagination or load more button in this case
if( $max_page <= 1 ) return;
// set the current page to 1 if not exists
if( empty( $current_page ) || $current_page == 0) $current_page = 1;
// you can play with this parameter - how much links to display in pagination
$links_in_the_middle = 4;
$links_in_the_middle_minus_1 = $links_in_the_middle-1;
$first_link_in_the_middle = $current_page - floor( $links_in_the_middle_minus_1/2 );
$last_link_in_the_middle = $current_page + ceil( $links_in_the_middle_minus_1/2 );
// some calculations with $first_link_in_the_middle and $last_link_in_the_middle
if( $first_link_in_the_middle <= 0 ) $first_link_in_the_middle = 1;
if( ( $last_link_in_the_middle - $first_link_in_the_middle ) != $links_in_the_middle_minus_1 ) { $last_link_in_the_middle = $first_link_in_the_middle + $links_in_the_middle_minus_1; }
if( $last_link_in_the_middle > $max_page ) { $first_link_in_the_middle = $max_page - $links_in_the_middle_minus_1; $last_link_in_the_middle = (int) $max_page; }
if( $first_link_in_the_middle <= 0 ) $first_link_in_the_middle = 1;
// begin to generate HTML of the pagination
$pagination = '<nav id="misha_pagination" class="navigation pagination" role="navigation">
<div class="older">';
if( $current_page < $max_page )
$pagination.= '<a title="Read More" id="misha_loadmore">Read More <i class="fa fa-angle-double-right"></i> </a> ';
$pagination.= "</div></nav>\n";
echo str_replace(array("/page/1?", "/page/1\""), array("?", "\""), $pagination);
}
function misha_my_load_more_scripts() {
global $wp_query;
// In most cases it is already included on the page and this line can be removed
wp_enqueue_script('jquery');
// register our main script but do not enqueue it yet
wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/myloadmore.js', array('jquery') );
wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $wp_query->max_num_pages,
'first_page' => get_pagenum_link(1)
) );
wp_enqueue_script( 'my_loadmore' );
}
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );
function misha_loadmore_ajax_handler(){
$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
$args['post_status'] = 'publish';
query_posts( $args );
if( have_posts() ) :
while( have_posts() ): the_post();
get_template_part('content', 'grid');
endwhile;
endif;
die;
}
add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}
JS code to send Ajax call and receive response
<script type="text/javascript">
jQuery(function($){
$('body').on('click', '#misha_loadmore', function(){
$.ajax({
url : misha_loadmore_params.ajaxurl,
data : {
'action': 'loadmore',
'query': misha_loadmore_params.posts,
'page' : misha_loadmore_params.current_page,
'first_page' : misha_loadmore_params.first_page // here is the new parameter
},
type : 'POST',
beforeSend : function ( xhr ) {
$('#misha_loadmore').text('Loading...');
},
success : function( data ){
$('ul.sp-grid').append(data);
//$('#misha_pagination').remove();
$('#misha_loadmore').html('Read More <i class="fa fa-angle-double-right"></i>');
misha_loadmore_params.current_page++;
}
});
return false;
});
});
</script>
Finally html button which will be clicked to load more posts again and again.
<?php misha_paginator( get_pagenum_link() ); ?>