Blog

WordPress Tutorial : Plugin Basics

March 19, 2019 by admin

In this tutorial we we learn basics of Writing a wordpress plugin.

A WordPress plugin is a PHP file with a WordPress plugin header comment. It’s highly recommended that you create a directory to hold your plugin so that all of your plugin’s files are neatly organised in one place.

In this article we will discuss in detail with following steps:

Step 1:

1.First you will need to install WordPress, you can follow wordpress installation tutorial here.

2.After wordpress installation. Goto to the WordPress installation’s wp-content directory.

3.Open the plugins directory.

4.Create a new directory and name it after the plugin (e.g. plugin-name).

5.Create a new PHP file (it’s also good to name this file after your plugin, e.g. plugin-name.php).

6.Open the File to Edit and write this code in header and save the File.

       

6.Open the File to Edit and write this code in header and save the File.

7.Now goto Plugins form left menu. This screen will list number of plugins installed. find "GBS DEMO PLUGIN" and activate.

You can also include further information in plugin header

       

What is: Hooks

In WordPress theme and development, Hooks are functions that can be applied to an Action or a Filter in WordPress. Actions and Filters in WordPress are functions that can be modified by theme and plugin developers to change the default WordPress functionality.

Functions used to modify Actions/Filters in WordPress can be hooked into WordPress. However, it is important to note that actions and filters are not the same thing. Now we will discuss and differentiate in detail WordPress Actions and filters.

Filters

Filters are functions that WordPress uses to pass data through. Passing data through filters allows developers to modify the default behavior of a specific function. Functions used to filter data are called hooks. Filters and Actions together allow developers great flexibility to modify default WordPress filters and actions and even create their own custom filters and actions so that other developers can extend their plugins or themes.

Filters are different than Actions. WordPress actions are executed at events like when a theme or plugin is activated, or when a post is published. Filters are used to filter output when it is sent to either database or to user browser.

Example of using a WordPress filter

       
function wpb_custom_excerpt( $output ) {
  if ( has_excerpt() && ! is_attachment() ) {
    $output .= wpb_continue_reading_link();
  }
  return $output;
}
add_filter( 'get_the_excerpt', 'wpb_custom_excerpt' );

The sample code above adds a function wpb_custom_excerpt to the filter get_the_excerpt.

Actions

An action is a function in WordPress code that is run at certain points throughout the WordPress core. In WordPress code there are numerous pre-defined actions or hooks that allow developers to add their own code at these points. These are a part of what makes WordPress so extensible and most plugins depend on actions for their operation.

o give you an example of how they can be used to add code to a website let us assume that you want to add a copyright notice to your footer. In order to do this you could modify your footer template directly. In some cases this would be preferable but many times it is much easier, and better practice, to hook your code to a predefined action that is already being executed in the footer. To do this you can add your copyright code into a function in your functions.php file. You can then add this function to an action that is in the spot where you would like your copyright code to be executed.

       
function copyright_notice() {
   echo "Copyright All Rights Reserved";
}
add_action('wp_footer','copyright_notice');

In this example, copyright_notice is an action hooked into wp_footer hook. The function copyright_notice will be executed whenever the wp_footer() hook appears in a WordPress theme code.

Django Tutorial : Difference between Signal and Model

March 18, 2019 by admin

When I needed to implement some functionality on model saving, I always asked a question to myself – where to place it. In signal or in model method save()? Let’s see, what and when is more applicable.

When use model methods save(), delete()?

To my mind class methods are more usable, if logic concerns exclusively current model. For example, fill some field on model saving according to data from other fields.

Some people say, that signals are better because it is easy to reuse them. It seems strange to me because we can define a function or a mixin class and reuse it in method save() as well.

Generally, we can always use signals, so why I favor method save? Simple answer – it is more readable. When you are going through the model, you can easily understand, that something will happen on saving. In case of signals, especially if there is no rule where they are defined, the logic often come out of sight.

Keep in mind, that delete signals pre_delete, post_delete have some advantage over delete() method: they are called even on cascading delete and deleting a queryset. This is not happening with model method. In this situation make a decision according to context, maybe cascading delete is not so important.

On bulk creating and updating no code is executed: nor signal nor save(). So here they are equal.

And don’t forget to call parent’s method save() or delete() if you override them.

When use signals?

Signals are more applicable when you implement reusable applications. The users of your app can easily connect signals to their models without modifying the code of these models.

We can define a function or a mixin class for the same purpose. But agree, that attaching logic from some foreign app is more comfortable by using signals. Besides, if you decide to stop using the app, you will need to modify very small part of the project’s code.

The same is true when there are two (or more) apps within one project and you need to do something with one model when another model from the different application is being saved.

Imagine two applications, users and reports. When we create a user we need to create automatically a report for that user. In this case, I prefer to create a signal in reports application, since logic corresponds to it.

Why?

Firstly, we keep logic in the place where it belongs. Secondly, if for some reason we decide to delete the reports app entirely, we don’t even touch the users application.

Where define signals and were connect them?

As django docs (section “Where should this code live?”) suggests, define signals in separate submodule signals and not in models.py and __init__.py. This will save you from import problems.

To be sure the signals are connected, we need to execute the code that connects them on project launch. When we define them in models.py, we already get this. But now they are living in other place and it won’t run until we import it somewhere. Let’s do it in ready() method of application config class.

In general, I follow recommendations from this stackoverflow answer. Here is an example for users and reports applications, that I talked about earlier. We need to create a report on user creation.

1.Create submodule signals and place handlers.py in it

       
reports/signals/__init__.py
reports/signals/handlers.py

2.Define signals in that file handlers.py

       
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth import get_user_model

from reports.models import Report

User = get_user_model()

@receiver(post_save, sender=User)
def create_user_report(sender, instance, created, **kwargs):
    if created:
        Report.objects.create(user=instance)

3.Create application config class

       
reports/apps.py

with code:

       
from django.apps import AppConfig

class ReportsConfig(AppConfig):
    name = 'reports'
    verbose_name = 'Reports'

    def ready(self):
        import reports.signals.handlers  # noqa

And now our signal is connected. In this example I used decorator @receiver, so just import is enough. We also could call connect method of the signal explicitly here. It is a matter of taste.

Don’t forget to define our ReportsConfig class as config of the application. To do it, place this code in reports/__init__.py:

       
default_app_config = 'reports.apps.ReportsConfig'

Or place ReportsConfig in your settings.INSTALLED_APP

If follow these rules we will always know where to find signal handlers. And consequently no need to search the models module to find them.

Django Tutorial : queryset count cache

March 18, 2019 by admin

Django application makes several similar queries like ‘SELECT COUNT(*) ……’.As it turns out queryset.count() has not obvious cache. Let’s discuss in detail from the very beginning.

Continue Reading …

Django Tutorial: SQL Debugging

March 15, 2019 by admin

In django tests we can measure number of sql queries:

Continue Reading …

Django Tutorial : JSON in Django and PostgreSQL

March 15, 2019 by admin

New JSONField will be added in Django 1.9, it can be used with PostgreSQL >= 9.4. Let’s try to work with it and find out, in what situations it can be useful.

Continue Reading …

WordPress Tutorial : WordPress Site Optimization Step by Step Guide

March 14, 2019 by admin

In this article you will learn about how you can improve performance of your website.For wordpress site performance improvements you must have knowledge of wordpress, plugins, css, javascript. Now we will discuss in details about optimization.

Continue Reading …

Python : Dictionaries

March 14, 2019 by admin

In this Article you will learn about basics of python Dictionaries.

Python provides another composite data type called a dictionary, which is similar to a list in that it is a collection of objects. Continue Reading …

Python : Lists and Tuples in Python

March 13, 2019 by admin

In this article You’ll cover the important characteristics of lists and tuples.You’ll learn how to define them and how to manipulate them. When you’re finished, you should have a good feel for when and how to use these object types in a Python program.

Python Lists

In short, a list is a collection of arbitrary objects, somewhat akin to an array in many other programming languages but more flexible. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets ([]), as shown below:


>>> a = ['foo', 'bar', 'baz', 'qux']
>>> print(a)
['foo', 'bar', 'baz', 'qux']
>>> a
['foo', 'bar', 'baz', 'qux']

The important characteristics of Python lists are as follows:

  • Lists are ordered.
  • Lists can contain any arbitrary objects.
  • List elements can be accessed by index.
  • Lists can be nested to arbitrary depth.
  • Lists are mutable.
  • Lists are dynamic.

Each of these features is examined in more detail below.

Lists Are Ordered

A list is not merely a collection of objects. It is an ordered collection of objects. The order in which you specify the elements when you define a list is an innate characteristic of that list and is maintained for that list’s lifetime. (You will see a Python data type that is not ordered in the next tutorial on dictionaries.)

Lists that have the same elements in a different order are not the same:


>>> a = ['foo', 'bar', 'baz', 'qux']
>>> b = ['baz', 'qux', 'bar', 'foo']
>>> a == b
False
>>> a is b
False

>>> [1, 2, 3, 4] == [4, 1, 3, 2]
False

Lists Can Contain Arbitrary Objects

A list can contain any assortment of objects. The elements of a list can all be the same type:


>>> a = [2, 4, 6, 8]
>>> a
[2, 4, 6, 8]

Or the elements can be of varying types:


>>> a = [21.42, 'foobar', 3, 4, 'bark', False, 3.14159]
>>> a
[21.42, 'foobar', 3, 4, 'bark', False, 3.14159]

Lists can even contain complex objects, like functions, classes, and modules, which you will learn about in upcoming tutorials:


>>> int

>>> len

>>> def foo():
...     pass
...
>>> foo

>>> import math
>>> math


>>> a = [int, len, foo, math]
>>> a
[, , ,
]

A list can contain any number of objects, from zero to as many as your computer’s memory will allow:


>>> a = []
>>> a
[]

>>> a = [ 'foo' ]
>>> a
['foo']

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
... 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
... 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
... 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
... 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100]

(A list with a single object is sometimes referred to as a singleton list.)

List objects needn’t be unique. A given object can appear in a list multiple times:


>>> a = ['bark', 'meow', 'woof', 'bark', 'cheep', 'bark']
>>> a
['bark', 'meow', 'woof', 'bark', 'cheep', 'bark']

Lists Are Mutable

Most of the data types you have encountered so far have been atomic types. Integer or float objects, for example, are primitive units that can’t be further broken down. These types are immutable, meaning that they can’t be changed once they have been assigned. It doesn’t make much sense to think of changing the value of an integer. If you want a different integer, you just assign a different one.

By contrast, the string type is a composite type. Strings are reducible to smaller parts—the component characters. It might make sense to think of changing the characters in a string. But you can’t. In Python, strings are also immutable.

The list is the first mutable data type you have encountered. Once a list has been created, elements can be added, deleted, shifted, and moved around at will. Python provides a wide range of ways to modify lists.

Modifying a Single List Value

A single value in a list can be replaced by indexing and simple assignment:


>>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']
>>> a
['foo', 'bar', 'baz', 'qux', 'quux', 'corge']

>>> a[2] = 10
>>> a[-1] = 20
>>> a
['foo', 'bar', 10, 'qux', 'quux', 20]

You may recall from the tutorial Strings and Character Data in Python that you can’t do this with a string:


>>> s = 'foobarbaz'
>>> s[2] = 'x'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'str' object does not support item assignment

A list item can be deleted with the del command:


>>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']

>>> del a[3]
>>> a
['foo', 'bar', 'baz', 'quux', 'corge']

Python Tuples

Python provides another type that is an ordered collection of objects, called a tuple.

Pronunciation varies depending on whom you ask. Some pronounce it as though it were spelled “too-ple” (rhyming with “Mott the Hoople”), and others as though it were spelled “tup-ple” (rhyming with “supple”). My inclination is the latter, since it presumably derives from the same origin as “quintuple,” “sextuple,” “octuple,” and so on, and everyone I know pronounces these latter as though they rhymed with “supple.”

Defining and Using Tuples

Tuples are identical to lists in all respects, except for the following properties:

  • Tuples are defined by enclosing the elements in parentheses (()) instead of square brackets ([]).
  • Tuples are immutable.

Here is a short example showing a tuple definition, indexing, and slicing:


>>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')
>>> t
('foo', 'bar', 'baz', 'qux', 'quux', 'corge')

>>> t[0]
'foo'
>>> t[-1]
'corge'
>>> t[1::2]
('bar', 'qux', 'corge')

Never fear! Our favorite string and list reversal mechanism works for tuples as well:


>>> t[::-1]
('corge', 'quux', 'qux', 'baz', 'bar', 'foo')

Everything you’ve learned about lists—they are ordered, they can contain arbitrary objects, they can be indexed and sliced, they can be nested—is true of tuples as well. But they can’t be modified:


>>> t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')
>>> t[2] = 'Bark!'
Traceback (most recent call last):
  File "", line 1, in 
    t[2] = 'Bark!'
TypeError: 'tuple' object does not support item assignment

Why use a tuple instead of a list?

  • Program execution is faster when manipulating a tuple than it is for the equivalent list. (This is probably not going to be noticeable when the list or tuple is small.)
  • Sometimes you don’t want data to be modified. If the values in the collection are meant to remain constant for the life of the program, using a tuple instead of a list guards against accidental modification.
  • There is another Python data type that you will encounter shortly called a dictionary, which requires as one of its components a value that is of an immutable type. A tuple can be used for this purpose, whereas a list can’t be.

In a Python REPL session, you can display the values of several objects simultaneously by entering them directly at the >>> prompt, separated by commas:


>>> a = 'foo'
>>> b = 42
>>> a, 3.14159, b
('foo', 3.14159, 42)

Python displays the response in parentheses because it is implicitly interpreting the input as a tuple.

There is one peculiarity regarding tuple definition that you should be aware of. There is no ambiguity when defining an empty tuple, nor one with two or more elements. Python knows you are defining a tuple:


>>> t = ()
>>> type(t)



>>> t = (1, 2)
>>> type(t)

>>> t = (1, 2, 3, 4, 5)
>>> type(t)


But what happens when you try to define a tuple with one item:


>>> t = (2)
>>> type(t)


Doh! Since parentheses are also used to define operator precedence in expressions, Python evaluates the expression (2) as simply the integer 2 and creates an int object. To tell Python that you really want to define a singleton tuple, include a trailing comma (,) just before the closing parenthesis:


>>> t = (2,)
>>> type(t)

>>> t[0]
2
>>> t[-1]
2

You probably won’t need to define a singleton tuple often, but there has to be a way.

When you display a singleton tuple, Python includes the comma, to remind you that it’s a tuple:


>>> print(t)
(2,)

Tuple Assignment, Packing, and Unpacking

As you have already seen above, a literal tuple containing several items can be assigned to a single object:


>>> t = ('foo', 'bar', 'baz', 'qux')

When this occurs, it is as though the items in the tuple have been “packed” into the object:


>>> t
('foo', 'bar', 'baz', 'qux')
>>> t[0]
'foo'
>>> t[-1]
'qux'

If that “packed” object is subsequently assigned to a new tuple, the individual items are “unpacked” into the objects in the tuple:


>>> (s1, s2, s3, s4) = t
>>> s1
'foo'
>>> s2
'bar'
>>> s3
'baz'
>>> s4
'qux'

When unpacking, the number of variables on the left must match the number of values in the tuple:


>>> (s1, s2, s3) = t
Traceback (most recent call last):
  File "", line 1, in 
    (s1, s2, s3) = t
ValueError: too many values to unpack (expected 3)

>>> (s1, s2, s3, s4, s5) = t
Traceback (most recent call last):
  File "", line 1, in 
    (s1, s2, s3, s4, s5) = t
ValueError: not enough values to unpack (expected 5, got 4)

Packing and unpacking can be combined into one statement to make a compound assignment:


>>> (s1, s2, s3, s4) = ('foo', 'bar', 'baz', 'qux')
>>> s1
'foo'
>>> s2
'bar'
>>> s3
'baz'
>>> s4
'qux'

Again, the number of elements in the tuple on the left of the assignment must equal the number on the right:


>>> (s1, s2, s3, s4, s5) = ('foo', 'bar', 'baz', 'qux')
Traceback (most recent call last):
  File "", line 1, in 
    (s1, s2, s3, s4, s5) = ('foo', 'bar', 'baz', 'qux')
ValueError: not enough values to unpack (expected 5, got 4)

In assignments like this and a small handful of other situations, Python allows the parentheses that are usually used for denoting a tuple to be left out:


>>> t = 1, 2, 3
>>> t
(1, 2, 3)

>>> x1, x2, x3 = t
>>> x1, x2, x3
(1, 2, 3)

>>> x1, x2, x3 = 4, 5, 6
>>> x1, x2, x3
(4, 5, 6)

>>> t = 2,
>>> t
(2,)

It works the same whether the parentheses are included or not, so if you have any doubt as to whether they’re needed, go ahead and include them.

Tuple assignment allows for a curious bit of idiomatic Python. Frequently when programming, you have two variables whose values you need to swap. In most programming languages, it is necessary to store one of the values in a temporary variable while the swap occurs like this:


>>> a = 'foo'
>>> b = 'bar'
>>> a, b
('foo', 'bar')

>>># We need to define a temp variable to accomplish the swap.
>>> temp = a
>>> a = b
>>> b = temp

>>> a, b
('bar', 'foo')

In Python, the swap can be done with a single tuple assignment:


>>> a = 'foo'
>>> b = 'bar'
>>> a, b
('foo', 'bar')

>>># Magic time!
>>> a, b = b, a

>>> a, b
('bar', 'foo')

As anyone who has ever had to swap values using a temporary variable knows, being able to do it this way in Python is the pinnacle of modern technological achievement. It will never get better than this.

Django Tutorial : How to Unapply Migrations

March 1, 2019 by admin

In this article we will discuss the ways to unapply migrations in django.

Continue Reading …

Django Tutorial : Changing Models

February 27, 2019 by admin

In this article you will know about how you can change you models as per requirement of your application.

Continue Reading …

Django Tutorial : Creating Migrations

February 27, 2019 by admin

In This Article you will learn how you can create migrations in Django Continue Reading …

Django Tutorial : Passing View data to Template (HTML templates)

February 27, 2019 by admin

Templating in Django is one of the most powerful feature.Templates allow you to specify the structure of the output document. Templates are often used to create HTML documents
Continue Reading …

Django Tutorial : Querying Data From Models (Views.py)

February 27, 2019 by admin

Django Comes with simple Data query API for the database…

Continue Reading …

Django Tutorial : Creating Django Models (models.py)

February 26, 2019 by admin

Django Models are stored in model.py. Models are basically the database structure of the application. Continue Reading …

Django Tutorial : Request handling / Writing Django Views

February 26, 2019 by admin

In this article we will teach you to write the views in django.Views are the heart of the Django applications. They are responsible for the HTTP requests and HTTP responses. In between they perform some activities like database operations, rendering HTML templates, etc..

Continue Reading …

Django Tutorial : Defining URL and Sending request to the view

February 26, 2019 by admin

urls.py is a url mapper in Django project .The mapper called as urlpatterns defines different types of url patterns and corresponding view function. Continue Reading …

Django Tutorial : Django code structure

February 26, 2019 by admin

In this article you will learn in detail the Django code Structure and features. You will learn about Django URLs, MVC pattern and Templates.

Continue Reading …

Django Tutorial : Web Development with Django

February 26, 2019 by admin

In this Tutorial you are going to learn about Django which is python framework, we will cover basics of django in detail and then we will develop django Application.

Continue Reading …

Django Blog Tutorial – Step by step beginners guide for Django blog

February 25, 2019 by admin

In This tutorial we will guide you to the step by step blog development in Django, at this tep we are assuming that you have covered the previous chapters about Django setup.

Continue Reading …

Django Tutorial For Beginners – My First Application

February 22, 2019 by admin

This article will Teach you that how can you Write your First application in Django.
First you will need to setup Python/django you can read article How to Setup Django/python. After that Follow the steps below.

Continue Reading …

How to Setup Django/python

February 22, 2019 by admin

This article will guide you to how you can setup Python django. This article also explain how you can Setup Python Environments, Virtual Environment , Installing pip , Setup Django project and run ,By Follow easy Steps.

Continue Reading …

Asp.Net core 2.1 Web application using HTTPS

January 11, 2019 by admin

Normally configuring HTTPs properly on web project is a headache, we need lots of code on different places or need to handle through different tools. But in Asp.net Core (2.0 or higher) its super easy to configure. In fact, we don’t have to configure its already there we just need to understand.

Continue Reading …

How to Setup PHP Version in IIS Manager

January 3, 2019 by admin

This article will guide you to how you can setup PHP version in IIS Manager. This article also explain how you can update your Php version from previous version or you can degrade the php version.

Continue Reading …

How to Setup PHP in IIS Manager

January 1, 2019 by admin

This article will guide you to how you can setup PHP in IIS Manager.

Continue Reading …

How to install wordpress on IIS windows server

January 1, 2019 by admin

This article will guide you to how you can install or setup wordpress on IIS Manager windows based server.
Wordpress can be installed using PHP, IIS (internet information services) Manager , Mysql (phpmyadmin or MySQL Workbench ) database.

Continue Reading …