Friday, 30 September 2011

How to prevent update checks for installed plug-ins in wordpress?

Plug-in is the one which is used to add some additional functionality to the blog. We have many free plug-in in the repository to extend the functionality.

If we get any plug-in, for our requirement we can use it directly or else we will customize the plug-in according to our requirements.

Each time the wordpress will check for the updates in the repository by using the function wp_update_plugins( ) in wp_includes/update.php. If there is any new version available we will get a notification at wordpress plugin dashboard.

If anyone clicks on update the plug-in will be updated to latest version, then the changes what we done will be lost and again we have to update the customizations. There is no hook to avoid this problem.

I faced this problem for one of my website. Each time, when the notification comes for the plug-in my client will update it.

To avoid this problem I added the following code to the plug-in for which I want to exclude from update check.

add_filter( 'http_request_args', 'prevent_update_check', 10, 2 );
function  prevent_update_check( $r, $url ) {
    if ( 0 === strpos( $url, '' ) ) {
        $my_plugin = plugin_basename( __FILE__ );
        $plugins = unserialize( $r['body']['plugins'] );
        unset( $plugins->plugins[$my_plugin] );
        unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
        $r['body']['plugins'] = serialize( $plugins );
    return $r;

Tuesday, 27 September 2011

How to avoid the “XML Parsing Error: XML or text declaration not at start of entity” in wordpress feed?

Web feed or news feed is a format which is to represent the updated data for the users or subscribers. Wordpress has in built functionality for the generation of rss feeds for website.

Sometimes while accessing the rss feed it will throw a parsing error i.e

XML Parsing Error: XML or text declaration not at start of entity.

This error will cause if there is a blank space in the theme files or in the core files of wordpress.
To avoid this error remove the blank spaces before or after the <?php ?> tags  or add the following code.

We have different plug-ins to check the spaces in the files and to remove ,but the best solution is to add the following code. I resolved the problem is like this only. Adding a piece of code is better than using one more plugin for a website.Its better to use very less plug-ins for a website.

$out = ob_get_contents();  
$out = str_replace(array("\n", "\r", "\t", " "), "", $input);

Add this code in the following files ,located root directory of your  wordpress site.


Thursday, 15 September 2011

How to get post ancestors of a post?

In wordpress we have an option to assign a post as a child to another post. In order to get the ancestors of a post wordpress have an inbuilt function i.e.  get_post_ancestors( ).

This function will have one argument that can be either post id or post object and it returns the ancestor’s ids as an array. 


Tuesday, 13 September 2011

How to Activate or Deactivate wordpress plug-in programmatically?

Recently I had a problem with wordpress plug-in display at the dashboard. I searched on the web but did not find a proper solution. At that time I got a requirement to install one plug-in, I uploaded the plug-in from ftp and the uploaded plug-in is not displayed in the dashboard to activate.

For this I tried the following hook and done the activation of the plug-in.

function activate_plugin() {    
         // Absolute path to your specific plugin
$my_plugin = WP_PLUGIN_DIR.'/wp-email/wp-email.php';
 // Here wp-email is the plug-in which i want to activate. Replace it with the plug-in which you want to activate.
    // Check to see if plugin is already active
    if(is_plugin_active($my_plugin)) {
        // Deactivate plugin
        // Note that deactivate_plugins() will also take an
        // array of plugin paths as a parameter instead of
        // just a single string.
    else {
        // Activate plugin

Add the above code to the functions.php of your current theme .Once the activation is done comment the code, because we are activating the plug-in if It is in deactivated state and we are deactivating the plug-in if it is active state. So if we did not remove the code the plug-in will be deactivated again.
If you don’t need to deactivation functionality just comment the following line and no need of commenting the whole function
This code made my work easy that’s why I am sharing it , so it may be useful for someone.