Publish custom html page using wordpress post api
-
Hello Milos,
I researched so much to publish custom html (eg. <html> to </html>) and i finally found this helpful plugin.
I am moving my whole site to wordpress so, I want to move my all pages to wordpress site. all pages html is already ready to my end. I just want to use this plugin to publish it on my wordpress site so, as i said there are thousands of pages i have to move at a time. is there any way to publish custom html pages by using API with wpchtmlp_page post type or any onother way? Please help.
Thanks in advance.
-
Hello,
it is possible to enable WP Custom HTML Pages to publish through WP API, but a bit of coding is required. Major update will be published in a few days which will bring lots of improvements including the feature you asked for as a toggle option.
If you want to change the code yourself, here are some instructions.
Most general instructions about making a plugin available through the rest API: https://developer.www.ads-software.com/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/#adding-rest-api-support-to-existing-content-types
The accepted answer to this question has two examples: https://wordpress.stackexchange.com/questions/294085/wordpress-rest-create-post-of-custom-type/294101
Thanks Milos,
as you said i changed the code. now i am able to publish page through API but now the problem is i cant set custom html and custom permalink through API.following is the body of xhr request:
{
“title”: “example”,
“status”: “publish”,
“slug”: “this-is-permalink”,
“content”: `<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style type=”text/css”></style>
</head>
<body>
</body>
</html>
`
}Hello,
good job getting that far, and sorry for giving you an incomplete solution.Custom fields have to be exposed server side so more WP coding is required. Best way to do this is by using wp_register_field https://developer.www.ads-software.com/rest-api/extending-the-rest-api/modifying-responses/#using-register_rest_field
I will be able to post a complete solution in a couple of hours.
Hello, try this please. It should expose custom metadata as API fields for both reading and writing, named html_permalink and html_code
add_action( 'rest_api_init', 'WPCHTML_create_api_fields' ); //or whatever name you give to the function below function WPCHTML_create_api_fields() { ? ? ? //register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() ) ? ? //for custom permalink ? ? register_rest_field( 'wpchtmlp_page', 'html_permalink', array( ? ? ? ? ? ?'get_callback' ? ?=> 'WPCHTMLP_get_api_post_meta_permalink', //or whatever you name the callback function below ? ? ? ? ? ?'update_callback' => 'WPCHTMLP_update_api_post_meta_permalink', //name of update callback function at bottom ? ? ? ? ? ?'schema' ? ? ? ? ?=> null, ? ? ? ? ) ? ? ); ? ? //for HTML content ? ? register_rest_field( 'wpchtmlp_page', 'html_code', array( ? ? ? ? ? ?'get_callback' ? ?=> 'WPCHTMLP_get_api_post_meta_code', //or whatever you name the callback function below ? ? ? ? ? ?'update_callback' => 'WPCHTMLP_update_api_post_meta_code', //name of update callback function at bottom ? ? ? ? ? ?'schema' ? ? ? ? ?=> null, ? ? ? ? ) ? ? ); } function WPCHTMLP_get_api_post_meta_permalink( $object, $field_name, $request ) { ? ? $meta = get_post_meta( $object['id'], 'WPCHTMLP_page_meta_box', true ); ? ? //return the post meta ? ? return $meta['html_permalink']; } function WPCHTMLP_get_api_post_meta_code( $object, $field_name, $request ) { ? ? $meta = get_post_meta( $object['id'], 'WPCHTMLP_page_meta_box', true ); ? ? //return the post meta ? ? return $meta['html_code']; } function WPCHTMLP_update_api_post_meta_permalink($value, $object, $field_name){ ? return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_permalink', $value)); } function WPCHTMLP_update_api_post_meta_code($value, $object, $field_name){ ? return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_code', $value)); }
hello, I put above code and changed keys content to html_code and slug to html_permalink in the request body. but now it’s giving error with status 500.
below is the error response:
{
“code”: “internal_server_error”,
“message”: “The site is experiencing technical difficulties.”,
“data”: {
“status”: 500
},
“additional_errors”: []
}and below is the error in console:
Access to XMLHttpRequest at https://example.com/wp-json/wp/v2/wpchtmlp_page from origin https://localhost:4200 has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
I have solved CORS error by putting header(“Access-Control-Allow-Origin: *”); at the top. but error 500 is still present.
Hello, I pasted the exact code from my post into the plugin file and have got server errors as well, which were caused by invisible non-breaking space characters which somehow got into the code.
Please copy paste the cleaned version of the code from the link below:
Let me know if you get further errors upon using API. Feel free to contact me using the gmail address (I have sent a reply to you yesterday).
In order to display PHP errors:
If using Apache locate php.ini and replace these lines:
(change to On:)
display_errors = On(remove comment “;” from two lines below)
display_startup_errors
; Default Value: Off
; Development Value: On
; Production Value: Offerror_reporting
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATEDHello, I copy paste code from the link I am still getting error. If you tried same is it successfully done at your end?
I am using postman and angular to test api call.
Hello, what you are getting is a server error so please find and post here the error from your web server’s log.
Hello milos, Below is the error log.
<strong>Warning</strong>: Illegal string offset 'html_permalink' in <strong>/www/sandboxframework11_975/public/wp-content/plugins/wp-custom-html-pages/wp-custom-html-pages.php</strong> on line <strong>292</strong> <strong>Notice</strong>: Uninitialized string offset: 0 in <strong>/www/sandboxframework11_975/public/wp-content/plugins/wp-custom-html-pages/wp-custom-html-pages.php</strong> on line <strong>292</strong> <strong>Warning</strong>: Illegal string offset 'html_code' in <strong>/www/sandboxframework11_975/public/wp-content/plugins/wp-custom-html-pages/wp-custom-html-pages.php</strong> on line <strong>286</strong> <strong>Notice</strong>: Uninitialized string offset: 0 in <strong>/www/sandboxframework11_975/public/wp-content/plugins/wp-custom-html-pages/wp-custom-html-pages.php</strong> on line <strong>286</strong>
Hello, while post request its giving following error in log:
2019/11/18 11:14:13 [error] 130878#130878: *6306 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Cannot use object of type WP_Post as array in /www/sandboxframework11_975/public/wp-content/plugins/wp-custom-html-pages/wp-custom-html-pages.php:298 Stack trace: #0 /www/sandboxframework11_975/public/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(431): WPCHTMLP_update_api_post_meta_permalink('custom-article1', Object(WP_Post), 'html_permalink', Object(WP_REST_Request), 'wpchtmlp_page') #1 /www/sandboxframework11_975/public/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(602): WP_REST_Controller->update_additional_fields_for_object(Object(WP_Post), Object(WP_REST_Request)) #2 /www/sandboxframework11_975/public/wp-includes/rest-api/class-wp-rest-server.php(946): WP_REST_Posts_Controller->create_item(Object(WP_REST_Request)) #3 /www/sandboxframework11_975/public/wp-includes/rest-api/class-wp-rest-server.php(329): WP_REST_Server->dispatch(Object(WP_REST_Request)) #4 /www/sandboxframework11_975/public/wp-includes/rest-api.ph" while reading response header from upstream, client: 116.75.133.173, server: framework11.com, request: "POST /wp-json/wp/v2/wpchtmlp_page HTTP/1.0", upstream: "fastcgi://unix:/var/run/php7.3-fpm-sandboxframework11.sock:", host: "framework11.com", referrer: "https://localhost:4200/"
following is the Line no 298 in plugin code –
return update_post_meta($object['id'], 'WPCHTMLP_page_meta_box', array('html_permalink', $value));
Hi, if it is OK with you we can continue this correspondence via email or google chat [email protected]
Please attach your edited version of wp-custom-html-pages.php
It took a while since I lost touch with codebase for this version, but the issue is solved as the poster can now create working custom HTML pages through API.
I don’t have access to the full code, for those who want to code their own feature
it involves:add_action( ‘rest_api_init’, ‘WPCHTML_create_api_fields’ );
function WPCHTML_create_api_fields() {
foreach( array( ‘html_permalink’, ‘html_code’ ) as $field ) {
register_rest_field( ‘wpchtmlp_page’, $field, array(
‘get_callback’ => ‘WPCHTMLP_get_api_post_meta’,
‘update_callback’ => ‘WPCHTMLP_update_api_post_meta’,
‘schema’ => null
)
);
}
}function WPCHTMLP_update_api_post_meta($value, $object, $fieldName){
….
Inside WPCHTMLP_update_api_post_meta callback function:1 – get post meta
and alter its $meta[‘html_permalink’] and $meta[‘html_code’] fields.
$meta = get_post_meta( $object[‘id’], ‘WPCHTMLP_page_meta_box’, true );
$meta[$fieldName] = $value;2 – update post meta with altered one
update_post_meta($object->ID, ‘WPCHTMLP_page_meta_box’, $meta);3 – after update also handle database row, you will want to make a check and then insert if new or update if existing post_id
To insert new row:
global $wpdb;
$table_name = $wpdb->prefix . ‘wpchtmlp_pages’;
$wpdb->show_errors();
$wpdb->insert(
$table_name,
array(
‘post_id’ => $object->ID,
‘time’ => current_time( ‘mysql’ ),
‘name’ => get_the_title( $object->ID ),
‘html’ => $meta[‘html_code’],
‘url’ => $meta[‘html_permalink’]
)
);
To update existing row:
global $wpdb;
$table_name = $wpdb->prefix . ‘wpchtmlp_pages’;
$wpdb->show_errors();
$wpdb->update(
$table_name,
array(
‘time’ => current_time( ‘mysql’ ),
‘name’ => get_the_title( $object->id ),
‘html’ => $meta[‘html_code’],
‘url’ => $meta[‘html_permalink’]
),
array(‘post_id’=>$object->id)
);That was bare basics, you will also want to keep in mind security concerns of exposing this plugin to API input, to prevent malicious use of API.
- The topic ‘Publish custom html page using wordpress post api’ is closed to new replies.