We can block the user login by the Role. This will work before the user name and password validation. So we can block the user before the validation for this we need to assign the user to particular role in this I used the "blocked" role which is custom one you can make any role to get blocked.
Sample code to block the user by role.Just paste the
following code in either plugin files or in the functions.php ( theme
file )
add_filter('wp_authenticate_user', 'custom_block_user_role_login', 10, 2);
if(!function_exists('custom_block_user_role_login')) {
function custom_block_user_role_login($user, $password) {
$role = $user->wp_capabilities;
/*
* In below code "blocked" is the user role you can assign it to another role
* which you want to get blocked
*/
if(array_key_exists('blocked', $role)) {
return new WP_Error('block_user', __('You are Blocked.'));
}
return $user;
}
}
Showing posts with label add_filter. Show all posts
Showing posts with label add_filter. Show all posts
Monday, 1 July 2013
Friday, 7 June 2013
Conditional Stylesheet
By default wp_enqueue_style function does not provide any option to include stylesheet based on browser but we can do it with the help of $wp_styles global variable.
add_filter( 'wp_enqueue_scripts', 'custom_enqueue_scripts' );
if(!function_exists('custom_enqueue_scripts')) {
function custom_enqueue_scripts() {
wp_enqueue_style( 'conditionalstyle', get_stylesheet_directory_uri().'/css/conditional.css' );
global $wp_styles;
$wp_styles->registered['conditionalstyle']->add_data( 'conditional', 'lt IE 8' );
}
}
add_filter( 'wp_enqueue_scripts', 'custom_enqueue_scripts' );
if(!function_exists('custom_enqueue_scripts')) {
function custom_enqueue_scripts() {
wp_enqueue_style( 'conditionalstyle', get_stylesheet_directory_uri().'/css/conditional.css' );
global $wp_styles;
$wp_styles->registered['conditionalstyle']->add_data( 'conditional', 'lt IE 8' );
}
}
Thursday, 30 May 2013
Prevent plugins from update
We can prevent certain plugins from updating themselves or asking to be updated. It can be done using the http_request_args filter
if(!function_exists('custom_prevent_plugin_update_check')){
function custom_prevent_plugin_update_check( $r, $url ) {
if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
$to_be_blocked = array('plugin-folder-name/plugin-filename.php');
/*Example: array('contact-form-7/wp-contact-form-7.php'). It will prevent contact-form-7 for update check.*/
/*We can block multiple plugins by specifying their names in this array as above. */
if (!empty($to_be_blocked)){
foreach ($to_be_blocked as $individual){
$plugins = unserialize( $r['body']['plugins'] );
unset( $plugins->plugins[$individual] );
unset( $plugins->active[array_search( $individual,
$plugins->active )] );
$r['body']['plugins'] = serialize( $plugins );
}
}
}
return $r;
}
}
add_filter( 'http_request_args', 'custom_prevent_plugin_update_check',10, 2 );
if(!function_exists('custom_prevent_plugin_update_check')){
function custom_prevent_plugin_update_check( $r, $url ) {
if ( 0 === strpos( $url, 'http://api.wordpress.org/plugins/update-check/' ) ) {
$to_be_blocked = array('plugin-folder-name/plugin-filename.php');
/*Example: array('contact-form-7/wp-contact-form-7.php'). It will prevent contact-form-7 for update check.*/
/*We can block multiple plugins by specifying their names in this array as above. */
if (!empty($to_be_blocked)){
foreach ($to_be_blocked as $individual){
$plugins = unserialize( $r['body']['plugins'] );
unset( $plugins->plugins[$individual] );
unset( $plugins->active[array_search( $individual,
$plugins->active )] );
$r['body']['plugins'] = serialize( $plugins );
}
}
}
return $r;
}
}
add_filter( 'http_request_args', 'custom_prevent_plugin_update_check',10, 2 );
Wednesday, 15 May 2013
Custom fields in user profile page
We can add custom fields to the user profile page as like the post types.
user_contactmethods filter will do the trick
if (!function_exists('custom_user_contactmethods')) {
function custom_user_contactmethods($user_contactmethods) {
return array_merge($user_contactmethods, array(
'address1' => __('Address Line1')
/*We can add multiple fields in this array.*/
)
);
}
}
add_filter('user_contactmethods', 'custom_user_contactmethods', 5, 1);
Note: The fields added through this filter will get appeared only while editing the user's profile page. It wont be available while creating a user.
user_contactmethods filter will do the trick
if (!function_exists('custom_user_contactmethods')) {
function custom_user_contactmethods($user_contactmethods) {
return array_merge($user_contactmethods, array(
'address1' => __('Address Line1')
/*We can add multiple fields in this array.*/
)
);
}
}
add_filter('user_contactmethods', 'custom_user_contactmethods', 5, 1);
Note: The fields added through this filter will get appeared only while editing the user's profile page. It wont be available while creating a user.
Wednesday, 8 May 2013
Admin bar for non logged users
Its possible to display the admin bar for the non-logged-in users it can be easily done by using the show_admin_bar filter and admin_bar_menu hook.
Just paste the following code in either plugin files or in the functions.php ( theme file ).
if(!function_exists('custom_login_adminbar')){
function custom_login_adminbar( $wp_admin_bar) {
if ( !is_user_logged_in() )
$wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
/*Here you can add your own menus*/
}
}
add_action( 'admin_bar_menu', 'custom_login_adminbar' );
add_filter( 'show_admin_bar', '__return_true' , 1000 );
By default the admin bar will come with wordpress documentation link and search box. You can remove the admin bar items by using wp_before_admin_bar_render filter. Following code will remove the wordpress documentation link for non-logged-in user.
Just paste the following code in either plugin files or in the functions.php ( theme file ).
if(!function_exists('custom_admin_bar_render')) {
function custom_admin_bar_render() {
if ( !is_user_logged_in() ) {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
}
}
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_render' );
Just paste the following code in either plugin files or in the functions.php ( theme file ).
if(!function_exists('custom_login_adminbar')){
function custom_login_adminbar( $wp_admin_bar) {
if ( !is_user_logged_in() )
$wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
/*Here you can add your own menus*/
}
}
add_action( 'admin_bar_menu', 'custom_login_adminbar' );
add_filter( 'show_admin_bar', '__return_true' , 1000 );
By default the admin bar will come with wordpress documentation link and search box. You can remove the admin bar items by using wp_before_admin_bar_render filter. Following code will remove the wordpress documentation link for non-logged-in user.
Just paste the following code in either plugin files or in the functions.php ( theme file ).
if(!function_exists('custom_admin_bar_render')) {
function custom_admin_bar_render() {
if ( !is_user_logged_in() ) {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
}
}
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_render' );
Tuesday, 7 May 2013
Adding Log in Log out link to menu
We can add the Log in / Log out link to the menu assigned to the particular Theme Location by using wp_nav_menu_items filter.
Just paste the following code in either plugin files or in the functions.php ( theme file ).
add_filter('wp_nav_menu_items', 'custom_add_login_logout_link', 10, 2);
if(!function_exists('custom_add_login_logout_link')){
function custom_add_login_logout_link($items, $args) {
$menu_name = 'primary'; /*Here primary specifies the Theme Location in which your menu is assigned*/
if ( ! is_user_logged_in() )
$link = '<a href="' . esc_url( wp_login_url() ) . '">Log In</a>';
else
$link = '<a href="' . esc_url( wp_logout_url() ) . '">Log Out</a>';
if ( ($menu_name) && ($args->theme_location == $menu_name) )
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page">'. $link .'</li>';
return $items;
}
}
Just paste the following code in either plugin files or in the functions.php ( theme file ).
add_filter('wp_nav_menu_items', 'custom_add_login_logout_link', 10, 2);
if(!function_exists('custom_add_login_logout_link')){
function custom_add_login_logout_link($items, $args) {
$menu_name = 'primary'; /*Here primary specifies the Theme Location in which your menu is assigned*/
if ( ! is_user_logged_in() )
$link = '<a href="' . esc_url( wp_login_url() ) . '">Log In</a>';
else
$link = '<a href="' . esc_url( wp_logout_url() ) . '">Log Out</a>';
if ( ($menu_name) && ($args->theme_location == $menu_name) )
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page">'. $link .'</li>';
return $items;
}
}
Thursday, 18 April 2013
Blank search issue in wordpress
In wordpress default search form the blank search will redirects to home page. We can use request filter to the trick. Following code can be used to redirect the blank search to search page.
Just paste the following code in either plugin files or in the functions.php ( theme file ).
Just paste the following code in either plugin files or in the functions.php ( theme file ).
add_filter( 'request', 'blank_search_fix' );
if(!function_exists('blank_search_fix')){
function blank_search_fix( $query_vars ) {
if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
$query_vars['s'] = " ";
}
return $query_vars;
}
}in this code $_GET['s'] denotes the search field in the (search)form
<input type="text" name="s" id="s" value="<?php echo trim( get_search_query() ); ?>" />Friday, 1 March 2013
Custom arguments to add_filter
We can pass custom arguments to filters when using add_filter.
For example plugin needs to pass the order dynamically it can be done by using global variables.Sample code to change the order by using posts_orderby_request filter
.
For example plugin needs to pass the order dynamically it can be done by using global variables.Sample code to change the order by using posts_orderby_request filter
.
global $corder;
$corder = 'asc';
add_filter( 'posts_orderby_request',function(){
global $corder;
$val = " wp_posts.post_title $corder";
return $val;
}
);
Subscribe to:
Posts (Atom)