How to add shortcode in your wordpress theme or plugin

Shortcode is really important part of codding. Sometimes you need to execute code through shortcode on any event or through any filter. Since Version 2.5 WordPress supports shotcodes.

Before using shortcode you should keep these things in your mind:

  1. Don’t use any general used shortcode name. Because shortcode uses hook so if any other plugin or theme functions uses same shortcode name then your shortcode will be overwrite of those.
  2. If you are using any attributes in shortcode then these should be in lower case.

Add_shortcode is a function which use to add hook for shortcode. It uses 2 parameters.

  1. Tags
  2. Function name
 add_shortcode( $tag , $func ); 

Here is a simple example to use shortcode.


function shortcode_function( $atts ) {

     return "name = {$atts[name]}";

}

add_shortcode('shortcode_fun', 'shortcode_function');

 

So now you can use this code with the shortcode [shortcode_fun].

How Akismet Plugin is useful for WordPress Websites

Akismet is WordPress plugin. You will find lots of articles and posts related to Akismet Plugin. I just want to tell that How it’s useful for your WordPress website.

WordPress is good CMS but now days SEO Guys (link builders) use WordPress websites and Drupal as a link building websites. Especially if you have enabled the comments option in WordPress then you will get hundreds of spam through comments. For these problems Akismet Plugin is really good solution.

You need an API key to enable the Akismet Plugin. You can find the API through the Akismet website. There are 3 types of plan. If you are using your website for Business purpose then you should buy business plan otherwise Akismet provides a free API key for personal use.

How to Disable WordPress Admin Tool Bar

Recently I was working with a membership WordPress website. I added login form and write some code for that.
I checked the login code and that was working but I realize that logged in user can view admin bar and unknown user can also access wp-admin dashboard then I search and found the following code. It is really useful.

If you want to hide admin tool bar only for users:

add_action('after_setup_theme', 'remove_admin_toolbar');

function remove_admin_toolbar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
	}
}

But if you want to hide for all type of users:

add_action('after_setup_theme', 'remove_admin_toolbar');

function remove_admin_toolbar() {
  show_admin_bar(false);

}

How to close comment for all posts and pages in wordpress

There are 2 option to close comment in wordpress:

1. replace

<?php comments_template(); ?>

to

<?php //comments_template()?>

2. This is really good option to close comment.

add_filter( 'comments_open', 'my_comments_open', 10, 2 );

function my_comments_open( $open, $post_id ) {

	$post = get_post( $post_id );

	if ( 'page' == $post->post_type && 'post' == $post->post_type )
		$open = false;

	return $open;
}

How to remove comment in genesis

Paste below code in functions.php to remove comment section from front end in genesis.

add_action( 'wp_enqueue_scripts', 'nm_custom_remove_comments' );

function nm_custom_remove_comments() {
        remove_action( 'genesis_after_post', 'genesis_get_comments_template' );
}

Template for custom post type

Sometimes you create any custom post type like product testimonial etc. but then you face problem with templating for custom post type. You did not call archive or single page directly for that. In that case you can create 2 template file for custom post.

  • archive-{post_type}.php
  • single-{post_type}.php

Eg. You have created custom post type “product” then archive.php will be archive-product.php and single.php will be single-product.php.

Debugging in WordPress

Debugging is the part of every programming language and every CMS of PHP. WordPress have specific debugging system with specific sets of Code.

WP_DEBUG

WP_DEBUG is part of wp-config.php file. You can set Boolean value for this. True to show errors and False to display off the errors.

define('WP_DEBUG', true);
define('WP_DEBUG', false);

WP_DEBUG_LOG

WP_DEBUG_LOG used to save all errors in debug.log file. It will be helpful when you want to review all errors after any particular time. So you have all errors in debug.log file.

define('WP_DEBUG_LOG', true);

SCRIPT_DEBUG

SCRIPT_DEBUG will force WordPress to use the “dev” versions of core CSS and Javascript files rather than the minified versions.

define('SCRIPT_DEBUG', true);

SAVEQUERIES

SAVEQUERIES saves the database queries to an array.

define('SAVEQUERIES', true);

Footer in Genesis Freamework of WordPress

Sometimes you need to do to following things with footer:
1. Need to remove footer support from theme
2. Need to remove footer
3. Need to add custom footer HTML
So here is an example to for all above option.

1. Need to remove footer support from theme

/** Remove footer widgets support from theme */
remove_theme_support( 'genesis-footer-widgets', 3 );

2. Need to remove footer

/** Remove Footer */
remove_action( 'genesis_footer', 'genesis_do_footer' );

3. Need to add custom footer HTML

remove_action( 'genesis_footer', 'genesis_do_footer' );
add_action( 'genesis_footer', 'custom_footer_html' );
/**
 * Custom Footer HTML.
 */
function custom_footer_html() { ?>

	<div>
		 <!-- Your Copyright text goes here -->
	</div>
<?php } ?>

Add custom column in post list

Sometimes you want to add one custom column for post or custom type posts like when you added some post meta values and want to show it in post list then you found that you have no way to add that through wp-admin files.
It’s just a simple function.php work. You just need to work on code to add extra column in post list.

    // ADD NEW EXTRA COLUMN
    function postauthorDOB_columns_head($defaults) {
        $defaults['author_dob'] = 'Authors DOB';
        return $defaults;
    }

    function postauthorDOB _columns_content($column_name, $post_ID) {
        if ($column_name == 'author_dob') {
$meta_values = get_post_meta( $post_ID, 'author_dob');
Echo $meta_values ;
      }
    }

add_filter('manage_posts_columns', ‘postauthorDOB _columns_head');
add_action('manage_posts_custom_column', ‘postauthorDOB _columns_content', 10, 2);

How to call JS in your wordpress plugin

Javascript is really an amazing think, when you are developing any website with good animation, call value without page loading etc.
Wordpress already call latest jquery when call wp_head() function in header.php, but sometimes you want to add own script in plugin. wp_enqueue_script is then useful for you.

<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>

$handle : (string) (required) Name used as a handle for the script.

$src : (string) (optional) URL to the script, e.g. http://example.com/wp-content/themes/my-theme/my-theme-script.js.

$deps : (array) (optional) Array of the handles of all the registered scripts that this script depends on, that is the scripts that must be loaded before this script. Set false if there are no dependencies.

$ver : (string) (optional) String specifying the script version number, if it has one, which is concatenated to the end of the path as a query string.

$in_footer : (boolean) (optional) Normally, scripts are placed in of the HTML document.

<?php
function my_scripts_method() {
	wp_enqueue_script( 'my-script-name', get_template_directory_uri() . '/js/script.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
?>