Doug Sparling

Mobile/Web Developer

Month: October, 2013

Using cURL to Test the WordPress Theme and Plugin APIs

by Doug Sparling

Occasionally (rarely) when trying to search for a theme or plugin via the WordPress dashboard, you may see an error like this:

An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums.

This means that the request WordPress has made to the theme or plugins api has failed, or that the body of the response is bad or empty. Often web hosts will turn off outbound http requests and this will be the source of your problem. However, there can be a myriad of other issues that may cause this error. WordPress will use one of three “transports” and search for them on your server in this order: curl, streams, and fsockopen. Since the focus of this article is on using cURL, that’s what will use at the command line.

To check if cURL is installed on your server, use the Unix ‘which’ command to find it’s install location.

$ which curl

and you should get a response something like this (your path may vary):

/usr/bin/curl

To simply check connectivity with the WordPress theme and plugin apis, you can make an http HEAD request with cURL:

$ curl -I http://api.wordpress.org/plugins/info/1.0/

and

$ curl -I http://api.wordpress.org/themes/info/1.0/

You should see output something like this:

<br />HTTP/1.1 200 OK<br />Server: nginx<br />Date: Tue, 15 Oct 2013 14:06:36 GMT<br />Content-Type: text/html; charset=utf-8<br />Connection: close<br />Vary: Accept-Encoding<br />

If you don’t have connectivity, you may see something like this:

<br />curl: (6) Could not resolve host: api.wordpress.org; nodename nor servname provided, or not known<br />

If you want to duplicate the request made when you’re actually on the WordPress dashboard, you’ll have to make a POST request with serialized data parameters. To mimic a search for a “blue” theme, use this cURL command:

$ curl --data 'action=query_themes&amp;request=O:8:"stdClass":4:{s:4:"page";i:1;s:8:"per_page";i:36;s:6:"fields";N;s:6:"search";s:4:"blue";}' http://api.wordpress.org/themes/info/1.0/

To mimic a search for a “cache” plugin, use this command:

$ curl --data 'action=query_plugins&amp;request=O:8:"stdClass":3:{s:4:"page";i:1;s:8:"per_page";i:30;s:6:"search";s:5:"cache";}' http://api.wordpress.org/plugins/info/1.0/

A successful request will return quite a bit of HTML and serialized data (which I won’t post here).

Contributing to WordPress

by Doug Sparling

Contributing to an Open Source project is fun. (and it’s good for you!) Not only that it’s educational. I can’t tell you how much I’ve learned by answering questions in the WordPress.org Support Forums (you can find me here), doing an occasional bit of work on the WordPress Documentation Team, and writing and releasing a handful of WordPress plugins.

As you can see, there are many ways to get involved and contribute to WordPress. (and I’ve just done a few of them) Whether you’re a WordPress developer, theme author, or just simply a fan or user of WordPress, there’s probably a way you can contribute.

And one important point I’ve neglected to mention…you’ll be helping others and you’ll be helping WordPress! And I don’t have to tell you, but by giving back to the fabulous WordPress community you’ll make the world just a little better place than it was before.

If you’re interested (and I hope you are), I’ve curated a few links to help you get started.

WordPress.org – Codex

Make WordPress

WordPress.org –  Make WordPress Core

General

Core

Documentation

Video

Misc

If you got any additional resources, don’t hesitate to send them my way.

Moderate Use of Shortcodes in WordPress

by Doug Sparling

In a recent question on the WordPress.org forums a user wanted to know if there was a way to prevent non-admins from using shortcodes when creating/editing posts.

The solution I came up won’t prevent non-admin users from entering shortcodes, but it will prevent WordPress from saving them.

Add this to your theme’s functions.php:

function filter_handler( $data , $postarr ) {
    if ( !current_user_can( 'manage_options' ) ) {
        $data['post_content'] = preg_replace( "/\[([^\[\]]++|(?R))*+\]/", "", $data['post_content'] );
    }
    return $data;
}
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

This will prevent anyone other than admin/superadmin from inserting shortcodes (well, it will delete a pair of square brackets and anything in between when a non-admin inserts or updates a page/post – if there’s a better way I haven’t found it yet).

WordPress Posts Page ID

by Doug Sparling

To find the ID of the page that contains the posts (the listing of posts), you use the WordPress function:

get_option('page_for_posts')

The caveat is that this value will only be set if you assign a static page as your front page and assign another page as your posts page (via Settings->Reading). When you run WordPress where the front page displays your blogs posts, that’s a special case and there is no “posts page” id. (actually, it comes back as 0).

$posts_page_id = get_option( 'page_for_posts' );
echo "page id: " . $posts_page_id;