1

So, we have a custom CMS in which writers we've hired write articles for clients of ours. We are tryint to take the content of those articles and publish them on our clients' WP sites. At this point, this is a manual process and a real pain in the behind.

I'm trying making a custom plugin that pulls article data from our custom CMS and (what we want it to do is) creates a new post with the title, article, ad some other content to the client's WP site. Ideally, we would install the plugin on our clients' sites and it would then start to pull articles hourly (using a cron job plugin or something) from our in house CMS straight into our client's sites as new posts.

I am having some trouble writing the plugin. I want to make it so that on activation, this plugin hits our CMS' database and starts making new posts. I'm unsure as to what

Here is what I have so far for my custom plugin(I am unsure as to what do to in the add_action() function/ how to do it).

 <?php
    /**
 * @package New
 * @version 1.6
 */
/*
Plugin Name: CMS
Plugin URI:
Description: Practice
Author: Moe
Version: 1.0
Author URI: N/A
*/

function cms_db_connect(){
    //global $wpdb;
    $cms_wpdb = new wpdb( 'ourdbusername', 'password', 'dbname', 'hostname');
    $cms_wpdb->show_errors();
    $sql = "
        SELECT * from articles as a
        INNER JOIN campaign_keyword as ck ON a.keyword_id = ck.keyword_id
        INNER JOIN client_campaigns as cc ON ck.campaign_id = cc.campaign_id
        INNER JOIN client_wp_credentials as cwp ON cc.client_id = cwp.client_id
        WHERE a.article_status = '5'";
        //die(print_r($sql));
        $cms = $cms_wpdb->get_results($sql);
        foreach($cms as $key => $value){
            $title = $key['title'];
            $author = $key['ordered_by'];
            $body = $key['richtext_body'];

            //doing some more stuff here
        }
        return $cms;

}

add_action('','cms_db_connect', 10, 2);

/*

*/

?>
Moe Kahn
  • 11
  • 1
  • To communicate with a WordPress instance, you'll want an HTTP library so you can create an automated web client. I've had success automating WordPress posting using PHP `curl`. Unfortunately I don't have sample code handy at the moment. UDPATE [Here](http://stackoverflow.com/questions/728274/php-curl-post-to-login-to-wordpress) is a `curl` snippet that logs in to WordPress. – Juan Tomas Aug 04 '16 at 20:31
  • Perhaps I misunderstood your post but what I am trying to do is have WP (via a plugin) communicate with the custom CMS we have (specifically, its database), not the other way around. – Moe Kahn Aug 11 '16 at 15:28
  • It's the same in principle. You have a CMS. You can access content via HTTP. `curl` speaks HTTP, and makes some provision to deal with session data such as cookies for login purposes. So, try using `curl` in `php` to fetch a page from your CMS and dump the content to the screen. Once you've got that (and it should be a pretty quick exercise), you can integrate the concept with your WP plugin, and figure out how to dump the content as a new WP post. – Juan Tomas Aug 12 '16 at 13:51

0 Answers0