• JLCodE Team

Creating WordPress Posts from external web applications


Hi. Everyone.


As we know, Wordpress website provide features which users can create posts with categories, tags and attributes.

Wordpress site is based on php languages and use mysql database.

Sometimes, people want create posts automatically when do something from external websites. For example, User A create event and blog in external site A, and need to push it into WordPress site B automatically.

In this case, WP site must need to be customised.


Our Team have enough skills in this kind of building features.

External site can be built with any kind of frameworks and languages. It can be built with node.js, php , Django, python, etc. External resource can be mobile apps.

Most important part is in WordPress site.

If you are programmer or developer, you maybe know about wp_ prefix functions like wp_post_update, wp_post_insert, etc.


Here, we will introduce pure php example code for creating wordpress post.


- External site code.

send post request to wordpress site using php CURL.


- WordPress site code

1. create new php file in wp root folder called createpost.php

**important**

You must include wp-load.php in the first line.

2.call wp_remote_post function with useful parameters.


$api_response = wp_remote_post'{domain}/wp-json/wp/v2/posts'array( 'sslverify' => FALSE, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode'{username}:{password} ')             ), 'body' => array( 'title'   => 'Test Post',//$_POST['subject'], 'status'  => 'draft'// publish, draft, future,etc, -> post status 'content' => 'Test Content',//post content 'categories' => 2// category ID 'tags' => '2,3,4' // string, comma separated 'date' => $date// YYYY-MM-DDTHH:MM:SS 'excerpt' => 'Read this awesome post', 'password' => '12$45', 'slug' => 'test_post',//str_replace(' ','_',$subject) // part of the URL usually    ) ) );

$body = json_decode$api_response['body'] ); // you can always print_r to look what is inside // print_r( $body ); // or print_r( $api_response ); ifwp_remote_retrieve_response_message$api_response ) === 'Created' ) { //echo 'The post ' . $body->title->rendered . ' has been created successfully'; $arg = array( 'ID' => $body->id, 'post_author' => $user_id,     ); wp_update_post$arg ); echo $body->id;//$lastid = $wpdb->insert_id; }


Additionally, we can change the post featured image when create post from external applications.


require('wp-load.php'); $post_id = intval($_POST['postid']); $featured = $_POST['image']; $image_name       = $featured; $upload_dir       = wp_upload_dir(); // Set upload folder $unique_file_name = wp_unique_filename$upload_dir['path'], $image_name ); // Generate unique name $filename         = basename$unique_file_name ); // Create image file name ifwp_mkdir_p$upload_dir['path'] ) ) { $file = $upload_dir['path'] . '/' . $filename; copy('{external resource}'.$featured$file); else { $file = $upload_dir['basedir'] . '/' . $filename; copy({external resource}'.$featured$file); } // Check image file type $wp_filetype = wp_check_filetype$filenamenull ); // Set attachment data $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title'     => sanitize_file_name$filename ), 'post_content'   => '', 'post_status'    => 'inherit' ); // Create the attachment $attach_id = wp_insert_attachment$attachment$file$post_id ); // Include image.php require_once(ABSPATH . 'wp-admin/includes/image.php'); // Define attachment metadata $attach_data = wp_generate_attachment_metadata$attach_id$file ); // Assign metadata to attachment wp_update_attachment_metadata$attach_id$attach_data ); // And finally assign featured image to post set_post_thumbnail$post_id$attach_id );


That's all.

Next time, we will introduce you about using Google Cloud Print in Laravel frameworks.

Thanks.

JLCODE


16 views

JunLong Science Technology Co.LTD

JL CODE

©2020 by Professional Development Team