Wordpress Tutorials
- Wordpress
- Directory and file structure.
- Wordpress configuration
- Htaccess Configuration
- WP-CONTENT user playground
- Increase file size loading in phpmyadmin on localhost.
- Fatal error: Allowed memory size of x bytes
- wordpress Dashboard
- How to set local host domain or Virtual host
- Header, index and footer page.
- How to create wordpress Themes
- Html to wordpress themes
- Child Themes
- Increase Upload Max Filesize
- wp_enqueue_style & script
- How to make plugin
- Create Plugin
- Insert data into table using plugin.
- Show data from database
- Hook & Filter
- Template Or Custom page template
- Custom Login template
- How to show post on diffrent page.
- Custom widgit
- How to use contact form 7 plugin.
- Wordpress Database
- How to use Duplicator plugin
- How to fix Fatal Error: Maximum Execution Time Exceeded in WordPress
- How to debuggin mode on in wordpress
- Auto update plugin and themes
- How to Transfer WordPress
- How to change a WordPress site URL
- Top 10 issues in wordpress
- wordpress website link
Wordpress setting
Wordpress Function
wordpress top 10 problems
Important Links
What is hook?
Hooks allows you to do two things: change default functionality within WordPress and add your own functionality without modifying core WordPress files at all. Hooks are built into WordPress and are used to modify or add functionality to the core system.
in others words, A Hook is a generic term in WordPress that refers to places where you can add your own code or change what WordPress is doing or outputting by default. so that people can “hang” their own code on those hooks.
Types of hooks:
Two types of hooks in WordPress: actions and filters.
Action :-
An Action in WordPress is a hook that is triggered at specific time when WordPress is running and lets you take an action. This can include things like creating a widget when WordPress is initializing or sending a Tweet when someone publishes a post.
Actions allow you to add extra functionality at a specific point in the processing of the page—for example, you might want to add extra widgets or menus, or add a promotional message to your page.
Action hooks allow you to execute your custom functions which are referred to as actions. Action hooks allow you to add additional code to the WordPress core or theme so that you can achieve some new functionality or customizations.
Types of Action Function.
How to work ?
Syntax :-
add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );
File name : index.php
add_action( $hook, $function_to_add, $priority, $accepted_args );
The required parameters of the add_action function are the hook and function to add. The priority is an optional integer value based on a scale of 1 to 999 that determines the priority of order for functions tied to that specific hook. Higher priority means it runs later, lower priority means earlier. The last parameter is used less often and it is for when you need to pass or accept multiple arguments.
Action Hooks
Action hooks are designated points in the WordPress core, theme and plugin code where it is possible for outside resources to insert additional code and, there by, customise the code to do additional functions they may desire. An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between the
and tags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.To hook on to an action, create a function in your theme’s functions.php file (or in your plugin’s code) and hook it on using the add_action() function, as follows:
<?php
add_action( 'wp_head', 'my_hook_fun' );
function my_hook_fun() {
echo '<meta name="description" content="This is the meta description for this page." />';
echo "Hello add action hook";
}
?>
The above code adds the text "Hello add action hook" between your theme's
tags. Placing "wp_head" in the call to add_action() with "get_header" would display this text above your theme.
// Add some text after the header
add_action( '__after_header' , 'add_text' );
function add_text() {
// If we're not on the home page, do nothing
if ( !is_front_page() )
return;
// Echo the html
echo "<div>Special offer! June only: Free chocolate for everyone!</div>";
}<br>
function custom_register()
{
echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>';
}
add_action('wp_head','custom_register');
has_action() :-
When writing a WordPress plugin or theme, you may need to check if an action hook exists before running additional code.
Usage
<?php has_action( $tag, $function_to_check ) ?>
Parameters :-
The has_action function includes 2 parameters, on of them being required and the other optional. The following are the parameters that can be defined:
Determine if an action exists
if ( has_action('my_hook', 'hooked_action') ) {
// do something
}
do_action :-
do_action( string $tag, $arg = '' )
Execute functions hooked on a specific action hook.
This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter.
Parameters
do_action('mytheme_aboveblog');
function mytheme_online_user_message() {
global $current_user;
if (!empty($current_user)) {
$user_login = addslashes($current_user->user_login);
echo "Hello {$user_login}!";
}
}
add_action('mytheme_aboveblog', 'mytheme_online_user_message');
Now if the user is logged on it will run this function in that do_action place you put it and if the user is online inject that message into that place.
Summary
Placing a do_action() into your code allows other functions to hook into it to extend the functionality of where that hook is placed, using add_action().
*****************************************************************************************
Filter :-
A Filter in WordPress allows you get and modify WordPress data before it is sent to the database or the browser. Some examples of filters would include customizing how excerpts are displayed or adding some custom code to the end of a blog post.
Filters allow you to intercept and modify data as it is processed—for example, you might want to insert another CSS class in a WordPress HTML element, or modify some of your pages blocks.
Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data (such as adding it to the database or sending it to the browser screen). Filters sit between the database and the browser (when WordPress is generating pages), and between the browser and the database (when WordPress is adding new posts and comments to the database); most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering.
The basic steps to add your own filters to WordPress are:
File name : index.php
add_filter ( 'hook_name', 'your_filter', [priority], [accepted_args] );
where:
hook_name = name of the hook you want register to
your_filter = the name of your filter (or function)
priority = (optional) integer which represents the order your filter should be applied. If no value is added, it defaults to 10.
accepted_args = (optional) integer argument defining how many arguments your function can accept (default 1) because your filter must accpet at least one parameter which will be returned.
Filter hooks are another type of WordPress hook and these deal with the manipulation of text and other output.
Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data (such as adding it to the database or sending it to the browser screen). Filters sit between the database and the browser (when WordPress is generating pages), and between the browser and the database (when WordPress is adding new posts and comments to the database); most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering.
<?php
add_filter( 'wp_title', 'myfilter_fun', 10, 2 );
myfilter_fun( $title, $sep ) {
/* Retrieve site name. */
$name = get_bloginfo( 'name' );
/* Append site name to $title. */
$title .= $sep . ' ' . $name;
/* Return the title. */
return $title;
}
?>