Using Custom Bulk Actions – Azioni di gruppo

I’m happy to tell you that in WordPress 4.7, developers can register their own bulk actions on list table screens.

custom-bulk-action-screenshot

Let’s walk through the steps required to add one.

An option in the dropdown

To add an option in the Bulk Actions dropdown HTML element, register a callback on the bulk_actions-{screen_id} filter that adds the new option onto the array. Replace {screen_id} with the ID of the admin screen to offer the bulk action on.

To add a bulk action “Email to Eric,” we could use the following code:

1
2
3
4
5
6
add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );
function register_my_bulk_actions($bulk_actions) {
  $bulk_actions['email_to_eric'] = __( 'Email to Eric', 'email_to_eric');
  return $bulk_actions;
}

Handling the form submission

To handle a bulk action form submission, register a callback on the handle_bulk_actions-{screen_id} filter for the corresponding screen. The filter expects the redirect URL to be modified, so be sure to modify the passed $redirect_url. This allows us to carry success or failure state into the next request to display a notice to the user. The other callback arguments will differ depending on the screen here to include contextually relevant data.

To add a bulk action handler for emailing the selected posts, we could use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );
function my_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
  if ( $doaction !== 'email_to_eric' ) {
    return $redirect_to;
  }
  foreach ( $post_ids as $post_id ) {
    // Perform action for each post.
  }
  $redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to );
  return $redirect_to;
}

Showing notices

We could use the existing notice hooks to let the user know what happened, depending on the state we set in the URL:

1
2
3
4
5
6
7
8
9
10
11
12
13
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );
function my_bulk_action_admin_notice() {
  if ( ! empty( $_REQUEST['bulk_emailed_posts'] ) ) {
    $emailed_count = intval( $_REQUEST['bulk_emailed_posts'] );
    printf( '<div id="message" class="updated fade">' .
      _n( 'Emailed %s post to Eric.',
        'Emailed %s posts to Eric.',
        $emailed_count,
        'email_to_eric'
      ) . '</div>', $emailed_count );
  }
}

For the curious, see the related changeset and Trac ticket.

How to migrate wordpress blog

This weekend I migrated my blog to a new vps, the article describes how to configure and install a blog based on wordpress with lighttpd and mysql. The reference platform is Debian Squeeze.

1. Backup your blog

From old vps,  stop Service

/etc/init.d/lighttpd stop
/etc/init.d/mysqld stop

Backup database

cd /root
mysql -u wp_admin -p wordpress > ./wordpress_db_backup.sql
tar -pczf wordpress_db_backup.sql.tar.gz ./wordpress_db_backup.sql

Backup content

tar -pczf sitebackup.tar.gz /var/www/yourwordpressblog/

2. Copy data from old vps

From new vps, transfer site backup

scp root@oldvps:/root/sitebackup.tar.gz ./

Transfer database backup

scp root@oldvps:/root/databasebackup.tar.gz ./

3. Install and Configure Mysql

Install MYSQL

apt-get install mysql-server mysql-client

Create Password for the Mysql user root

mysqladmin -u root password yourrootsqlpassword

Create database

mysqladmin -u root -p create wordpress

Set permission to user wp_admin on database with name “wordpress”

mysql -u root -p
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_admin'@'localhost' IDENTIFIED BY 'wp_admin_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_admin'@'localhost.localdomain' IDENTIFIED BY 'wp_admin_password';
FLUSH PRIVILEGES;
quit;

Extract dabase backup

tar xvfz wordpress_db_backup.sql.tar.gz

Restore database

mysql -u wp_admin -p wordpress < wordpress_db_backup.sql

4. Install and configure Lighttpd with php

Install lighttp

apt-get install lighttpd

Install php

apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Configure Lighttpd

ln -s /etc/lighttpd/conf-available/05-auth.conf /etc/lighttpd/conf-enabled/05-auth.conf
ln -s /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/10-fastcgi.conf
ln -s /etc/lighttpd/conf-available/10-simple-vhost.conf /etc/lighttpd/conf-enabled/10-simple-vhost.conf
ln -s /etc/lighttpd/conf-available/15-fastcgi-php.conf /etc/lighttpd/conf-enabled/15-fastcgi-php.conf

Edit /etc/lighttpd/lighttpd.conf and enable “mod_rewrite”

From this:

#	"mod_rewrite";

to this:

	"mod_rewrite"

Save and close.

Configure wordpress site on lighttpd
Edit /etc/lighttpd/conf-available/10-simple-vhost.conf

Insert:

## Your wordpress blog
$HTTP["host"] == "www.yourwordpressblog.com" {

        server.document-root = "/var/www/yourwordpressblog/"
        server.errorlog = "/var/log/lighttpd/yourwordpressblog/error.log"
        #accesslog.filename = "/var/log/lighttpd/yourwordpressblog/access.log"
        url.rewrite = (
                "^/(wp-admin|wp-includes|wp-content|download)/(.*)" => "$0",
                "^/(sitemap.xml|sitemap.xml.gz|robots.txt)" => "$0",
                "^/(.*.php)" => "$0",
                "^/(.*)$" => "/index.php/$1"
        )
        url.access-deny = ("wp-config.php")
}

#OPTIONAL: Secure rules to protect /wp-admin/
auth.backend = "htdigest"
auth.backend.htdigest.userfile = "/etc/lighttpd/.passwd"
#auth.debug = 2

auth.require = ( "/wp-admin/" =>
(
"method" => "digest",
"realm" => "Authorized users only",
"require" => "valid-user"
)
)

#OPTIONAL: Redirect rule in case of multiple domain
$HTTP["host"] =~ "www.yourwordpressblog.it|yourwordpressblog.it|www.yourwordpressblog.net|yourwordpressblog.net|www.yourwordpressblog.org|yourwordpressblog.org" {
  url.redirect = ( "^/(.*)" => "http://www.yourwordpressblog.com/$1" )
}

Save and close.

Create Log directory

mkdir /var/log/lighttpd/yourwordpressblog

Set Permission

chown www-data:www-data /var/log/lighttpd/yourwordpressblog

Optional step, to protect /wp-admin/ folder create “.passwd” file:
Install htdigest

apt-get install apache2-utils

Create “.passwd” file

htdigest -c /etc/lighttpd/.passwd ‘Authorized users only’ administrator

Exctract sitebackup.tar.gz

tar xvfz sitebackup.tar.gz

Move the content of the blog in /var/www/yourwordpressblog

mv ./sitebackup /var/www

Set permission:

chow -R www-data:www-data /var/www/yourwordpressblog

Restart lighttpd

/etc/init.d/lighttpd restart

Now the blog has been migrated to the new vps, comments are available to you if you have any suggestions.

NOTE:
If  permalink don’t work, and return error 404, check if rows below are there :

# BEGIN WordPress

<IfModule mod_rewrite.c>
ErrorDocument 404 /index.php?error=404
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Thanks,