Posts Mentioning RSS Toggle Comment Threads | Keyboard Shortcuts

  • January 12, 2010 Permalink | Reply  

    WPMU posts return 404 not found 

    I’ve installed WordPress MU on a fresh Apache httpd and I go to try it out. I start by clicking on the ‘hello world’ post that comes as default and the page returns a 404 “not found” message. What is happening? This keeps happening to me all the time so I might as well share it here for others.

    This can be fixed this by setting “AllowOverride to All” in the httpd.conf file, in the relevant Directory directive section where MU is installed.

     
  • November 25, 2009 Permalink | Reply  

    Authentication by POST/GET in WordPress 

    For a project I’m working on I needed to trigger an action in a remote WordPress installation. I had a bit of trouble understanding how I could authenticate myself against the remote site. Eventually I found a clean and nice way and I want to share it here with you. Of course, if you’re not into WordPress you can safely skip this post.

    The Problem

    Where I first stumbled was when from my plugin I tried to access directly the other plugin in the remote site. The code I was using for doing this was something like:


    // create curl resource
    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, "http://wordpress.mu/wp-admin/admin.php?page=myplugin");
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);
    // print the result of the whole request:
    print "CONTENT = ".$output;

    And this wouldn’t work because although I was authenticated in the remote site via the browser, the session that the curl request creates is not, and so this request results in us being redirected to the login page.

    What to do?

    Ideally we want to do a POST request that we execute before and that authenticates us. I searched around on how to do this but couldn’t find anything. What I found was one of the many third-party applications that allow talking to WordPress remotely. I then looked at how they work and how they authenticate themselves.

    Instead of doing a POST request to the plugin page like before, they do a POST request to the site’s index.php. A plugin is registered to catch a particular POST request. Then you simple use the WordPress function user_pass_ok(), that authenticates the user against the database.

    Here I demonstrate how to do this using a GET request, because its simpler, but the same thing would work with a POST request.

    add_action('plugins_loaded', 'unpackimport_createblog', -1);
    function unpackimport_createblog() {
    if(isset($_GET['myplugin'])) {
    if(user_pass_ok($_REQUEST['username'], $_REQUEST['password']))
    echo 'Authentication successful';
    }

    And the client request:

    curl_setopt($ch, CURLOPT_URL, "http://wordpress.mu/index.php?myplugin=true&username=test&password=testpass");

    Screencast

    I wanted to show a working example rather than just saying this works so I did this screencast. You can also download the plugins yourself and try them out.

    Download

    unpack_import_demo.tar.gz
    dump_pack_demo.tar.gz

     
  • October 16, 2009 Permalink  

    WordPress Media Buttons 

    If you know Wordpress, you know it is a great publishing platform. For a particular plugin I wanted to build I started looking at how to add one of those little icons you have access while writing a post, e.g. ‘Add an Image’, etc.

    I was looking at how other plugins do this and found this code:

    function wp_myplugin_media_button() {
    	$context = __('Add media: %s');
    	$wp_myplugin_media_button_image = '/path/media_button.gif';
    	$wp_myplugin_media_button = '<a href="media-upload.php?type=wp_myplugin&amp;TB_iframe=true" class="thickbox">
      <img src='".$wp_myplugin_media_button_image."' /></a>".' %s';
    	return sprintf($context, $wp_myplugin_media_button);
    }
    add_filter('media_buttons_context', 'wp_myplugin_media_button');
    

    But when I tried this in my plugin something weird happened. If both my plugin and the one from where I took this code were active, then only the button from the other plugin would appear. If I disabled the second one then it worked. Why, oh why? After struggling a bit I found the answer.

    The way the function is receiving the context is not correct. Both functions are asking for the default “context” (this is the media buttons themselves) and then adding something to it. The correct way to receive the context is to have it as an argument of the function. In that way it can be updated without a problem. Example follows:

    function wp_myplugin_media_button($context) {
    	$wp_myplugin_media_button_image = '/path/media_button.gif';
    	$wp_myplugin_media_button = '<a href="media-upload.php?type=wp_myplugin&amp;TB_iframe=true" class="thickbox">
      <img src='".$wp_myplugin_media_button_image."' /></a>".' %s';
    	return sprintf($context, $wp_myplugin_media_button);
    }
    add_filter('media_buttons_context', 'wp_myplugin_media_button');
    

    Update: if you want to add the button to the end of the media button list just append the %s first, e.g.:

    function wp_myplugin_media_button($context) {
    	$wp_myplugin_media_button_image = '/path/media_button.gif';
    	$wp_myplugin_media_button = ' %s' . '<a href="media-upload.php?type=wp_myplugin&amp;TB_iframe=true" class="thickbox">
      <img src='".$wp_myplugin_media_button_image."' /></a>";
    	return sprintf($context, $wp_myplugin_media_button);
    }
    add_filter('media_buttons_context', 'wp_myplugin_media_button');
    

    Hope it helps someone. Cheers.

     
    • k3 10:31 pm on October 25, 2009 Permalink

      This is actually really helpful, thank you! I was struggling with a similar issue.

      This isn’t specifically related to your code above, but I’m not sure how to add my custom media button to the end of the media button list instead of the beginning (as this code does). Any suggestions?

    • Nuno Morgadinho 4:12 pm on October 26, 2009 Permalink

      Hi k3,

      I’ve updated the post to show how to add it to the end of the media button list. It’s actually quite simple.

      Cheers,
      Nuno

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel