Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Basically I am trying to display data from WordPress site #1 on WordPress site #2, and to get around the issue of trying to connect to the database, I thought it might be smarter to make an API-like plugin to return the information.

My notion was to build a URL with a parameter which would be used to pull the information I need. Here's all I've got so far...

<?php

if ( isset( $_GET['post_id'] ) ) {

    $post_id = $_GET['post_id'];

    // use WordPress functionality to retrieve data with $post_id

    echo $stuff;

}

Let's say the location of this code is /wp-content/plugins/custom-api.php. I want to be able to visit, for example, the URL /wp-content/plugins/custom-api.php?post_id=420 and have it spit back some information on the page (like post title, thumbnail, whatever).

The issue I'm having is that I don't know how to gain access to use the WordPress functions. I know normally with plugins you hook in a function, and then any WP functions you use will work.

Here, I'm not trying to hook in; I'm trying to hook out. If I use any WordPress native function, it's not recognized as existing. I'm kind of stumped at the moment. Any suggestions or links to resources I could mull over would be much appreciated.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You might not need a plugin for this. You can do it this way.

Say you are fetching data from WP1 to WP2 instances.

  1. create a page in WP1.. say "Custom API" with url /custom-api/.
  2. create a template in WP1 named "custom api" in WP1
  3. assign page "Custom API" to template "custom api".

Since your template is a regular WordPress template you have access to all the WP functions.i.e. the ability to retrieve wordpress data.

  1. in WP2, however you want to do it, make a web service call to /custom-api/ (the page you

create in step 1).

share|improve this answer
    
Very cool idea!!! You're a genius. All hail rxn. I'll be having a lot of fun with this... –  Adam Capriola Jan 25 '12 at 23:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.