Saturday 5 November 2016

Sunday 7 August 2016

Get the Home URL or Site URL in PHP

It is very simple to get the Home URL or the Site URL in PHP. You have to just include a settings  file consisting of the functions that we create and include in all the pages so that if we call the function and echo the variable the output will be obtained.

Code:

function get_site_url()
{
    $currentPath = $_SERVER['PHP_SELF'];
    $pathInfo = pathinfo($currentPath);
    $hostName = $_SERVER['HTTP_HOST'];
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
    return $protocol.$hostName.$pathInfo['dirname']."/";
}

Usage:

Example: http://example.com/about
This is a Page you are currently accessing and we have to display the home page link in that page and if you access this function you will get the output as follows 

$url =get_site_url();
echo $url;
Output: http://example.com

Sunday 17 July 2016

Register a Custom Menus Location In WordPress

To register a custom Menu location you must use the following function for registration.

register_nav_menu( $location, $description );
$location - The location where you want to display the menu in your theme (e.g.) - Primary,Secondary,Main Header etc.,
$description - The Description about the menu Location and the Menu.

Step: 1 Add the following lines in the function.php file.
function register_menu() {
    register_nav_menu('primary-menu', __('Primary Menu'));
}
add_action('init', 'register_menu');

Step: 2 Add the following lines in the file where you want the menu to display.

if ( has_nav_menu( 'primary-menu' ) ) { /* if menu location 'primary-menu' exists then use custom menu */
      wp_nav_menu( array( 'theme_location' => 'primary-menu') );
}

Saturday 16 July 2016

Register and Display a Sidebar in Wordpress

To register and display the Sidebar in WordPress you have to follow the two steps.

Step: 1 You have to add this in the Function.php file in your theme
Below the "wpb alone should be replaced by your theme name"

function wpb_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'wpb' ),
        'id' => 'sidebar-1',
        'description' => __( 'The main sidebar appears on the right on each page except the front page template', 'wpb' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    register_sidebar( array(
        'name' =>__( 'Front page sidebar', 'wpb'),
        'id' => 'sidebar-2',
        'description' => __( 'Appears on the static front page template', 'wpb' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    }
add_action( 'widgets_init', 'wpb_widgets_init' );

Step: 2 Display it Dynamically in the Page where ever you want by adding the below code

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
    <div id="secondary" class="widget-area" role="complementary">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
    </div>
<?php endif; ?>

Note: " is_active_sidebar( 'sidebar-1' )" - The sidebar-1 is the id of the registered sidebar in the function.php file. If the id is not used properly the Sidebar will not be displayed even if the registration is correct.

Custom Post Type Loop in Wordpress

In order to get posts from our custom post type you can follow the loop that is shown below by replacing the post type with your custom post type.

$query = array(
        'post_type'=> 'your_custom_post_type',
        'orderby'    => 'ID',
        'post_status' => 'publish',
        'order'    => 'DESC',
        'posts_per_page' => -1
    );
    $result = new WP_Query( $query );
    if ( $result-> have_posts() ) : ?>
        <?php while ( $result->have_posts() ) : $result->the_post(); ?>
            <?php
            // to get the post featured image as thumbnail
            the_post_thumbnail('thumbnail');
             ?>
            <?php
           // to get the Post title
           the_title();
            ?>
            <?php
           // to get the post content
            the_content();
           ?>
        <?php endwhile; ?>
    <?php endif; wp_reset_postdata(); ?>

This query will get the posts that are created under your custom post type and i have displayed the post featured image, post title and the post content.

Sunday 24 April 2016

Creating Custom Post type Plugin in Wordpress

8 Easy Steps of how to create a Custom Post type Plugin in WordPress

This post will provide you a very easiest way to create the custom post type plugin in WordPress which automatically creates your plugin with the default functionality that a WordPress plugin will have.

Plugin folder Structure:
  • wp-content
    • places_to_visit(folder-name)
    • index.php
      • css
        • style.css
      • js
        • jquery.js
      • images
        • logo.png

 Follow this Steps and you will find it Interesting of how easy the plugin creation is in WordPress.

Step:1 Create a New Folder inside the Plugins folder which you find it inside the wp-content.
Step:2 Name the New folder which you have created with the custom post-type plugins name that you are going to create.
Step:3 Open up your editor with a new file which you are using and paste the below code in that and save the file name as "index.php"

Index File Code:

<?php
/*
Plugin Name: Places to Visit
Plugin URI: http://nareshkumar979.blogspot.in/
Description: The plugin helps the tourists to know what are all the places available to visit when then plan to spend a vacation at a particular place.
Version: 1.0
Author: Naresh Kumar.P
Author URI: http://nareshkumar979.blogspot.in/
License: NK001
*/
//Starting of the Custom Post Type called "Places to Visit"
add_action( 'init', 'places_to_visit' );
function places_to_visit()
{
 $args = array(
            'labels' => array(
            'name' => 'Places to Visit',
            'singular_name' => 'Place to Visit',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Places to Visit',
            'edit' => 'Edit',
            'edit_item' => 'Edit Places to Visit',
            'new_item' => 'New Places to Visit',
            'view' => 'View',
            'view_item' => 'View Places to Visit',
            'search_items' => 'Search Places to Visit',
            'not_found' => 'No Places to Visit found',
            'not_found_in_trash' => 'No Places to Visit found in Trash',           
            ),          
            'menu_position' => 15,
            'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),           
            'menu_icon' => plugins_url( 'images/logo.png', __FILE__ ),
            'public' => true         
         );
         register_post_type( 'places_to_visit',$args);            
 }
 //End of the Custom Post Type called "Places to Visit"
?>

Points to Ponder:

Note: The plugin Index file should not contain any spaces between the codes. It should be coded as per my "index.php" file.
"If index.php file is coded with spaces it will create warning dialog at the time of plugin activation"

In this plugin the post type will be "places_to_visit".
"supports"-> This will decide what are all functionalities and input fields that your plugin can have.
(eg.) I have added
  • title:  will hold the Post Name 
  • editor: used for entering the contents for that particular posts.
  • thumbnail:  used for having a image for the post.
  • custom-fields: It will be displayed if you create the custom fields for the posts that you create(Will explain in the Later posts of how to create custom fields for the particular posts)
For placing the Image icon to your plugin continue the steps below.

Step:4 Create a folder called "images" inside the custom post-type folder and place the image which you want to keep as icon for you plugin.
Note: the icon size should be either (24X24 or 32X32). Other than this size the icon will not fit in the menu place.
Step:5 For adding the css and js create seperate folder as you done for the images and include it in the pages which you need.
Step:6 At last after doing all this you go to the WordPress dashboard by logging in and move to the Plugins Menu.
Step:7 You will find the Name of the Plugin which you have created under the "Activate" mode.
Step:8 If you click on Activate your plugin will be activated and you will find your plugin in Sidebar Menu.


Laravel - Best framework in PHP

Meaning of the Term Laravel?

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. Some of the features of Laravel are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance, and its orientation toward syntactic sugar.

Who uses Laravel and for what purpose they use?

A person who is onto the Web Development uses this terminology called Laravel for developing the Web Applications / Portals / Forums etc., It is one of the Latest framework which is been used by most of the developers who belongs to the open source.

Laravel Releases?

The team of Laravel started with a basic version of Laravel 4.2 and now they are fast developing
and they have released the following versions.
  • Laravel 4.2
  • Laravel 5.0
  • Laravel 5.1
  • Laravel 5.2 and the Master pack of Laravel
 

Email Code PHP

This is a Simple Code which is written in the PHP for Sending Emails from One Server to all the Other Domains. It is very easy and little C...