wordpress

How to run cron and execute function inside plugin in WordPress 3

28 Dec , 2011  

Running cronjobs in WordPress is easy but seem there are lack of documentation that talking about this. Especially, if we want to running our custom plugin in WordPress from our cronjobs. Actually, there are two methods of running cron in WordPress. First, it’s running from loaded pages. Seconds, it’s running through cron UNIX servers (wget, lynx, or somethings).

For instance, I have plugin called “montage”, so I will defined scheduler event here :
More…

,

wordpress

Download image and attach into post programmatically in WordPress 3

24 Dec , 2011  

All we know, we can use wp_insert_post() to create content programmatically in WordPress. But, there more magic goodies bag in WordPress. We can download images and attach into post programmatically by using media_sideload_image(). There are several issue while using this function because lack of documentation about how to implement it.

The common error when using media_sideload_image() :
1. It generate blank page with errors.
2. Undefined function media_sideload_image()
3. Undefined function download_url()
More…

,

wordpress

How to create custom table from plugin in WordPress 3

27 Nov , 2011  

When creating plugin, we need to create a custom table. We will use register hook activation, de-activation and uninstall to handle database creation and removal. Now, we start with database created automatically when plugin installed / activated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Register installation into hook */
register_activation_hook(__FILE__, 'your_plugin_install');

/* Install montage SQL into database */
function your_plugin_install() {
    global $wpdb;
    $table_name = $wpdb->prefix.'your-table';

    $sql = "CREATE TABLE IF NOT EXISTS ". $table_name . " (
            id int(11) NOT NULL AUTO_INCREMENT,
            name varchar(255) binary DEFAULT NULL,
            PRIMARY KEY (id)
            ) TYPE=MyISAM AUTO_INCREMENT=1;
    ";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

More…

,

wordpress

Showing WordPress 3 query database error

22 Nov , 2011  

When you building plugin or themes, sometimes you need to contact with database ( select, update, insert or delete ). Somehow after executing database, there no result and no changes in database. That mean some problem with your code. You should check it with showing query error in WordPress by :

1
$wpdb->show_errors();

For example :

1
2
$wpdb->show_errors();
$wpdb->insert($wpdb->preffix.'table', $result);

This will show what error occured in inserting data.

Simple and straight-foward! :)

,

wordpress

How to load Zend Framework in WordPress 3 plugin

12 Nov , 2011  

Loading Zend Framework into WordPress 3 plugin is quite easy. You can download latest ZendFramework on http://framework.zend.com/download/latest . To use Zend Framework with our plugin, extract it on inside plugin folder.

1
2
3
4
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) );

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

,

wordpress

How to load Zend Framework into PHP files

11 Nov , 2011  

Zend Framework provide a lot of interesting libraries. Instead re-invent the wheel, using Zend Framework libraries will boost our development time. You can download latest ZendFramework on http://framework.zend.com/download/latest . To use Zend Framework with our system like individual php files, CMS or Framework, we should configure our path.

This is scheme of folders and extracted Zend Framework:

1
2
3
Project Folder
   |__ Zend
   |__ my.php

More…

,

wordpress

How to create WordPress 3 Custom Plugin for dummies

10 Nov , 2011  

Creating custom plugin in WordPress 3.2 is easy and it will fast if we do on the right way. So, what we need to know as newbie / n00b to build a custom wordpress plugin ? First, you should read : http://codex.wordpress.org/Writing_a_Plugin.

Now you have insight to build a plugin on WordPress. I will guide you to create wordpress plugin that follow WordPress Guidelines. We will create plugin called “writter”.

Create folder on “/wp-content/plugins” called “writter”. Go to folder and create file called “writter.php”

1
2
cd your-wordpress-path/wp-content/plugins/
mkdir writter

Now we ready to create plugins in WordPress 3 :

1. Standar plugin Information
We start create a standar plugin information on writter.php.
More…

,

wordpress

Configuring WordPress on Localhost NGINX sub-folder

21 Oct , 2011  

Using WordPress on localhost with sub-folder in Apache is very easy. But using NGINX, it will little bit differents. Usually, we use custom domain like http://wordpress, register domain into “/etc/hosts” and define into NGINX sites-available.

But, this is not effective way. We like to access our wordpress like Apache. Access WordPress from localhost like http://localhost/wordpress will ease our development. Here are steps to do :

1. Configure /etc/nginx/sites-available/default

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;
    # server_name_in_redirect off;
       
    location / {
         autoindex on;
         # This is cool because no php is touched for static content
         try_files $uri $uri/ /index.php;
    }

    location ~ .php$ {                                      
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }
}

More…

,

wordpress

Upgrade WordPress using command-line in 10 seconds

21 Oct , 2011  

WordPress always release their engine most in every 2 month or less. Using command-line will make upgrading wordpress done in 10 seconds.

I assume you have latest version of wordpress in folder called “wordpress”. And i have “wordpress-old” as my old wordpress. Make latest and old on same level folder and do this :

1. Deleting Old WordPress files

1
2
rm -rf wordpress-old/wp-includes
rm -rf wordpress-old/wp-admin/

2. Copy New WordPress into Old files

1
2
3
cp -dR wordpress/wp-includes wordpress-old/
cp -dR wordpress/wp-admin wordpress-old/
cp -dR wordpress/wp-content wordpress-old/

3. Check wp-config and compare if there new feature to be added.

4. Login into admin and do “Update Database”

Done! :D

,

wordpress

How to fix memcached problem in WordPress 3

10 Aug , 2011  

When using memcached plugin in WordPress 3 and multiple sites used same memcached server, all sites seems have wrong redirection. Thought it was my firefox cache problem but it doesn’t. This is because bug in object-cache.php. Mohanjit give solution for this problem by patching them.

Here are object-cache.php that you should put into wp-content :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php

/*
Name: Memcached
Description: Memcached backend for the WP Object Cache.
Version: 2.0
URI: http://dev.wp-plugins.org/browser/memcached/
Author: Ryan Boren


Install this file to wp-content/object-cache.php
*/

function wp_cache_add($key, $data, $flag = '', $expire = 0) {
    global $wp_object_cache;

    return $wp_object_cache->add($key, $data, $flag, $expire);
}

function wp_cache_incr($key, $n = 1, $flag = '') {
    global $wp_object_cache;

    return $wp_object_cache->incr($key, $n, $flag);
}

function wp_cache_decr($key, $n = 1, $flag = '') {
    global $wp_object_cache;

    return $wp_object_cache->decr($key, $n, $flag);
}

function wp_cache_close() {
    global $wp_object_cache;

    return $wp_object_cache->close();
}

function wp_cache_delete($id, $flag = '') {
    global $wp_object_cache;

    return $wp_object_cache->delete($id, $flag);
}

function wp_cache_flush() {
    global $wp_object_cache;

    return $wp_object_cache->flush();
}

function wp_cache_get($id, $flag = '') {
    global $wp_object_cache;

    return $wp_object_cache->get($id, $flag);
}

function wp_cache_init() {
    global $wp_object_cache;

    $wp_object_cache = new WP_Object_Cache();
}

function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
    global $wp_object_cache;

    return $wp_object_cache->replace($key, $data, $flag, $expire);
}

function wp_cache_set($key, $data, $flag = '', $expire = 0) {
    global $wp_object_cache;

    if ( defined('WP_INSTALLING') == false )
        return $wp_object_cache->set($key, $data, $flag, $expire);
    else
        return true;
}

function wp_cache_add_global_groups( $groups ) {
    global $wp_object_cache;

    $wp_object_cache->add_global_groups($groups);
}

function wp_cache_add_non_persistent_groups( $groups ) {
    global $wp_object_cache;

    $wp_object_cache->add_non_persistent_groups($groups);
}

class WP_Object_Cache {
    var $global_groups = array ('users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss');

    var $no_mc_groups = array( 'comment', 'counts' );

    var $autoload_groups = array ('options');

    var $cache = array();
    var $mc = array();
    var $stats = array();
    var $group_ops = array();

    var $cache_enabled = true;
    var $default_expiration = 0;

    function add($id, $data, $group = 'default', $expire = 0) {
        $key = $this->key($id, $group);

        if ( in_array($group, $this->no_mc_groups) ) {
            $this->cache[$key] = $data;
            return true;
        }

        $mc =& $this->get_mc($group);
        $expire = ($expire == 0) ? $this->default_expiration : $expire;
        $result = $mc->add($key, $data, false, $expire);
        @ ++$this->stats['add'];
        $this->group_ops[$group][] = "add $id";

        if ( false !== $result )
            $this->cache[$key] = $data;
        return $result;
    }

    function add_global_groups($groups) {
        if ( ! is_array($groups) )
            $groups = (array) $groups;

        $this->global_groups = array_merge($this->global_groups, $groups);
        $this->global_groups = array_unique($this->global_groups);
    }

    function add_non_persistent_groups($groups) {
        if ( ! is_array($groups) )
            $groups = (array) $groups;

        $this->no_mc_groups = array_merge($this->no_mc_groups, $groups);
        $this->no_mc_groups = array_unique($this->no_mc_groups);
    }

    function incr($id, $n, $group) {
        $key = $this->key($id, $group);
        $mc =& $this->get_mc($group);

        return $mc->increment($key, $n);
    }

    function decr($id, $n, $group) {
        $key = $this->key($id, $group);
        $mc =& $this->get_mc($group);

        return $mc->decrement($key, $n);
    }

    function close() {

        foreach ( $this->mc as $bucket => $mc )
            $mc->close();
    }

    function delete($id, $group = 'default') {
        $key = $this->key($id, $group);

        if ( in_array($group, $this->no_mc_groups) ) {
            unset($this->cache[$key]);
            return true;
        }

        $mc =& $this->get_mc($group);

        $result = $mc->delete($key);

        @ ++$this->stats['delete'];
        $this->group_ops[$group][] = "delete $id";

        if ( false !== $result )
            unset($this->cache[$key]);

        return $result;
    }

    function flush() {
        return true;
    }

    function get($id, $group = 'default') {
        $key = $this->key($id, $group);
        $mc =& $this->get_mc($group);

        if ( isset($this->cache[$key]) )
            $value = $this->cache[$key];
        else if ( in_array($group, $this->no_mc_groups) )
            $value = false;
        else
            $value = $mc->get($key);

        @ ++$this->stats['get'];
        $this->group_ops[$group][] = "get $id";

        if ( NULL === $value )
            $value = false;

        $this->cache[$key] = $value;

        if ( 'checkthedatabaseplease' == $value )
            $value = false;

        return $value;
    }

    function get_multi( $groups ) {
    /*
    format: $get['group-name'] = array( 'key1', 'key2' );
    */
    $return = array();
        foreach ( $groups as $group => $ids ) {
            $mc =& $this->get_mc($group);
            foreach ( $ids as $id ) {
                $key = $this->key($id, $group);
                if ( isset($this->cache[$key]) ) {
                    $return[$key] = $this->cache[$key];
                    continue;
                } else if ( in_array($group, $this->no_mc_groups) ) {
                    $return[$key] = false;
                    continue;
                } else {
                    $return[$key] = $mc->get($key);
                }
            }
            if ( $to_get ) {
                $vals = $mc->get_multi( $to_get );
                $return = array_merge( $return, $vals );
            }
        }
        @ ++$this->stats['get_multi'];
        $this->group_ops[$group][] = "get_multi $id";
        $this->cache = array_merge( $this->cache, $return );
        return $return;
    }

    function key($key, $group) {
        global $blog_id;

        if ( empty($group) )
            $group = 'default';

        if (false !== array_search($group, $this->global_groups))
            $prefix = $blog_id;
        else
            $prefix = $blog_id . ':';

        return preg_replace('/s+/', '', "$prefix$group:$key");
    }

    function replace($id, $data, $group = 'default', $expire = 0) {
        $key = $this->key($id, $group);
        $expire = ($expire == 0) ? $this->default_expiration : $expire;
        $mc =& $this->get_mc($group);
        $result = $mc->replace($key, $data, false, $expire);
        if ( false !== $result )
            $this->cache[$key] = $data;
        return $result;
    }

    function set($id, $data, $group = 'default', $expire = 0) {
        $key = $this->key($id, $group);
        if ( isset($this->cache[$key]) && ('checkthedatabaseplease' == $this->cache[$key]) )
            return false;
        $this->cache[$key] = $data;

        if ( in_array($group, $this->no_mc_groups) )
            return true;

        $expire = ($expire == 0) ? $this->default_expiration : $expire;
        $mc =& $this->get_mc($group);
        $result = $mc->set($key, $data, false, $expire);

        return $result;
    }

    function colorize_debug_line($line) {
        $colors = array(
            'get' => 'green',
            'set' => 'purple',
            'add' => 'blue',
            'delete' => 'red');

        $cmd = substr($line, 0, strpos($line, ' '));

        $cmd2 = "<span style='color:{$colors[$cmd]}'>$cmd</span>";

        return $cmd2 . substr($line, strlen($cmd)) . "n";
    }

    function stats() {
        echo "<p>n";
        foreach ( $this->stats as $stat => $n ) {
            echo "<strong>$stat</strong> $n";
            echo "<br/>n";
        }
        echo "</p>n";
        echo "<h3>Memcached:</h3>";
        foreach ( $this->group_ops as $group => $ops ) {
            if ( !isset($_GET['debug_queries']) && 500 < count($ops) ) {
                $ops = array_slice( $ops, 0, 500 );
                echo "<big>Too many to show! <a href='" . add_query_arg( 'debug_queries', 'true' ) . "'>Show them anyway</a>.</big>n";
            }
            echo "<h4>$group commands</h4>";
            echo "<pre>n";
            $lines = array();
            foreach ( $ops as $op ) {
                $lines[] = $this->colorize_debug_line($op);
            }
            print_r($lines);
            echo "</pre>n";
        }

        if ( $this->debug )
            var_dump($this->memcache_debug);
    }

    function &get_mc($group) {
        if ( isset($this->mc[$group]) )
            return $this->mc[$group];
        return $this->mc['default'];
    }

    function failure_callback($host, $port) {
        //error_log("Connection failure for $host:$portn", 3, '/tmp/memcached.txt');
    }

    function WP_Object_Cache() {
        global $memcached_servers;

        if ( isset($memcached_servers) )
            $buckets = $memcached_servers;
        else
            $buckets = array('default' => array('127.0.0.1:11211'));

        foreach ( $buckets as $bucket => $servers) {
            $this->mc[$bucket] = new Memcache();
            foreach ( $servers as $server  ) {
                list ( $node, $port ) = explode(':', $server);
                $this->mc[$bucket]->addServer($node, $port, true, 1, 1, 15, true, array($this, 'failure_callback'));
                $this->mc[$bucket]->setCompressThreshold(20000, 0.2);
            }
        }
    }
}
?>

And put this on your wp-config :

1
2
3
4
5
global $memcached_servers;
$memcached_servers = array('default' => array('127.0.0.1:11211'));

global $blog_id;
$blog_id = 'your-domain-name-here';

,

wordpress

Google Street Map can’t work in WordPress 3.2 Solved!

25 Jul , 2011  

I tried to embed Google Map into WordPress 3.2 and see it’s works without any failure. But, not with Google Street Map. When i trying embed this javascript code, it’s only show boxes and not images.

I thought it was my NGINX configuration or w3-total cache plugin conflict with Google Street Map. So, i try installed brand new WordPress and use this code Google Street Code :

1
2
<div id="pano" style="width: 400px; height: 300px"></div>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script><script type="text/javascript">function init() {var fenway = new google.maps.LatLng(42.345573,-71.098326);var panoOptions = {position: fenway,addressControlOptions: {position: google.maps.ControlPosition.BOTTOM},linksControl: false, panControl: false,zoomControlOptions: {style: google.maps.ZoomControlStyle.SMALL},enableCloseButton: false};var panorama = new google.maps.StreetViewPanorama(    document.getElementById("pano"), panoOptions);}window.onload = init;</script>

Guess what? It’s still not works! When i use plain test.php inside wordpress, this code work succesfully. So, this is not about .htaccess, nginx configuration or something outside WordPress.

Right now i’m still searching way to make it fixed. I will updated this post soon after success embed Google Street Map into WordPress 3.2.1

Updated
I found the problem! It’s because Twenty Eleven Themes issue. Change your themes into Twenty Ten and you got Google Street View works!

Also, i posted this problem in http://wordpress.org/support/topic/google-street-view-not-work-in-twenty-eleven-themes?replies=1#post-2245186

Updated
I found solution, this Google Street View not loaded images because max-width CSS in style.css.

1
2
3
4
5
.entry-content img,
.comment-content img,
.widget img {
    max-width: 97.5%; /* Fluid images for posts, comments, and widgets */
}

You can choose 2 solution here :

1. Delete .entry-content img and you got Google Street View works. ( Not recommended )

2. Use “max-width: none” in Google maps image container.
http://wordpress.org/support/topic/twenty-eleven-12-breaks-embedded-google-maps?replies=3. Kudos to http://wordpress.org/support/profile/iorinne

,