php - How to insert data into a wordpress table using a custom function -
i have wordpress app hosted on azure, , need insert data wp_posts table, using custom function since i'm running script background job , cannot use wordpress functions since script not on wordpress folder.
i've tried:
mysql_query("insert wp_posts(post_title, post_content, post_status, post_type, comment_status, page_template), values($title, $sum, 'publish', 'post', 'closed', 'content.php')");
but following error: php warning: mysql_query(): attempt made access socket in way forbidden access permissions.
the log file follows:
[04/07/2014 09:51:47 > adf9f5: sys info] status changed initializing [04/07/2014 09:51:47 > adf9f5: sys info] run script 'data.php' script host - 'phpscripthost' [04/07/2014 09:51:47 > adf9f5: sys info] status changed running [04/07/2014 09:51:48 > adf9f5: err ] php warning: mysql_query(): attempt made access socket in way forbidden access permissions. [04/07/2014 09:51:48 > adf9f5: err ] in c:\dwasfiles\sites\abacuskenya\temp\jobs\triggered\y\rdksuocv.xbx\data.php on line 50 [04/07/2014 09:51:48 > adf9f5: err ] php warning: mysql_query(): link server not established in c:\dwasfiles\sites\abacuskenya\temp\jobs\triggered\y\rdksuocv.xbx\data.php on line 50 [04/07/2014 09:51:48 > adf9f5: err ] php warning: mysql_insert_id(): attempt made access socket in way forbidden access permissions.
how script insert data table using custom function other wordpress's wp_insert_post
my data.php follows:
<?php define('db_name', '**********'); define('db_user', '********'); define('db_password', '**************'); define('db_host', '*******'); $link = mysql_connect(db_host, db_user, db_password); if (!$link){ die('could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(db_name, $link); if(!$db_selected){ die('cannot use '. db_name . ': ' .mysql_error()); } $pages = array("page0", "page1", "page2", "page3", "page4"); foreach($pages $page){ $json_feed = "http://digitalrand.net/api/url_data/?key=*****&pass=*****%&".$page; $json = file_get_contents($json_feed); $obj = json_decode($json, true); foreach($obj $article_array){ $url = $article_array['url']; $domain = $article_array['domain']; $favicon = $article_array['favicon']; $title = $article_array['title']; $category = $article_array['category']; $large_summary = $article_array['summary']; $sum = implode(',',$large_summary); $images = $article_array['images']; $image_array = reset($images); $image = $image_array["image"]; $sql = "insert wp_posts(post_title, post_content, post_status, post_type, comment_status, page_template), values($title, $sum, 'publish', 'post', 'closed', 'content.php')"; echo $sql; mysql_query($sql); $post_id = mysql_insert_id(); echo "the post id " . $post_id . "<br>"; $data = array($favicon, $domain, $image); $value = implode(',',$data); //$a = add_post_meta($post_id, 'post_favicon_domain_image', $value, true); $sql2 = "insert wp_postmeta(post_id, meta_key, meta_value), values($post_id, 'post_favicon_domain_image', $value)"; mysql_query($sql2); //echo $a; } }
i suggest create content on wordpress using wp api. can anywhere want. if insert data wp db manually, system may not work well. example, when create post, sends email. if insert db manually, not work. instead of that, can use wp api below;
class wpclient { var $xmlrpcurl = ""; var $username = ""; var $password = ""; public function __construct($xmlrpcurl, $username, $password) { $this->xmlrpcurl = $xmlrpcurl; $this->username = $username; $this->password = $password; } function sendrequest($requestname, $params) { $request = xmlrpc_encode_request($requestname, $params); $ch = curl_init(); curl_setopt($ch, curlopt_postfields, $request); curl_setopt($ch, curlopt_url, $this->xmlrpcurl); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_timeout, 1); $results = curl_exec($ch); curl_close($ch); return $results; } function createpost($title, $body, $category, $keywords = '', $encoding='utf-8') { $title = htmlentities($title, ent_noquotes, $encoding); $keywords = htmlentities($keywords, ent_noquotes, $encoding); $postdata = array( 'title' => $title, 'description' => $body, 'mt_allow_comments' => 0, // if 1 => allow comments 'mt_allow_pings' => 0, // if 1 => allow trackbacks 'post_type' => 'post', 'mt_keywords' => $keywords, 'categories' => array($category) ); $params = array(0, $this->username, $this->password, $content, true); return $this->sendrequest('metaweblog.newpost', $params); } }
and usage;
$wpclient = new wpclient("http://yourdomain.com/xmlrpc.php", "your_username", "your_password"); $wpclient->createpost("your title", "your content", "post category", "your tags");
Comments
Post a Comment