Categories
Uncategorized WordPress

Get WordPress post content by post id

$my_postid = 12;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
echo get_post_field('post_content', $post_id);
Categories
WooCommerce WordPress

WooCommerce – Update number of items in cart and total after Ajax

add_filter('add_to_cart_fragments', 'chet_tips_woocommerce_header_add_to_cart_fragment');

function chet_tips_woocommerce_header_add_to_cart_fragment( $fragments ) {
  global $woocommerce;

  ob_start();

  ?>
  <span class="cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
  <?php

  $fragments['span.cart-count'] = ob_get_clean();

  return $fragments;

}
Categories
Search WordPress

WordPress Exclude pages from search

function chet_tips_modify_search_filter($query) {

  if ($query->is_search) {

    $query->set('post_type', 'post');

  }

  return $query;

}

add_filter('pre_get_posts','chet_tips_modify_search_filter');
Categories
Search WordPress

WordPress Exclude categories from search

function chet_tips_search_filter($query) {

  if ( $query->is_search && ! is_admin() ) {

    $query->set('cat','8,15'); 

  }

  return $query; 

}

add_filter('pre_get_posts','chet_tips_search_filter');
Categories
robot.txt WordPress

Block directories using robots.txt

Restrict search engines from crawling to the root directory by adding the code snippet below to robots.txt file.

User-agent: *

Disallow: /cgi-bin/

Disallow: /wp-admin/

Disallow: /wp-includes/

Disallow: /xmlrpc.php

Disallow: /wp-content/plugins/

Disallow: /wp-content/cache/

Disallow: /wp-content/themes/

Disallow: /trackback/

Disallow: /feed/

Disallow: /comments/

Disallow: /category/

Disallow: /trackback/

Disallow: /feed/

Disallow: /comments/

Disallow: /*?

Allow: /wp-content/uploads/
Categories
htaccess WordPress

WordPress Restrict access to wp-includes file via .htaccess fil

# Block wp-includes folder and files

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteRule ^wp-admin/includes/ - [F,L]

RewriteRule !^wp-includes/ - [S=3]

RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]

RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]

RewriteRule ^wp-includes/theme-compat/ - [F,L]

</IfModule>
Categories
WordPress

WordPress Redirect to a custom page after registration

function chet_tips_my_registration_redirect(){

    return home_url( '/my-page' );

}

add_filter( 'registration_redirect', 'chet_tips_my_registration_redirect' );
Categories
BuddyPress WordPress

Automatically add new users to BuddyPress group

function chet_tips_automatic_group_membership( $user_id ) {

 if( !$user_id ) return false;

 groups_accept_invite( $user_id, <# group ID #> );

}

add_action( 'bp_core_activated_user', 'chet_tips_automatic_group_membership' );
Categories
WordPress

WordPress Disable admin bar access to all users

If you want to disable access to admin bar for all the users including the administrator, add the following to the functions file.

show_admin_bar(false);
Categories
WordPress

WordPress Change the length of excerpts

function chet_tips_excerpt_length( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'chet_tips_excerpt_length', 999 );

By default, length of the excerpts in WordPress is 55 words. Tweak the functions by adding the following commands to customize the length so it can fit the layout.