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' );
}
}
Friday, 7 June 2013
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 );
Friday, 17 May 2013
Custom validation in user profile page
We can perform custom validation in user profile page. We can perform the validation through user_profile_update_errors hook.
if (!function_exists('custom_user_contact_validate')) {
function custom_user_contact_validate(&$errors, $update = null, &$user = null) {
if (!empty($_POST["action"]) && ($_POST["action"] == "adduser" || $_POST["action"] == "createuser"))
return $errors;
if (empty($_REQUEST['first_name'])) {
$errors->add('Invalid Data', '<strong>ERROR</strong>: Please Enter your First Name');
}
/*Here the field first name has been validated*/
}
}
add_action('user_profile_update_errors', 'custom_user_contact_validate', 5);
Note: The validation added through this Hook will works while editing the user's profile page. Fields wont be validated while creating a user.
if (!function_exists('custom_user_contact_validate')) {
function custom_user_contact_validate(&$errors, $update = null, &$user = null) {
if (!empty($_POST["action"]) && ($_POST["action"] == "adduser" || $_POST["action"] == "createuser"))
return $errors;
if (empty($_REQUEST['first_name'])) {
$errors->add('Invalid Data', '<strong>ERROR</strong>: Please Enter your First Name');
}
/*Here the field first name has been validated*/
}
}
add_action('user_profile_update_errors', 'custom_user_contact_validate', 5);
Note: The validation added through this Hook will works while editing the user's profile page. Fields wont be validated while creating a user.
Thursday, 16 May 2013
Custom menu in admin bar
Our plugins may need to add a menu item for pointing to settings page in admin bar. wp_before_admin_bar_render filter helps us to do that.
if(!function_exists('custom_admin_bar_menu')){
function custom_admin_bar_menu() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'id' => 'my_settingspage',
'title' => __('My settings'),
'href' => admin_url('/plugingsettingspage.php')
) );
}
}
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_menu' );
We can also make it as child to existing menu by adding the menu-id as a parent to that menu
if(!function_exists('custom_admin_bar_menu')){
function custom_admin_bar_menu() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'id' => 'my_settingspage',
'title' => __('My settings'),
'href' => admin_url('/plugingsettingspage.php')
) );
}
}
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_menu' );
We can also make it as child to existing menu by adding the menu-id as a parent to that menu
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.
Monday, 13 May 2013
Get current user role
Its easy to get the currently logged-in user role by using the global variable $current_user. Following function can be used to get the current user's role.
if(!function_exists('custom_get_current_user_role')) {
function custom_get_current_user_role(){
if(is_user_logged_in()) {
global $current_user;
$user_role = $current_user->roles[0];
return $user_role;
}
return FALSE;
}
}
if(!function_exists('custom_get_current_user_role')) {
function custom_get_current_user_role(){
if(is_user_logged_in()) {
global $current_user;
$user_role = $current_user->roles[0];
return $user_role;
}
return FALSE;
}
}
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() ); ?>" />Thursday, 14 March 2013
Renaming Admin menu
Renaming the admin menu/submenu items is simple by using admin_head filter. Not only that, we can also change the Link of the menu/submenu items.
Consider the sample code to renaming the Labels. Just paste the following code in either plugin files or in the functions.php ( theme file ).
if(!function_exists('custom_admin_menu_labelrename')){
function custom_admin_menu_labelrename(){
global $submenu, $menu;
/* we can get index of the menu by using this code print_r($menu);/print_r($submenu)*/
$menu[2][0] = 'Overview'; /*Here I just changed the Dashboard label to Overview*/
$submenu['index.php'][0][2] = 'http://examplesite.com';/* Custom link to Home (submenu of Dashboard Menu) menu*/
$submenu['index.php'][0][0] = 'My Home'; /*Home (submenu of Dashboard Menu)label to My Home*/
}
}
add_filter( 'admin_head', 'custom_admin_menu_labelrename' );
Consider the sample code to renaming the Labels. Just paste the following code in either plugin files or in the functions.php ( theme file ).
if(!function_exists('custom_admin_menu_labelrename')){
function custom_admin_menu_labelrename(){
global $submenu, $menu;
/* we can get index of the menu by using this code print_r($menu);/print_r($submenu)*/
$menu[2][0] = 'Overview'; /*Here I just changed the Dashboard label to Overview*/
$submenu['index.php'][0][2] = 'http://examplesite.com';/* Custom link to Home (submenu of Dashboard Menu) menu*/
$submenu['index.php'][0][0] = 'My Home'; /*Home (submenu of Dashboard Menu)label to My Home*/
}
}
add_filter( 'admin_head', 'custom_admin_menu_labelrename' );
Subscribe to:
Posts (Atom)