Remove theme editor from WordPress setting menu

Many wordpress developers want to remove the theme editor option from wordpress appearance settings menu. Due to security reason removing theme editor from wordpress admin section is really great idea.

In this tutorial I given simple steps to remove theme editor option from your wordpress admin section.

You just need to copy and paste following code into your functions.php file. That will hide the theme editor menu from settings page.
Using following code you can easily remove editor.

function wpapi_remove_editor_menu() {
  remove_action('admin_menu', '_add_themes_utility_last', 101);
}

global $remove_submenu_page, $current_user;
get_currentuserinfo();
if($current_user->user_login == 'admin') { //Specify admin name here
    add_action('admin_menu', 'wpapi_remove_editor_menu', 1);
} 
Remove theme editor from WordPress setting menu
Remove theme editor from WordPress setting menu

How to make empty wordpress trash automatically

Sometimes we create pages and posts which are unwanted. In this article, I given code for make empty wordpress trash automatically. Using code deleted items will automatically delete from trash. Open your wp-config.php file which is in the root directory of wordpress installation.

How to make empty wordpress trash automatically

How to make empty wordpress trash automatically
How to make empty wordpress trash automatically
define('EMPTY_TRASH_DAYS', 7 );

Above code will delete the comments from trash section after every seven days.

Seven days are safe to keep trash. This is simple but good option to keep in your wordpress blog.

wordpress media settings full url path to files removed

As per changes of wordpress 3.5 “Full url path to files” which is media library setting is removed from wrodpress settings. If people want to set easily the upload path or subdomain for uploaded images they are not able set it very easily.

wordpress media settings full url path to files removed

wordpress media settings full url path to files removed
wordpress media settings full url path to files removed

If you want to set the subdomain for images in wordpress 3.5 then use following filter code in your functions.php file.


add_filter( 'pre_option_upload_url_path', 'subdomain_upload_url' );

function subdomain_upload_url()
{
 return 'http://subdomain.purabtech.in/uploads';
}

For more information you can see following URL

http://codex.wordpress.org/Editing_wp-config.php#Moving_uploads_folder

If still you want old settings then you can use following wordpress plugin:

WP Original Media Path

The new version of WordPress 3.5 has changed the media page, removing the two fields to define the location and the sub-field of media. There exists a constant wp-config.php, but it does not include sub-domains. This plug-in is designed for installations virgin WordPress. Once activated, the plugin will add two values ​​in the database, you modify later.

Hope your problem will be solved.

Display the authors in dropdown menu using wp_dropdown_users – Hook/Filter

One of my client faced issue with Autor drop down which is in Admin section.

Display the authors in dropdown menu using hooks.

While creating the New post there was problem with the Author field. There are hundreds of irrelevant selections (users) and it’s difficult to select the right one.

WordPress is by default showing all the users in author drop down. I don’t want to show the other users in author drop down.

Display the authors in a dropdown menu
Hook/Filter – In wordpress Admin -Add new Post section -Display the authors in a dropdown menu using wp_dropdown_users One of my client faced issue with Autor drop down which is in Admin section. Display the authors in a dropdown menu using hooks

I searched for wp_dropdown_users hook or filter. But I did not found any proper solution.

Following articles are found helpful to me.
http://wordpress.org/support/topic/filter-for-post-quick-edit-author-drop-down
http://codex.wordpress.org/Function_Reference/wp_dropdown_users

Using that code I modified the code and I am able to fix the issue. You can put following code in to functions.php file.

 /*
 * Hook for showing Admin and Author in Add new Post - Admin section dropdown menu
 */
function wpapi_override_wp_dropdown_users($output) {
    global $post, $user_ID;
    //get the Admin-role users IDs
    $admins = getUsersWithRole('admin');
    //get the author-role users IDs
    $authors = getUsersWithRole('author');
    //merge the array
    $result = array_merge($admins, $authors);

    //array converted into comma seprated string
    $authorsall = implode(",", $result);

    // return if this isn't the theme author override dropdown
    if (!preg_match('/post_author_override/', $output))
        return $output;

    // return if we've already replaced the list (end recursion)
    if (preg_match('/post_author_override_replaced/', $output))
        return $output;

    // replacement call to wp_dropdown_users
    $output = wp_dropdown_users(array(
        'echo' => 0,
        'name' => 'post_author_override_replaced',
        'selected' => empty($post->ID) ? $user_ID : $post->post_author,
        'include_selected' => true,
        'include' => $authorsall
    ));

    // put the original name back
    $output = preg_replace('/post_author_override_replaced/', 'post_author_override', $output);

    return $output;
}

add_filter('wp_dropdown_users', 'wpapi_override_wp_dropdown_users');

/*
 * Find User IDs by Role
 */

function getUsersWithRole($role) {
    $wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
    return $wp_user_search->get_results();
}

Using above code, you can load multiple role users in author drop down.

wordpress delete old post revisions improve site performance

wordpress tutorial, wordpress delete old post revisions improve site performance. We given sql commands and suggested some plugin for delete post revisions.

WordPress has auto save functionality. After some time wordpress creates the revision in database tables automatically. It is really useful. If something happens like powercut or browser crashes then you do not loose the your post data.

wordpress delete old post revisions improve site performance

SQL for delete post rivisions.



DELETE wp,wt,wpm FROM wp_posts wp LEFT JOIN wp_term_relationships wt ON (wp.ID = wt.object_id)
LEFT JOIN wp_postmeta wpm ON (wp.ID = wpm.post_id) WHERE wp.post_type = 'revision'

 
If you want to disable post revision system permanently, you can add following line in wp_config.php file.

define('WP_POST_REVISIONS', false);

 

But it has some bad issues also. If you have 5 to 10 revision per post and 200 posts then you will have at least 1500 to 2000 entries in table. Most important part is all revisions are unuseful. Each additional revision increase the size of your database thus making the processing time slower and slower.

But do not disable to auto save functionality and revision. you need to clean your db after certain time.

For cleaning the database you can use the following plugins.

  • 1. Optimize Database after Deleting Revisions –

Optimize Database after Deleting Revisions


This plugin is a ‘One Click’ WordPress Database Cleaner / Optimizer.

Main Features
Deletes redundant revisions of posts and pages (you optionally can keep an ‘x’-amount of the most recent revisions)
Deletes trashed posts, pages and comments (optional)
Deletes spammed comments (optional)
Deletes ‘orphan postmeta items’
Optimizes the database tables (optionally you can exclude certain tables from optimization)
Creates a log file of the optimizations (optional)
Optimization can be scheduled to automatically run once hourly, twice daily, once daily or once weekly at a specific time (optional)
‘Optimize DB (1 click)’ link in the admin bar (optional)
Settings
You can find the settings page in the WP Admin Panel » Settings » Optimize DB Options.

Starting the Optimization
You can start the Optimization in the WP Admin Panel » Tools » Optimize Database. Note: if you use the Scheduler the Optimization will run automatically!

  1. 2. Revision Removal – http://wordpress.org/plugins/revision-removal/

Revision Post is the new features of WordPress ver 2.6 and above. Every time you made a change to your post or page, it will automatic save add a revision, just like a draft. As many as you made change, the more revision you will have in your database. It will not only increase your database size, but also increase your load time as the server need more time to access the database.

Revision Manager is your choice to remove all the revisions quickly to help you to increase the speed of implementation of the SQL statement, and increase the speed of your server load.

wordpress delete old post revisions improve site performance
wordpress delete old post revisions improve site performance

how to disable cron job in wordpress

Before every post visit wordpress cron is running and taking too much bandwidth.  So we given trick and code to disable cron job in wordpress, which will disable wordpress cronjob.  if you have a lot of visitors it can be a problem. I am convinced this is the reason why my admin has become glacially slow and my site often crashes when making edits.

how to disable cron job in wordpress

Disable the wp-cron.php

you need to add the code to your wp-config.php file.


define('DISABLE_WP_CRON', 'true');

If you have shared hosting then add the following cron job


php -q wp-cron.php

If you have your own server then you need to add the cronjob in crontab


* */1 * * *wget https://purabtech.in/wp-cron.php

how to disable cron job in wordpress
how to disable cron job in wordpress

How to Add the social Bookmarks Icons in WordPress without using any plugins

All wordpress sites use the social bookmarks for sharing there content. For adding social bookmark they always use the wordpress plugins.
But that plugins will add some mysql quries and apache requests to your server. I recommend not to use these plugins.

How to Add the social Bookmarks Icons in WordPress without using any plugins

Just follow my steps:

1. Open single.php file from your wordpress theme.

just do one think, please assign class=”social_icons” to div.

<div >
<ul>

<ul>
	<li><a title="RSS" href="http://images.purabtech.in/feed" target="_blank" rel="nofollow"><img src="http://images.purabtech.in/rss_32.png" title="RSS" alt="RSS" /></a></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="del.icio.us"><img title="del.icio.us" src="http://images.purabtech.in/delicious_32.png" alt="del.icio.us" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="StumbleUpon"><img title="StumbleUpon" src="http://images.purabtech.in/stumbleupon_32.png" alt="StumbleUpon" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="Digg"><img title="Digg" src="http://images.purabtech.in/digg_32.png" alt="Digg" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>" title="TwitThis"><img title="TwitThis" src="http://images.purabtech.in/twitter_32.png" alt="TwitThis" /></li>
</ul>



<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="Mixx"><img title="Mixx" src="http://images.purabtech.in/mixx_32.png" alt="Mixx" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>" title="Technorati"><img title="Technorati" src="http://images.purabtech.in/technorati_32.png" alt="Technorati" /></li>
</ul>


<ul>
	<li>php?u=<!--?php the_permalink() ?-->&t=<!--?php echo urlencode(the_title('','', false)) ?-->" title="Facebook"><img title="Facebook" src="http://images.purabtech.in/facebook_32.png" alt="Facebook" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&h=<!--?php echo urlencode(the_title('','', false)) ?-->" title="NewsVine"><img title="NewsVine" src="http://images.purabtech.in/newsvine_32.png" alt="NewsVine" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="Reddit"><img title="Reddit" src="http://images.purabtech.in/reddit_32.png" alt="Reddit" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="Google"><img title="Google" src="http://images.purabtech.in/google_32.png" alt="Google" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->&source=<!--?php bloginfo('pingback_url'); ?-->&ary=<!--?php the_excerpt(); ?-->" title="LinkedIn"><img title="LinkedIn" src="http://images.purabtech.in/linkedin_32.png" alt="LinkedIn" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&title=<!--?php echo urlencode(the_title('','', false)) ?-->" title="co.mments"><img title="co.mments" src="http://images.purabtech.in/co.mments.gif" alt="co.mments" /></li>
</ul>


<ul>
	<li>php the_permalink() ?>&=<!--?php echo urlencode(the_title('','', false)) ?-->" title="YahooMyWeb"><img title="YahooMyWeb" src="http://images.purabtech.in/yahoobuzz_32.png" alt="YahooMyWeb" /></li>
</ul>



<ul>
	<li>php echo urlencode(the_title('','', false)) ?>&body=<!--?php the_permalink() ?-->" title="E-mail this story to a friend!"><img title="E-mail this story to a friend!" src="http://images.purabtech.in/email_32.png" alt="E-mail this story to a friend!" /></li>
</ul>

</ul>
</div>

Open style.css file from your theme folder and put following code:

.social_icons{ clear:both; }
.social_icons ul li {float:left; padding-right:8px;}
How to Add the social Bookmarks Icons in WordPress without using any plugins
How to Add the social Bookmarks Icons in WordPress without using any plugins

get posts vs new wp_query in wordpress theme

For getting the wordpress posts we use the following methods. In every wordpress theme we always use the get_posts() vs WP_Query vs query_posts() methods for creating the wordpress theme. We should know about wordpress methods first.

get posts vs new wp_query in wordpress theme

 

  • query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use ofpre_get_posts hook, for this purpose. TL;DRdon’t use query_posts() ever;
  • get_posts() is very similar in usage and accepts same arguments (with some nuances, like different defaults), but returns array of posts, doesn’t modify global variables and is safe to use anywhere;
  • WP_Query class powers both behind the scenes, but you can also create and work with own object of it. Bit more complex, fewer restrictions, also safe to use anywhere.

As per my opinion you should use the WP_Query() method. This is secure to use. If you have a choice, go with WP_Query. Both of the other functions (query_posts() andget_posts()) call WP_Query indirectly.

get posts vs new wp_query in wordpress theme
get posts vs new wp_query in wordpress theme

wordpress get post meta custom post type: Smart way

WordPress tutorial for, wordpress get post meta custom post type smartly. The get_post_meta method is very useful in wordpress. here is simple code snippet.

wordpress get post meta custom post type

If you want to shorthand the get_post_meta call for the current post then you can use following:
you can use above method through functions.php file. You need to put following code in file:

function get_current_post_meta($key,$postID=null){
if(empty($postID)) {
global $post;
return get_post_meta($post->ID,$key,true);
} else {
return get_post_meta($postID,$key,true);
}
}

and you can call it like this:

echo get_current_post_meta('custom_tags');

Using above method you can pass the post id also.

wordpress get post meta custom post type: Smart way
wordpress get post meta custom post type: Smart way

How to wordpress add javascript to page template

For many purpose and designs we use the page template in wordpress site. WordPress tutorial for, How to wordpress add javascript to page template. Some time we need to load the custom javascript to page template. Here is code for same.

How to wordpress add javascript to page template

For that we can use following code.

add_action('wp_enqueue_scripts','Load_Template_Scripts_wpapi');
function Load_Template_Scripts_wpapi(){
    if ( is_page_template('custom-page.php') ) {
        wp_enqueue_script('my-script', 'path/to/script.js');
    }
}

For loading any script or any custom code you can use above action in wordpress. Above code you need to add in functions.php file. Which you can find in your wordpress theme folder.

How to wordpress add javascript to page template
How to wordpress add javascript to page template